Thread: UDP Broadcast Client and Server

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    UDP Broadcast Client and Server

    Hi,
    I have determined that my UDP client is sending out more bytes than the server is receiving. For example, if I send "hello" via my client, my server only gets "h". Enclosed is my code. Any thoughts on why it's doing this?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdlib.h>
    #include <errno.h>
    
    /* Client.c */
    
    int main( int argc, char *argv[])
    {
       char buf[512];
       int clientToServer_socket;
       unsigned int fromLength;
       struct sockaddr_in Remote_Address, Server_Address;
       struct hostent *hostPointer;
       int message, checkCall;
       int counter1 = 0;
       int counter2 = 0;
       int broadcastOn = 1;
       int broadcastOff = 0;
       char *broadcastMessage;
    
       broadcastMessage = "Hello";
    
       printf("Message %s\n", broadcastMessage); 
    
       /*Create client socket*/ 
       clientToServer_socket=socket(AF_INET, SOCK_DGRAM, 0);
       if (clientToServer_socket==-1)
    	perror("Error: client socket not created");
    
       /*Fill in client's sockaddr_in */
       bzero(&Remote_Address, sizeof(Remote_Address));
       Remote_Address.sin_family=AF_INET;
       hostPointer=gethostbyname(argv[1]);
       memcpy((unsigned char * ) &Remote_Address.sin_addr, (unsigned char *) hostPointer -> h_addr, hostPointer -> h_length);
       Remote_Address.sin_port=htons(atoi(argv[2]));
    
       checkCall = setsockopt(clientToServer_socket, SOL_SOCKET, SO_BROADCAST, &broadcastOn, 4);
       if(checkCall == -1)
    	perror("Error: setsockopt call failed");
       
       Remote_Address.sin_addr.s_addr|=htonl(0x1ff);
    
       checkCall = setsockopt(clientToServer_socket, SOL_SOCKET, SO_BROADCAST, &broadcastOff, 4);
       if(checkCall == -1)
    	perror("Error: Second setsockopt call failed");
    
       int broadcastMessageLen = strlen(broadcastMessage) + 1; 
       
       printf("Message test %d\n", broadcastMessageLen);
      
         
       message = sendto(clientToServer_socket, broadcastMessage, broadcastMessageLen, 0, (struct sockaddr *) &Remote_Address, sizeof(Remote_Address));
       if (message ==-1)
    	perror("Error: sendto call failed");
       
       while(1)
       {
          fromLength = sizeof(Server_Address);
          message = recvfrom(clientToServer_socket, buf, 512, 0, (struct sockaddr *) &Server_Address, &fromLength);
          if (message ==-1)
    	 perror("Error: recvfrom call failed");
      
          printf("SERVER: read %d bytes from IP %s(%s)\n", message, inet_ntoa(Server_Address.sin_addr), buf);
       }
       close(clientToServer_socket);
    
    }


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdlib.h>
    #include <errno.h>
    
    /* Server.c */
    
    int main( int argc, char *argv[])
    {
       int server_socket;
       struct sockaddr_in server_address, client_address;
       char buf[512];
       unsigned int clientLength;
       int checkCall, message;
       
       /*Create socket */
       server_socket=socket(AF_INET, SOCK_DGRAM, 0);
       if(server_socket == -1)
            perror("Error: socket failed");
    
       bzero((char*) &server_address, sizeof(server_address));
    
       /*Fill in server's sockaddr_in*/
       server_address.sin_family=AF_INET;
       server_address.sin_addr.s_addr=htonl(INADDR_ANY);
       server_address.sin_port=htons(atoi(argv[1]));
    
       /*Bind server socket and listen for incoming clients*/
       checkCall = bind(server_socket, (struct sockaddr *) &server_address, sizeof(struct sockaddr));
       if(checkCall == -1)
            perror("Error: bind call failed");
    
       while(1)
       {
    	printf("SERVER: waiting for data from client\n");
    	
    	clientLength = sizeof(client_address);
    	message = recvfrom(server_socket, buf, strlen(buf)+1, 0,
    		  (struct sockaddr*) &client_address, &clientLength);
    	if(message == -1)
            perror("Error: recvfrom call failed");
    
    	printf("SERVER: read %d bytes from IP %s(%s)\n", message,
    		  inet_ntoa(client_address.sin_addr), buf);
    
    	if(!strcmp(buf,"quit"))
               break;
    
    	strcpy(buf,"ok");
    
    	message = sendto(server_socket, buf, strlen(buf)+1, 0,
    		  (struct sockaddr*) &client_address, sizeof(client_address));
    	if(message == -1)
            perror("Error: sendto call failed");		
    
    	printf("SERVER: send completed\n");
       }
       checkCall = close(server_socket);
       if(checkCall == -1)
            perror("Error: bind call failed");
    
    }
    Last edited by lamoseley; 10-18-2010 at 08:25 PM. Reason: clarification

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    11
    I see this thread is almost a week old, but since the OP may still have this problem and others may find this in a future search I hope no one blames me for necroposting here.

    I think your problem is in your server. You have:
    Code:
    	message = recvfrom(server_socket, buf, strlen(buf)+1, 0,
    		  (struct sockaddr*) &client_address, &clientLength);
    strlen() on an empty string (which actually may or may not be empty, since you never initialized 'buf') will return 0. So you're only receiving 0+1 bytes from the socket (hence the lone 'h' character). Try this instead:
    Code:
    	message = recvfrom(server_socket, buf, sizeof(buf), 0,
    		  (struct sockaddr*) &client_address, &clientLength);
    sizeof() on an array will "return" ("simplify to" really, as sizeof is an operator and not a function) the number of elements in it. But this only works if the array is statically allocated (as your 'buf' array is). If you declare your array as a pointer and dynamically allocate storage, sizeof() will simplify to the size of a pointer on your machine (usually 4 or 8 bytes).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. server client application - (i really need your help)
    By sarahnetworking in forum C Programming
    Replies: 3
    Last Post: 03-01-2008, 10:54 PM
  2. Using select() for client UDP
    By jazzman83 in forum C Programming
    Replies: 2
    Last Post: 04-03-2007, 05:31 AM
  3. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  4. Unicode vurses Non Unicode client server application with winsock2 query?
    By dp_76 in forum Networking/Device Communication
    Replies: 0
    Last Post: 05-16-2005, 07:26 AM
  5. UDP Server -> Displaying Client Information
    By Jedimark in forum Networking/Device Communication
    Replies: 6
    Last Post: 12-05-2003, 12:57 PM