Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7977263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:08:49+00:00 2026-06-04T09:08:49+00:00

I have written the following code in the server side for a chat client

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:08:50+00:00Added an answer on June 4, 2026 at 9:08 am

    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
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
I have written the following code to place the image path into sql server
I have written a chat server that works with telnet. I wrote a client
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in
I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another
I have written the following code. But it is removing only &nbsp; not <br>
i have written the following code: As you can see there is a for
I have written the following code in Perl. I want to iterate through a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.