尝试为从udp套接字接收数据的简单程序构建框架。然而,我会收到一条收到的消息,消息永远不会弹出,如果我尝试重新发送消息,则会弹出消息但反复出现同样的问题。我确信我错过了一些愚蠢的东西,但不能放置它。
#include <sys/socket.h>
#include <strings.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
int shared_sock,n, received, receivelen;
struct sockaddr_in receivesocket;
socklen_t len;
char mesg[100];
unsigned char assist_id[] = {0x18, 0xDE, 0x1B};
int main()
{
shared_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
/* my address */
memset(&receivesocket, 0, sizeof(receivesocket));
receivesocket.sin_family = AF_INET;
receivesocket.sin_addr.s_addr = htonl(INADDR_ANY);
receivesocket.sin_port = htons(11000);
receivelen = sizeof(receivesocket);
if (bind(shared_sock, (struct sockaddr *) &receivesocket, receivelen) < 0) {
perror("bind");
return 1;
}
memset(mesg, 0, 100);
while(true){
if ((received = recvfrom(shared_sock, mesg, 100, 0, NULL, NULL)) < 0){
perror("recvfrom");
return 1;
}
printf("Received the following %d:\n", received);
printf("%s",mesg);
}
return 0;
}