当前位置: 技术问答>linux和unix
请教socket问题
来源: 互联网 发布时间:2017-01-03
本文导语: 先贴代码 void ser_recev() { int sin_len; char buf_x[100]; char *buf_1="收到信息"; char *buf_2=buf_x; int sockfd; struct sockaddr_in my_addr_1; struct sockaddr sendip; my_addr_1.sin_family=AF_INET; my_addr_1.sin_addr.s_addr=htonl(INADDR_ANY);//inet_addr("127.0.0.1")...
先贴代码
void ser_recev()
{
int sin_len;
char buf_x[100];
char *buf_1="收到信息";
char *buf_2=buf_x;
int sockfd;
struct sockaddr_in my_addr_1;
struct sockaddr sendip;
my_addr_1.sin_family=AF_INET;
my_addr_1.sin_addr.s_addr=htonl(INADDR_ANY);//inet_addr("127.0.0.1");//htonl(INADDR_ANY);
my_addr_1.sin_port=htons(myport);
bzero(&(my_addr_1.sin_zero),8);
sin_len=sizeof(sendip);
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
printf("Error opening socketn");
exit(1);
}
if(bind(sockfd,(struct sockaddr *)&my_addr_1,sizeof(struct sockaddr_in))==-1)
{
printf("Error in bindn");
printf("errno value:%d,it means:%sn",errno,strerror(errno));
exit(1);
}
while(1)
{
if(recvfrom(sockfd,buf_x,sizeof(buf_x),0,(struct sockaddr *)&sendip,sin_len)!=1)
{
g_printf(buf_1);
g_printf(buf_2);
for(i=0;i
void ser_recev()
{
int sin_len;
char buf_x[100];
char *buf_1="收到信息";
char *buf_2=buf_x;
int sockfd;
struct sockaddr_in my_addr_1;
struct sockaddr sendip;
my_addr_1.sin_family=AF_INET;
my_addr_1.sin_addr.s_addr=htonl(INADDR_ANY);//inet_addr("127.0.0.1");//htonl(INADDR_ANY);
my_addr_1.sin_port=htons(myport);
bzero(&(my_addr_1.sin_zero),8);
sin_len=sizeof(sendip);
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
{
printf("Error opening socketn");
exit(1);
}
if(bind(sockfd,(struct sockaddr *)&my_addr_1,sizeof(struct sockaddr_in))==-1)
{
printf("Error in bindn");
printf("errno value:%d,it means:%sn",errno,strerror(errno));
exit(1);
}
while(1)
{
if(recvfrom(sockfd,buf_x,sizeof(buf_x),0,(struct sockaddr *)&sendip,sin_len)!=1)
{
g_printf(buf_1);
g_printf(buf_2);
for(i=0;i