Thread: Server socket closes after client disconnection

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    50

    Server socket closes after client disconnection

    Hi.

    I'm having this simple C code for a server and client. What server does is to send a message to the client when is connected. The thing is that when client disconnects server socket cannot accept new connection and program exits.

    SERVER CODE:
    Code:
      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 #include <sys/types.h>
      4 #include <sys/socket.h>
      5 #include <netdb.h>
      6 #include <netinet/in.h>
      7 #include <strings.h>
      8 #include <unistd.h>
      9 #include <string.h>
     10 
     11 char MESSAGE[] = "Welcome to Server";
     12 
     13 int main(){
     14     
     15     int server_socket , server_port = 4444, client_socket;
     16     int bind_status, listen_status;
     17     struct sockaddr_in server_address,client_address; 
     18     socklen_t addr_length = sizeof(client_address);
     19 
     20     // Create the socket
     21     server_socket = socket(AF_INET, SOCK_STREAM, 0);
     22 
     23     if(!server_socket){
     24             fprintf(stderr, "[mon] Couldn't create socket\n");
     25             exit(1); 
     26     }
     27     
     28     
     29     // Setup the address structure
     30     bzero(&server_address,sizeof(server_address));
     31     server_address.sin_family = AF_INET;
     32     server_address.sin_addr.s_addr = htonl(INADDR_ANY);
     33     server_address.sin_port = htons(server_port);
     34     
     35     // Bind the socket to the specified port and address
     36     bind_status = bind(server_socket , 
     37                     (struct sockaddr *)&server_address, 
     38                     sizeof(server_address));
     39 
     40     if(bind_status != 0){
     41             fprintf(stderr, "[mon] Failed to bind the socket\n");
     42             close(server_socket);
     43             exit(1);
     44     }
     45 
     46     // Make the socket listen for connections
     47     listen_status = listen(server_socket, 5);
     48 
     49     if(listen_status == -1){
     50             fprintf(stderr, "[mon] Failed to listen\n");
     51             close(server_socket);
     52             exit(1);
     53     }
     54     
     55     while(1){
     56        
     57         bzero(&client_address,sizeof(client_address));
     58         client_socket = accept(server_socket, 
     59                         (struct sockaddr *)&server_socket, 
     60                         &addr_length);
     61 
     62         if(client_socket == -1){
     63                 fprintf(stderr, "Cannot accept connections\n");
     64                 close(server_socket);
     65                 exit(1);
     66         }
     67         
     68         printf("Waiting for clients\n");
     69         
     70         // Send the message to the client
     71         write(client_socket, MESSAGE, strlen(MESSAGE));
     72 
     73         close(client_socket);
     74     }
     75 
     76     close(server_socket);
     77 
     78     return 0;        
     79 }
    CLIENT CODE

    Code:
    1 #include <stdlib.h>
      2 #include <stdio.h>
      3 #include <sys/types.h>
      4 #include <sys/socket.h>
      5 #include <netdb.h>
      6 #include <netinet/in.h>
      7 #include <strings.h>
      8 #include <unistd.h>
      9 #include <string.h>
     10 #include <arpa/inet.h>
     11 
     12 int main(){
     13 
     14     int client_socket , server_port = 4444;
     15     char buffer[256];
     16     struct sockaddr_in server_address ;
     17     socklen_t addr_length = sizeof(server_address);
     18     int connect_status, read_status;
     19 
     20     // Create clients socket
     21     client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     22 
     23     if(client_socket == -1){
     24         fprintf(stderr, "[mon] Could not create client socket\n");
     25         exit(1);
     26     }
     27 
     28     // Setup the address structure
     29     bzero(&server_address,sizeof(server_address));
     30     server_address.sin_family = AF_INET;
     31     server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
     32     server_address.sin_port = htons(server_port);
     33     
     34     // Connect to the server
     35     connect_status = connect(client_socket,
     36                     (struct sockaddr *)&server_address,
     37                     addr_length);
     38     
     39     if(connect_status != 0){
     40         fprintf(stderr, "[mon] Could not connect to the server\n");
     41         exit(1);
     42     }
     43 
     44     // Get the message from the server
     45     read_status = read(client_socket, buffer, sizeof(buffer));
     46 
     47     if((read_status > 0) && (read_status <= (int)sizeof(buffer)) ){
     48         // Null terminate the received buffer
     49         buffer[read_status] = 0;
     50         printf("%s\n",buffer);
     51     }else
     52         fprintf(stderr, "[mon] Nothing retrieved\n");
     53 
     54     close(client_socket);
     55 
     56     return 0;
     57 }
    When I'm running them, server's console returns :
    Waiting for clients
    Cannot accept connections
    Any idea why this is happening ?

    Thank you.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,410
    > 58 client_socket = accept(server_socket,
    > 59 (struct sockaddr *)&server_socket,
    You trash your socket with a badly placed &server_socket.

    Perhaps you meant &client_address.

    Next time you post code, please omit the line numbers at the start.
    The forum adds these for you for free.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    50
    You are right. That was the mistake. Thank you very much and sorry for the extra numbers. I ll take it in mind in the next post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to install socket server/client???
    By viendongshop in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-21-2015, 08:41 AM
  2. Socket ( server client Get pid )
    By Syd in forum C Programming
    Replies: 0
    Last Post: 02-15-2011, 08:00 AM
  3. Client closes before server receives
    By LuckY in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-06-2004, 04:08 PM
  4. Reset Socket w/o Disconnection :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 12:16 AM
  5. Code for Client/Server Socket
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2002, 09:30 AM

Tags for this Thread