从下面的代码中,我需要添加什么代码才能知道客户端何时连接到服务器?谢谢!:D
int sd, rc;
socklen_t cliLen;
struct sockaddr_in cliAddr, servAddr;
char* argv = (char*) arg;
/* socket creation */
sd=socket(AF_INET, SOCK_DGRAM, 0);
if(sd<0)
{
printf("%s: cannot open socket \n",argv);
// exit(1);
}
/* bind local server port */
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(LOCAL_SERVER_PORT);
rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
if(rc<0)
{
printf("%s: cannot bind port number %d \n",
argv, LOCAL_SERVER_PORT);
// exit(1);
}
printf("%s: waiting for data on port UDP %u\n",argv,LOCAL_SERVER_PORT);
while(1)
{
/* init buffer */
memset(msg,0x0,MAX_MSG);
/* receive message */
n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *) cliAddr,sizeof(cliAddr));
}
由于这是通过 UDP(无连接)完成的,我们无法知道客户端何时连接。有没有办法让我们知道谁发送了数据?