binish
binish

Reputation: 85

how to check whether a connection is established in the server side in C

I have written the following code in the server side for a chat client using C. However, inside the while loop i need to check whether the client has exited the program, that is whether the connection with the client is lost. Please tell me how I can write such a code.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <time.h>
int main()
{
int sock, connected, bytes_recieved , true = 1, pid;  
char send_data [1024] , recv_data[1024];     
struct sockaddr_in server_addr,client_addr;    
int sin_size;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
    perror("Socket");
    exit(1);
}
if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1)
{
    perror("Setsockopt");
    exit(1);
}
server_addr.sin_family = AF_INET;         
server_addr.sin_port = htons(3128);     
server_addr.sin_addr.s_addr = INADDR_ANY; 
bzero(&(server_addr.sin_zero),8); 
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
    perror("Unable to bind");
    exit(1);
}
if (listen(sock, 5) == -1)
{
    perror("Listen");
    exit(1);
}
printf("\nTCPServer Waiting for client on port 3128");
fflush(stdout);
FILE *log;
log = fopen("time.log", "a");
time_t t;
while(1)
{  
    sin_size = sizeof(struct sockaddr_in);
    connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
    printf("\n I got a connection from (%s , %d)", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
    fflush(stdout);           
    int pid=fork();
    while(1)
    {       
        if(pid == 0)
        {
            usleep(1000000);
            time(&t);
            send(connected, ctime(&t),30, 0);
        }
        else
        {
            bytes_recieved = recv(connected,recv_data,1024,0);
            recv_data[bytes_recieved] = '\0';
            fputs(recv_data, log);
            fputs("Time: ", log);
            time(&t);
            fputs(ctime(&t), log);
        }
        fflush(log);
    }   
    }   
fclose(log);
close(sock);
return 0;
} 

Please help.

Now I modified the code like this:

while(1)
{       
    if(pid == 0)
    {
        usleep(1000000); 
        time(&t);
        send(connected, ctime(&t),30, 0);
    }
    else
    {
        bytes_recieved = recv(connected,recv_data,1024,0);
        recv_data[bytes_recieved] = '\0';
        fputs(recv_data, log);
        fputs("Time: ", log);
        time(&t);
        fputs(ctime(&t), log);
    }
    fflush(stdout);
    fflush(log);
    fclose(log);
    if(bytes_recieved == 0 || bytes_recieved == -1)
    {
        close(sock); goto connct;}
    }   
}
close(sock);
return 0;

On compiling, it goes fine, but on running, it gives the following error output:

*** glibc detected *** ./server: double free or corruption (!prev): 0x09936008 ***
======= Backtrace: =========
/lib/i686/cmov/libc.so.6(+0x6b381)[0xb7659381]
/lib/i686/cmov/libc.so.6(+0x6cbd8)[0xb765abd8]
/lib/i686/cmov/libc.so.6(cfree+0x6d)[0xb765dcbd]
/lib/i686/cmov/libc.so.6(fclose+0x14a)[0xb764982a]
./server[0x8048c21]
/lib/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb7604ca6]
./server[0x8048861]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 133298     /home/harikrishnan/server/server
08049000-0804a000 rw-p 00000000 08:01 133298     /home/harikrishnan/server/server
09936000-09957000 rw-p 00000000 00:00 0          [heap]
b7400000-b7421000 rw-p 00000000 00:00 0 
b7421000-b7500000 ---p 00000000 00:00 0 
b75bf000-b75dc000 r-xp 00000000 08:01 3653635    /lib/libgcc_s.so.1
b75dc000-b75dd000 rw-p 0001c000 08:01 3653635    /lib/libgcc_s.so.1
b75ed000-b75ee000 rw-p 00000000 00:00 0 
b75ee000-b772e000 r-xp 00000000 08:01 3670051    /lib/i686/cmov/libc-2.11.3.so
b772e000-b772f000 ---p 00140000 08:01 3670051    /lib/i686/cmov/libc-2.11.3.so
b772f000-b7731000 r--p 00140000 08:01 3670051    /lib/i686/cmov/libc-2.11.3.so
b7731000-b7732000 rw-p 00142000 08:01 3670051    /lib/i686/cmov/libc-2.11.3.so
b7732000-b7735000 rw-p 00000000 00:00 0 
b7744000-b7747000 rw-p 00000000 00:00 0 
b7747000-b7748000 r-xp 00000000 00:00 0          [vdso]
b7748000-b7763000 r-xp 00000000 08:01 3654097    /lib/ld-2.11.3.so
b7763000-b7764000 r--p 0001b000 08:01 3654097    /lib/ld-2.11.3.so
b7764000-b7765000 rw-p 0001c000 08:01 3654097    /lib/ld-2.11.3.so
bfd94000-bfda9000 rw-p 00000000 00:00 0          [stack]
*** glibc detected *** ./server: double free or corruption (!prev): 0x09936008 ***
======= Backtrace: =========
/lib/i686/cmov/libc.so.6(+0x6b381)[0xb7659381]
/lib/i686/cmov/libc.so.6(+0x6cbd8)[0xb765abd8]
/lib/i686/cmov/libc.so.6(cfree+0x6d)[0xb765dcbd]
/lib/i686/cmov/libc.so.6(fclose+0x14a)[0xb764982a]
./server[0x8048c21]
/lib/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb7604ca6]
./server[0x8048861]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:01 133298     /home/harikrishnan/server/server
08049000-0804a000 rw-p 00000000 08:01 133298     /home/harikrishnan/server/server
09936000-09957000 rw-p 00000000 00:00 0          [heap]
b7400000-b7421000 rw-p 00000000 00:00 0 
b7421000-b7500000 ---p 00000000 00:00 0 
b75bf000-b75dc000 r-xp 00000000 08:01 3653635    /lib/libgcc_s.so.1
b75dc000-b75dd000 rw-p 0001c000 08:01 3653635    /lib/libgcc_s.so.1
b75ed000-b75ee000 rw-p 00000000 00:00 0 
b75ee000-b772e000 r-xp 00000000 08:01 3670051    /lib/i686/cmov/libc-2.11.3.so
b772e000-b772f000 ---p 00140000 08:01 3670051    /lib/i686/cmov/libc-2.11.3.so
b772f000-b7731000 r--p 00140000 08:01 3670051    /lib/i686/cmov/libc-2.11.3.so
b7731000-b7732000 rw-p 00142000 08:01 3670051    /lib/i686/cmov/libc-2.11.3.so
b7732000-b7735000 rw-p 00000000 00:00 0 
b7744000-b7747000 rw-p 00000000 00:00 0 
b7747000-b7748000 r-xp 00000000 00:00 0          [vdso]
b7748000-b7763000 r-xp 00000000 08:01 3654097    /lib/ld-2.11.3.so
b7763000-b7764000 r--p 0001b000 08:01 3654097    /lib/ld-2.11.3.so
b7764000-b7765000 rw-p 0001c000 08:01 3654097    /lib/ld-2.11.3.so
bfd94000-bfda9000 rw-p 00000000 00:00 0          [stack]
Aborted

Any idea what the actual problem is?

Upvotes: 2

Views: 721

Answers (3)

Harikrishnan
Harikrishnan

Reputation: 8065

A small suggestion in your case, instead of giving an 'if' condition for checking the connection, do the same inside the 'while' that is instead of 'while(1)', give the condition.

Upvotes: 0

dwalter
dwalter

Reputation: 7468

recv() will return 0 as soon as the client closed the connection. -1 will be returned if there was any error while trying to get data from the socket.

see man page for more information.

so change your code to

   int conn_closed = 0;

   bytes_recieved = recv(connected,recv_data,1024,0);
   switch (bytes_recieved) {
   case 0:
         /* connection closed by client */
         close(connected);
         conn_closed = 1;
         break;
   case -1:
         /* error on socket */
         /* TODO: insert error handling code */
         if (errno == EAGAIN || errno == EINTR) {
            /* safe to retry */
            conn_closed = 0;
         } else {
            close(connected)
            conn_closed = 1;
         }
         break;
   default:
        recv_data[bytes_recieved] = '\0';
        fputs(recv_data, log);
        fputs("Time: ", log);
        time(&t);
        fputs(ctime(&t), log);
        conn_closed = 0;
        break;
   }
   /* break out of the loop if conn_closed */
   if (conn_closed)
       break;

and do not forget to check the return-value of send(), to see if an error occurred while sending data.

UPDATE: added error handling code for EAGAIN and EINTR.

2nd UPDATE:

For completeness, here is the code you should add for sending

if (send(connected, ctime(&t),30, 0) == -1)
{
    if (errno == EAGAIN || errno == EINTR) {
       /* retry to send */
    }
    close(connected);
    break;
}

Upvotes: 3

John Smith
John Smith

Reputation: 1172

if send return SOCKET_ERROR that means that the socket is closed or the connection is lose

so just do

if (send(connected, ctime(&t),30, 0) == SOCKET_ERROR)
 {
   // blablabla
 }

Upvotes: 0

Related Questions