Thread: UDP Sockets in C under Linux

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    6

    UDP Sockets in C under Linux

    Problem:
    The server receives the correct number and prints it, prints correctly the IP address but when it comes to port, it prints rubbish:
    What went wrong ?


    This is the client, it reads a number from standard input and sends it to the server.

    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    int main() {
    	int sock, n, nr;
    	socklen_t length;
    	struct sockaddr_in server, from;
    	sock = socket(AF_INET, SOCK_DGRAM, 0);
    	if(sock < 0) {
    		printf("Can not create socket in client!\n");
    	}
    	memset(&server, 0, sizeof(struct sockaddr_in));
    	server.sin_family = AF_INET;
    	server.sin_port = htons(5913);
    	server.sin_addr.s_addr = inet_addr("127.0.0.1");
    	length = sizeof(struct sockaddr_in);
    	scanf("%d",&nr);
    	nr = htonl(nr);
    	n = sendto(sock, &nr, sizeof(nr), 0, (const struct sockaddr *)&server, length);
    	if(n < 0) {
    		printf("Can not send from client");
    	}
    	close(sock);
    	return 0;
    }
    This is the server

    Code:
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #define N 100
    #define M 50
    #define K 100
    
    char IP[N][M];
    int Arr1[N][K];
    int Arr2[N][K];
    
    int main() {
    	int sock, n, nr;
    	socklen_t fromlen;
    	struct sockaddr_in server;
    	struct sockaddr_in from;
    
    	sock = socket(AF_INET, SOCK_DGRAM, 0);
    	if (sock < 0) 
     	 printf("Can not create socket in server\n");
    
    	memset(&server, 0, sizeof(struct sockaddr_in));
    	server.sin_family = AF_INET;
    	server.sin_port = htons(5913);
    	server.sin_addr.s_addr = INADDR_ANY;
    
    	if(bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
    	  printf("Can not bind in server!\n");
    	
    	fromlen = sizeof(struct sockaddr_in);
    	while(1) {
    		unsigned int gen;
    		n = recvfrom(sock, &nr, sizeof(nr), 0, (struct sockaddr *) &from, &fromlen);
    		if (n < 0) 
    		  printf("Error when receiving in server!\n");
    		nr = ntohl(nr);
    		gen = ntohs(from.sin_port);
    		printf("I have received %d from IP %s and port %u \n",nr,inet_ntoa(from.sin_addr), gen);
    	}
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Show us what you actually saw.

    Rubbish like "6423" perhaps?
    If it is, then you have a byte swapping problem somewhere.

    Perhaps memset your 'from' struct as well. At least you would always get a consistent answer if the function is not updating your struct.
    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
    Join Date
    Dec 2010
    Posts
    6
    Rubbish in range 50 000 - 60 000, even if I memset my from struct.

    I get different rubbish each time a new client connects to the server.

    What can be wrong ? It seems coded book-like and it's a very simple example !

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    nr = htonl(nr);
    Since you are pretending you have a long, why aren't you actually using a long:
    Code:
    int sock, n, nr;
    There's no guarantee that int and long are the same thing.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It would probably be a good idea to have some
    if ( n == sizeof(nr) )
    tests as well, to really check for success.
    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.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    6
    What you are saying makes sense. Now my code looks like this:

    Server
    Code:
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #define N 100
    #define M 50
    #define K 100
    
    int pars;
    
    int main() {
    	int sock, n, nr;
    	socklen_t fromlen;
    	struct sockaddr_in server;
    	struct sockaddr_in from;
    
    	sock = socket(AF_INET, SOCK_DGRAM, 0);
    	if (sock < 0) 
     	 printf("Can not create socket in server\n");
    
    	memset(&server, 0, sizeof(struct sockaddr_in));
    	server.sin_family = AF_INET;
    	server.sin_port = htons(5913);
    	server.sin_addr.s_addr = INADDR_ANY;
    
    	if(bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
    	  printf("Can not bind in server!\n");
    	memset(&from, 0, sizeof(struct sockaddr_in));
    	fromlen = sizeof(struct sockaddr_in);
    	while(1) {
    		int n, l1;
    		fflush(stdout);
    		n = recvfrom(sock, &l1, sizeof(l1), 0, (struct sockaddr*) &from, &fromlen);
    		if (n < 0) {	
    			printf("Can not receive in server!\n");
    		}
    		if (n == sizeof(nr)) printf("\n\nSuccess\n\n");
    		l1 = ntohl(l1);
    		printf("I have received %d from IP: %s, Port: %hu",l1,inet_ntoa(from.sin_addr), ntohs(from.sin_port));
    	}
    }
    Client

    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    #define N 100
    int sock, n, nr, nr2;
    socklen_t length;
    struct sockaddr_in server, from;
    
    void snd() {
    	int i,j;
    	length = sizeof(struct sockaddr_in);
    	nr = -1;
    	nr = htonl(nr);
    	n = sendto(sock, &nr, sizeof(nr), 0, (struct sockaddr *)&server, length);
    	if(n < 0) {
    	  printf("Can not send from client");
    	}
    	nr = -2;
    	nr = htonl(nr);
    	n = sendto(sock, &nr, sizeof(nr), 0, (struct sockaddr *)&server, length);
    	if(n < 0) {
    	  printf("Can not send from client");
    	}
    
    }
    void receive() {
    }
    int main() {
    	sock = socket(AF_INET, SOCK_DGRAM, 0);
    	if(sock < 0) {
    		printf("Can not create socket in client!\n");
    	}
    	srand(time(0));
    	memset(&server, 0, sizeof(struct sockaddr_in));
    	server.sin_family = AF_INET;
    	server.sin_port = htons(5913);
    	server.sin_addr.s_addr = inet_addr("127.0.0.1");
    	snd();
    	receive();	
    	close(sock);
    	return 0;
    }
    The server prints "Success" each time he gets a number(the number received is the number sent).
    I am using Ubuntu and compiling with gcc.

    The port is still rubbish(range 50 000 - 60 000) and isn't consistent across different clients connecting to the same port.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) you are not binding the socket in your client (thus no valid return address)
    2) you are setting the client IP to "localhost" which will only work on the same machine.
    3) you are asking both client and server to use the same port, which will fail on the same machine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (Where) learning linux tcp/ip sockets
    By codecaine_21 in forum Networking/Device Communication
    Replies: 4
    Last Post: 09-17-2010, 05:54 PM
  2. Udp Sockets: ports and bind
    By Phoenix_Rebirth in forum Linux Programming
    Replies: 5
    Last Post: 12-13-2009, 04:30 PM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Problems with UDP sockets
    By majoub in forum Networking/Device Communication
    Replies: 9
    Last Post: 04-30-2005, 12:25 AM