Thread: C check if client is clsoed

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    9

    C check if client is clsoed

    Hello,

    I'm write a C program.

    How can i check if the client is closed?

    I'm write a TCP Server and Client

    Thanks for helping me

  2. #2
    Registered User
    Join Date
    Aug 2019
    Posts
    9
    I found out:

    just add:
    Code:
    if (recv(newSocket, file, 1024, 0))
    {
        
    }
    else
    {
        printf("disconnection from %s:%" PRIu16 "\n", inet_ntoa(newAddr.sin_addr), ntohs(newAddr.sin_port));
        close(newSocket);
        break;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    Or there is read the manual page.
    Code:
    RETURN VALUE
           These calls return the number of bytes received, or -1 if an error occurred.  In the event of an error, errno is set to indicate the error.
    
           When a stream socket peer has performed an orderly shutdown, the return value will be 0 (the traditional "end-of-file" return).
    
           Datagram sockets in various domains (e.g., the UNIX and Internet domains) permit zero-length datagrams.  When such a datagram is received, the return value is 0.
    
           The value 0 may also be returned if the requested number of bytes to receive from a stream socket was 0.
    -1 would also evaluate to true in your code.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 11-16-2018, 08:30 AM
  2. Check to check string for a series of characters
    By robi in forum C Programming
    Replies: 1
    Last Post: 02-28-2012, 05:42 PM
  3. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  4. Client - PLEASE HELP!
    By markpratt in forum C++ Programming
    Replies: 6
    Last Post: 12-15-2003, 12:08 AM
  5. udp client
    By rotis23 in forum Linux Programming
    Replies: 3
    Last Post: 11-08-2002, 08:09 PM

Tags for this Thread