我有一个UDP套接字,我的客户端将一个整数数组发送到服务器,服务器接收它并将其存储为变量。这发生在无限循环中。我没有在服务器端使用任何memset函数进行缓冲。 我检查了服务器端的结果,一切都正确收到。
我想知道是否有必要在我拥有recvfrom()函数的每次迭代中使用memset ...
Servercode:
#define BUFSIZE 10
#define SERVERPORT "9090"
int main(int argc, char **argv)
{
struct sockaddr_in serveraddr4; /* server's addr */
struct sockaddr_in clientaddr4; /* client addr */
struct hostent *hostp4; /* client host info */
int sockfd4; /* socket */
int portno4; /* port to listen on */
socklen_t clientlen4;
char *hostaddrp4; /* dotted decimal host addr string */
int optval4; /* flag value for setsockopt */
int n4; /* message byte size */
sockfd4 = socket(AF_INET, SOCK_DGRAM, 0);
portno4 = atoi("8080");
optval4 = 1;
setsockopt(sockfd4, SOL_SOCKET, SO_REUSEADDR,(const void *)&optval4 , sizeof(int));
bzero((char *) &serveraddr4, sizeof(serveraddr4));
serveraddr4.sin_family = AF_INET;
serveraddr4.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr4.sin_port = htons((unsigned short)portno4);
int b4=bind(sockfd4, (struct sockaddr *) &serveraddr4, sizeof(serveraddr4));
clientlen4 = sizeof(clientaddr4);
int buf4[BUFSIZE];
int A;
int B;
int buf4[BUFSIZE];
while(1)
{
int n4 = recvfrom(sockfd4, buf4, BUFSIZE, 0,(struct sockaddr *) &clientaddr4, &clientlen4);
A=buf4[0];B=buf4[1];
cout<<A<<" "<<B<<endl;
}
}