Thread: implementing a non blocking udp socket by select()

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    1

    implementing a non blocking udp socket by select()

    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):

    Code:
    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:

    Code:
    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...

    Last edited by Salem; 04-23-2013 at 01:28 PM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps you should review your post, and make sure that code is correctly formatted.
    No one will bother to read it when it's all on one line.

    Hint: Use "paste as text" when you copy from your IDE.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    And although you've changed it now, we have a code tag for a reason. Firstly, it doesn't destroy any indentation, secondly it makes the text more easily readable if it is code. This is all in the FAQ's and the "new user" signup message, by the way.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Of course, having added the code tags, we find the indentation is so poor to make it still largely unreadable.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. select() chat client blocking.
    By netpumber in forum C Programming
    Replies: 19
    Last Post: 01-16-2012, 11:49 AM
  2. Socket error on non-blocking
    By newbie30 in forum C Programming
    Replies: 1
    Last Post: 09-01-2009, 07:09 AM
  3. Nonblocking socket...blocking?
    By zolom in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-13-2009, 12:06 PM
  4. how to intilise a non blocking socket?
    By lolguy in forum Networking/Device Communication
    Replies: 7
    Last Post: 03-20-2009, 12:18 AM
  5. blocking sockets and select
    By ecornips in forum C Programming
    Replies: 1
    Last Post: 11-11-2007, 04:33 PM

Tags for this Thread