Socket error on non-blocking

This is a discussion on Socket error on non-blocking within the C Programming forums, part of the General Programming Boards category; Hi; I have a client/server but i keep getting an error when i run the program. When the socket is ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    58

    Socket error on non-blocking

    Hi;

    I have a client/server but i keep getting an error when i run the program. When the socket is blocking it works but non blocking it doesnt.

    Here is the server code
    Code:
    #include <stdio.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <fcntl.h>
    
    #define BUFFSIZE 256
    void Die(char *mess) { perror(mess); exit(1); }
    int main(int argc, char *argv[])
    {
    
            int serversock, clientsock;
            struct sockaddr_in echoserver, echoclient;
            char buffer[BUFFSIZE];
            int val;
    
            if (argc != 3) {
               fprintf(stderr, "USAGE: echoserver <ipaddress>  <port>\n");
               exit(1);
            }
    
            /* Create the TCP socket */
            if ((serversock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
                Die("Failed to create socket");
            }
    
            /*Unblock the socket*/
            fcntl(serversock, F_SETFL, O_NONBLOCK);
    
            /* Construct the server sockaddr_in structure */
            memset(&echoserver, 0, sizeof(echoserver));       /* Clear struct */
            echoserver.sin_family = AF_INET;                  /* Internet/IP */
            echoserver.sin_addr.s_addr = inet_addr(argv[1]);  /* Incoming addr */
            echoserver.sin_port = htons(atoi(argv[2]));       /* server port */
    
            /* Bind the server socket */
            if (bind(serversock, (struct sockaddr *) &echoserver,
                sizeof(echoserver)) < 0) {
                Die("Failed to bind the server socket");
            }
    
            /* Listen on the server socket */
            if (listen(serversock, 5) < 0) {
               Die("Failed to listen on server socket");
             }
    
            /* Run until cancelled */
            while (1) {
                  unsigned int clientlen = sizeof(echoclient);
                  /* Wait for client connection */
                  if ((clientsock =
                       accept(serversock, (struct sockaddr *) &echoclient,
                              &clientlen)) < 0) {
                    Die("Failed to accept client connection");
                  }
                  fprintf(stdout, "Client connected: %s\n",
                    inet_ntoa(echoclient.sin_addr));
    
        }
    }
    Client code is
    Code:
    #include <stdio.h>
    #include <sys/socket.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <signal.h>
    #include <sys/time.h>
    #include <fcntl.h>
    #include <sys/types.h>
    
    #define BUFFSIZE 265
    void Die(char *mess) { perror(mess); exit(1); }
    
    int main(int argc, char *argv[])
    {
            int sock;
            struct sockaddr_in echoserver;
            unsigned int echolen;
            if (argc != 3) {
                    fprintf(stderr, "USAGE: TCPecho <ip address>  <port>\n");
                    exit(1);
            }
    /* Create the TCP socket */
            if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
                    Die("Failed to create socket");
            }
    
            fcntl(sock, F_SETFL, O_NONBLOCK);
    
            /*Construct the server sockaddr_in structure */
            memset(&echoserver, 0, sizeof(echoserver));       /* Clear struct */
            echoserver.sin_family = AF_INET;                  /* Internet/IP */
            echoserver.sin_addr.s_addr = inet_addr(argv[1]);  /* IP address */
            echoserver.sin_port = htons(atoi(argv[2]));       /* server port */
    
            
            /* Establish connection */
            if (connect(sock,
               (struct sockaddr *) &echoserver,
                sizeof(echoserver)) < 0) {
                    Die("Failed to connect with server");
            }
    }
    Client error on command prompt is Failed to connect with server: Operation now in progress

    Server error is :
    Failed to accept client connection: Resource temporarily unavailable!!


    Any ideas?



    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by newbie30 View Post
    Hi;

    I have a client/server but i keep getting an error when i run the program. When the socket is blocking it works but non blocking it doesnt.
    I don't know if this is why, but I would set the non-block after the socket is set-up; that is, for the server, after bind() but before listen(); for the client, there is no point in using non-block until after you connect().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nonblocking socket...blocking?
    By zolom in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-13-2009, 12:06 PM
  2. EAGAIN with write on a non blocking socket
    By mynickmynick in forum Networking/Device Communication
    Replies: 0
    Last Post: 04-30-2009, 09:59 AM
  3. how to intilise a non blocking socket?
    By lolguy in forum Networking/Device Communication
    Replies: 7
    Last Post: 03-20-2009, 12:18 AM
  4. How to initialize a non blocking socket using only winsock library
    By *DEAD* in forum Networking/Device Communication
    Replies: 4
    Last Post: 01-18-2008, 06:03 AM
  5. Socket Blocking Trouble!
    By LearningMan in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2003, 09:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21