Thread: socket programming issue

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    socket programming issue

    hey guys! im struggling for the past couple of days with a socket problem.
    im trying to master the basics in order to proceed and integrate this mechanism in my program which is supposed to run on a server.

    basically the problem is weird and there's couple of parts to it that i quite cant understand, mainly in the client program.
    in my client code i run an infinite while loop with 1 send() and 1 recv() - straightforward and simple; server is supposed to recv() and send() back to the client a string.
    but here's what bothers me:
    first of all when running client - the server receives only the first message; after that the strings wont be sent..
    second - the while loop that is supposed to be infinite - terminates after 3 iterations..
    Code:
    int main (int argc, char **argv)
    {
            int sock;
            struct sockaddr_in cudaserver;
            char buffer[BUFFSIZE];
            unsigned int cudalen;
            int received = 0;
            msg temp_msg;
            
            if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
            {
                    Die("Failed to create socket");
            }
                     
            memset(&cudaserver, 0, sizeof(cudaserver));     
            cudaserver.sin_family = AF_INET;                  
            cudaserver.sin_addr.s_addr = inet_addr(argv[1]);            
            cudaserver.sin_port = htons(atoi(argv[2]));      
                  
            while (connect(sock,(struct sockaddr *) &cudaserver,sizeof(cudaserver)) < 0)
            {
                            Die("Failed to connect with server");
            }
    
            while (1)
            {
                    printf("input the word to send to server\n");
                    scanf("%s",&buffer);
                                 
                            cudalen = strlen(buffer);
                            if (send(sock, buffer, cudalen, 0) != cudalen)
                            {
                                    Die("Mismatch in number of sent bytes");
                            }
    
                            while (received > 0)
                            {
                                    int bytes = 0;
                                    if ((bytes = recv(sock, buffer, BUFFSIZE-1, 0)) < 1)
                                    {
                                            Die("Failed to receive bytes from server");
                                    }
                                    received += bytes;
                                    buffer[bytes] = '\0';
                                    fprintf(stdout, buffer);
                            }
            }
            close (sock);
            exit(0);
    }
    any help related to this issue would be appreciated

    P.S. btw, gdb says that "Program received signal SIGPIPE, Broken pipe." (if that helps anyhow)
    Last edited by santarus; 10-20-2011 at 01:12 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
                            if (send(sock, buffer, cudalen, 0) != cudalen)
                            {
                                    Die("Mismatch in number of sent bytes");
                            }
     
                            while (received > 0)
                            {
                                    int bytes = 0;
                                    if ((bytes = recv(sock, buffer, BUFFSIZE-1, 0)) < 1)
                                    {
                                            Die("Failed to receive bytes from server");
                                    }
    So which of those two are you getting?


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

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Quote Originally Posted by quzah View Post
    Code:
                            if (send(sock, buffer, cudalen, 0) != cudalen)
                            {
                                    Die("Mismatch in number of sent bytes");
                            }
     
                            while (received > 0)
                            {
                                    int bytes = 0;
                                    if ((bytes = recv(sock, buffer, BUFFSIZE-1, 0)) < 1)
                                    {
                                            Die("Failed to receive bytes from server");
                                    }
    So which of those two are you getting?


    Quzah.
    hey Quzah,

    that's the problem - my client wont throw any errors out - it compiles, runs and terminates without any. and that's why im a bit lost..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%s",&buffer);
    bar.c:47: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[300]’
    Drop the & when scanning into a char array.

    > fprintf(stdout, buffer);
    bar.c:64: warning: format not a string literal and no format arguments

    The second one is a killer - all the sender has to do is send a % character, and the receiver is screwed.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    thanks for your prompt reply Salem,

    but after tweaking the code - the problem remains.
    oh, and the receiver (server) seems to work correctly.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    while (received > 0)
    What's the value of received the first time?

    You should step through this in a debugger. Actually, it looks like you are?
    Last edited by rags_to_riches; 10-20-2011 at 04:59 AM.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    yeah im using gdb for debugging.
    the value of received is 0 when initialized (line 7)
    after "received" is supposed to get incremented by the amount of bytes it actually received (return of recv())

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by santarus View Post
    yeah im using gdb for debugging.
    the value of received is 0 when initialized (line 7)
    after "received" is supposed to get incremented by the amount of bytes it actually received (return of recv())
    if it's 0 on entry, the current loop never executes.
    Flip your while loop over to a do - while() construct, at least then you're guaranteed it will execute at least once.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    ahhh! finally it did reach the loop;
    it sent the string to the server, but failed to receive with error "Failed to receive bytes from server: Success"
    so i assume that it simply doesn't receive anything?!
    running in gdb in a sec!

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    im really confused why the client fails to receive, when my server is trying to send on the same socket with a live established connection.
    any ideas?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Good catch on the recv():
    Quote Originally Posted by santarus View Post
    Code:
    int main (int argc, char **argv)
    {
            ...
            int received = 0;
                            ...
                            while (received > 0)
                            {

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

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What's your current code look like? Note. if you read the man page on recv, particularly the Return Value section, you're treating receiving 0 bytes as an error, which it is not:

    These calls return the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.
    Emphasis mine.

    In the future, note that there is a Networking/Device Communication forum here where a question like this is more appropriate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue receiving data from socket (multicast)
    By JessH in forum C++ Programming
    Replies: 11
    Last Post: 08-31-2010, 09:01 AM
  2. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 03:46 AM
  3. Socket programming in C with socket.h
    By funzy in forum Networking/Device Communication
    Replies: 13
    Last Post: 08-29-2008, 04:12 AM
  4. which programming language should be used for socket programming?
    By albert_wong_bmw in forum Networking/Device Communication
    Replies: 8
    Last Post: 06-04-2004, 08:12 PM
  5. socket programming
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-07-2001, 09:45 AM