Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C udp Sockets
hey
I wanted to create an asynchronous/non-blocking udp client server application where the client and server were supposed to chat away with each other without waiting for each other's turns.
I came to know that this could be done by select()...
 

here is my Server(Mentioning only the communication part):
fd_set readfds,writefds;
    while(1){
 

        FD_ZERO(&readfds);
        FD_ZERO(&writefds);
        FD_SET(sd,&readfds);
        FD_SET(sd,&writefds);
 
        int rv = select(n, &readfds, NULL, NULL, NULL);
        if(rv==-1)
        {
            printf("Error in Select!!!\n");
            exit(0);
        }
        if(rv==0)
        {
            printf("Timeout occurred\n");
        }
        if (FD_ISSET(sd, &readfds))
             {
                
                 int client_length = (int)sizeof(struct sockaddr_in);
                 memset(&buffer,0,sizeof(buffer));
        int bytes_received = recvfrom(sd, buffer,SIZE, 0, (struct sockaddr *)&client, &client_length);
        if (bytes_received < 0)
        {
            fprintf(stderr, "Could not receive datagram.\n");
            closesocket(sd);
            WSACleanup();
            exit(0);
        }
             }
 
        printf("\nClient says: %s",buffer);
            
 

        printf("\nWrite :");
        
 
        fgets(buffer,SIZE,stdin);
 
        if(FD_ISSET(sd,&writefds))
            {
               
                int client_length = (int)sizeof(struct sockaddr_in);
            if(sendto(sd, buffer,strlen(buffer), 0, (struct sockaddr *) &client,client_length)<0)
        {
            printf("Error sending the file! \n");
            printf("%d\n",WSAGetLastError());
            exit(1);
        }
            }
 

    }
    closesocket(sd);
    WSACleanup();
 
    return 0;
 
}
 
and this is my client:
fd_set readfds,writefds;
	while(1)
	{
		FD_ZERO(&readfds);
		FD_ZERO(&writefds);
		FD_SET(cs,&readfds);
		FD_SET(cs,&writefds);
		int rv=select(n,&readfds,&writefds,NULL,NULL);
        if(rv==-1)
		{
			printf("Error in Select!!!\n");
			exit(0);
		}
		if(rv==0)
		{
			printf("Timeout occurred\n");
		}
	printf("\nWrite  ");
 
	fgets(send_buffer,SIZE,stdin);
	if(FD_ISSET(cs,&writefds))
	
	   
		
			if(FD_ISSET(cs,&writefds))
			{
				int server_length = sizeof(struct sockaddr_in);
				FD_CLR(cs,&writefds);
	
	if (sendto(cs, send_buffer, (int)strlen(send_buffer) + 1, 0, (struct sockaddr *)&server, server_length) == -1)
	{
		fprintf(stderr, "Error transmitting data.\n");
		closesocket(cs);
		WSACleanup();
		exit(0);
	}
			}
	
	char file_buffer[SIZE];
	//Reply reception from the server:"Ready to receive file"
	int data2=0;
	if (FD_ISSET(cs, &readfds))
    {
		FD_CLR(cs,&readfds);
		int server_length = sizeof(struct sockaddr_in);
    data2=recvfrom(cs,file_buffer,strlen(file_buffer)-1,0,(struct sockaddr *)&server,&server_length);
	//file_buffer[data2]='\0';
	if(data2<0)
	{
		printf("Server is not on:(\n");
		exit(0);
	}
			 }
	//printf("%d",data2);
	printf("\nServer says:");
	for(int i=0;i<data2;i++)
	{
		putchar(file_buffer[i]);
	} 
	}
	
 
	return 0;
}
 

At first on the server side I wrote:
int rv = select(n, &readfds, &writefds, NULL, NULL);
 
but that led to the printing of an entire empty array on the server console when the server initialised in addition to the fact that communication between the server and client was not proper.
removing "&writefds" did remove the redundant data but the improper communication issue still persists...
So I would be really thankful if somebody helped me out...
Posted 19-Apr-13 21:50pm
Edited 19-Apr-13 23:23pm
(no name)131.6K
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,192
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 20 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100