Thread: program doesn't print data from socket

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    62

    program doesn't print data from socket

    I created a basic socket server, which listensing for incoming udp data. When I run the netcat program, I get a response:

    Code:
    $ nc -luv 1732
    Connection from 10.50.11.12 port 1732 [udp/*] accepted
    (?@??8?? ??.?n?5
    (?@??8?? ??.?n?5|?>)
    (?@??8?? ??.?n?5|?>)
    ^C              |?>)

    But with my c program it doesn't give the response. It should say something like "here is the message: " and then give a message.

    Code:
    #include <stdio.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main( int argc, char *argv[] )
    {
        int sockfd, newsockfd, portno, clilen;
        struct sockaddr_in serv_addr, cli_addr;
        char buffer[256];
        int n;
    
    
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
        {
            perror("ERROR opening socket");
            exit(1);
        }
    
    
        bzero((char *) &serv_addr, sizeof(serv_addr));
        portno = 1732;
    
    
        serv_addr.sin_family = AF_INET;
        serv_addr.sin_addr.s_addr = INADDR_ANY;
        serv_addr.sin_port = htons(portno);
    
    
        if (bind(sockfd, (struct sockaddr *) &serv_addr,
                              sizeof(serv_addr)) < 0)
        {
             perror("ERROR on binding");
             exit(1);
        }
    
    
        listen(sockfd,5);
        clilen = sizeof(cli_addr);
    
    
        newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, 
                                    &clilen);
    
    
        if (newsockfd < 0) 
        {
            perror("ERROR on accept");
            exit(1);
        }
    
    
        bzero(buffer,256);
        n = read( newsockfd,buffer,255 );
        if (n < 0)
        {
            perror("ERROR reading from socket");
            exit(1);
        }
        printf("Here is the message: %s\n",buffer);
    }
    Any idea what I might be doing wrong?

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    The 2nd argument of socket() should be SOCK_DGRAM if you are using UDP.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,908
    I don't have time to give this a thorough going-over, but:

    It would help if you provided the client you're using to help us test, and if you showed us exactly how you ran the server and client.

    Compile at maximum warning level (-Wall option for GCC):
    Code:
    $ make foo
    gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘main’:
    foo.c:47:24: warning: pointer targets in passing argument 3 of ‘accept’ differ in signedness [-Wpointer-sign]
    /usr/include/x86_64-linux-gnu/sys/socket.h:214:12: note: expected ‘socklen_t * restrict’ but argument is of type ‘int *’
    foo.c:58:5: warning: implicit declaration of function ‘read’ [-Wimplicit-function-declaration]
    You don't check if listen() fails. Check the return value of all functions, and print a useful error message and exit/recover on failure.

    Also, you open a SOCK_STREAM (TCP) socket, but you netcat a UDP socket. Again, providing the client and how you ran everything should help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doesn't print what it's supposed to
    By fane in forum C Programming
    Replies: 5
    Last Post: 02-01-2014, 07:31 PM
  2. receive data from c client socket program in linux
    By why_so_serious in forum C# Programming
    Replies: 0
    Last Post: 11-05-2013, 09:27 PM
  3. Why doesn't this print?
    By Matt Holden in forum C Programming
    Replies: 1
    Last Post: 01-21-2013, 03:04 PM
  4. Socket declaration just doesn't work
    By sean in forum C# Programming
    Replies: 3
    Last Post: 06-19-2004, 12:10 PM
  5. Doesn't print out?
    By cockroach007 in forum C Programming
    Replies: 8
    Last Post: 11-17-2001, 08:30 AM