UDP服务器无法发送消息

时间:2019-08-12 04:29:04

标签: c network-programming

我正在尝试在同一主机上的发送方和接收方之间建立UDP连接。客户端可以发送消息,但服务器无法答复。错误在哪里,该如何解决?

我猜这是错误的原因是接收者的发送到或发送者的接收中的参数列表。

SENDER CODE

df.set_index(['hour', 'sensor_id']).unstack(fill_value=0).stack().reset_index()

Out[171]:
    hour  sensor_id  hourly_count
0      1        101           651
1      1        102            19
2      2        101           423
3      2        102            12
4      3        101           356
5      3        102             0
6      4        101            79
7      4        102            21
8      5        101           129
9      5        102             0
10     6        101           561
11     6        102             0

收件人代码

void main()
   {
   int sockd=socket(AF_INET,SOCK_DGRAM,0);  //socket creation
   if(sockd==-1)
   { printf("Socket function failed\n");
   }
   else
   { printf("Socket creation successfull\n");
   } 

   struct sockaddr_in sender;
   sender.sin_family=AF_INET;
   sender.sin_port=htons(6521);
   sender.sin_addr.s_addr=inet_addr("127.0.0.1"); 


   int bindno=bind(sockd,(const struct sockaddr*)&sender,sizeof(sender));
   if(bindno==-1)
   { printf("Bind failure\n");
   }
   else
   { printf("Bind successfull\n");
   }

   struct sockaddr_in destaddr;
   destaddr.sin_family=AF_INET;
   destaddr.sin_port=htons(9446);
   destaddr.sin_addr.s_addr=inet_addr("127.0.0.1");

  char buf[100]="Hello from sender!";
  int sendno=sendto(sockd,buf,strlen(buf),0,(const struct sockaddr*)&destaddr,sizeof(destaddr));
 if(sendno==-1)
 { printf("Sending msg failed\n");
 }
 else
 { printf("Sending msg successful\n");
  }   

 char bufrecv[100];
 struct sockaddr_in recvaddr;
 int len;
 int recvno=recvfrom(sockd,bufrecv,sizeof(bufrecv),0,(struct sockaddr*)&recvaddr,&len);
 if(recvno==-1)
 { printf("Receive failed\n");
 }
 else
 { printf("Message received: %s",bufrecv);
 }
close(sockd);
}

0 个答案:

没有答案