Reputation: 3433
From the below code, what code do i need to add in order to know when has the client connected to the server? Thanks! :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));
}
Since this is done through UDP(connectionless), we are unable to know when the client has connected. Is there a way for us to know who sent the data?
Upvotes: 0
Views: 214
Reputation: 29966
You are using SOCK_DGRAM
which indicates a UDP socket.
UDP is not like TCP, there is no "connection" really, you just send packages of data and hope they reach the target.
However, if you want to indicate the fact of receiving something, you could add
printf("Recieved data");
after
n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *) cliAddr,sizeof(cliAddr));
Upvotes: 3