Thread: Server won't accept connections

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    2

    Server won't accept connections

    Hello everybody,

    I have a issue in my code. The application, I want to create should interact with the browser, therefore get and send request.
    The issue is, that the Server wont accapt the connection and I can't get data from the socket. Can someone help me, please?

    The relevant part of the code:

    Code:
    main(){
    
    struct sockaddr_in sockserv,sockclient;
    int socketfd,clientfd;
    socklen_t clientsocklen;
    char buff[BUFFERSIZE],buff2[BUFFERSIZE];
    int lenbuff;
    
    
    
    if((socketfd = socket(AF_INET,SOCK_STREAM,0))==-1){
            printf("Socket creation failed!\n");
        }
    printf("Socket Creation: %s\n",strerror(errno));
    
    bzero(&sockserv,sizeof(sockserv));
    
    sockserv.sin_family = AF_INET;
    sockserv.sin_addr.s_addr = INADDR_ANY;
    sockserv.sin_port = htons(8080);
    
    if(bind(socketfd,(struct sockaddr *)&sockserv,sizeof(sockserv))<0)
        {
        printf("Error binding the socket\n");
        }
    printf("Socket Bind: %s\n",strerror(errno));
    
    if(listen(socketfd,1) == -1){
    printf("Listening to Socket failed \n");
    }
    printf("Socket Listen: %s\n",strerror(errno));
    
    clientfd = accept(socketfd,(struct sockaddr*) &sockclient,&clientsocklen);
    if(clientfd  < 0){
        printf("Accepting Socket failed!\n");
    }
    
    
    ...
    
    
    }


    Best regards,
    Phil

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Looks like you forgot to initialize clientsocklen
    Code:
    clientsocklen = sizeof(sockclient);
    before calling accept
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    2
    Quote Originally Posted by vart View Post
    Looks like you forgot to initialize clientsocklen
    Code:
    clientsocklen = sizeof(sockclient);
    before calling accept

    Big thanks, that was right. I've spend several hours to find the problem. Now it's solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make it accept multi-client connections?
    By barramundi9 in forum C Programming
    Replies: 1
    Last Post: 03-28-2013, 04:00 AM
  2. Replies: 12
    Last Post: 02-10-2009, 02:14 PM
  3. Multiple Connections to a Server
    By niqo in forum Networking/Device Communication
    Replies: 9
    Last Post: 04-03-2008, 09:36 AM
  4. async Client/Server app, accept() stalls?
    By JaWiB in forum Networking/Device Communication
    Replies: 14
    Last Post: 01-31-2005, 05:59 PM
  5. Connections behind a proxy server
    By (TNT) in forum Windows Programming
    Replies: 1
    Last Post: 07-21-2002, 10:16 AM

Tags for this Thread