Thread: socket programming issue

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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