CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to detect TCP Disconnect?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
starkeeper



Joined: 06 Apr 2006
Posts: 9

View user's profile Send private message

How to detect TCP Disconnect?
PostPosted: Thu Apr 06, 2006 3:50 am     Reply with quote

Hi,
I'm using the CCS/microchip TCP stack. I'm running a tcp-server on my microchip and I have a short test program on my PC.

The tool on my PC can connect to the PIC and send data, that works fine. But what if the client disconnects. Now the PIC should detect that the client has disconnected, but at the moment the PIC does not recognize any change. The socket state remains in TCP_EST.

How can I find out if the client is still connected? The Socket should disconnect and change to TCP_listen.

greets starkeeper
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Thu Apr 06, 2006 6:53 am     Reply with quote

Search the web for the TCP rfc and specs. There is a three part handshake. Now if the virtual circuit is dropped as opposed to logically terminated then since TCP is inherentently asynchronous you will most likely need a timeout to be triggered if a packet isn't received within a specified time.
starkeeper



Joined: 06 Apr 2006
Posts: 9

View user's profile Send private message

PostPosted: Thu Apr 06, 2006 8:06 am     Reply with quote

Douglas Kennedy wrote:
Search the web for the TCP rfc and specs. There is a three part handshake. Now if the virtual circuit is dropped as opposed to logically terminated then since TCP is inherentently asynchronous you will most likely need a timeout to be triggered if a packet isn't received within a specified time.


OK, I just was not sure if the Stack already does this for me or not. Thanks for your advice.
hanxuallan
Guest







PostPosted: Thu Apr 06, 2006 7:57 pm     Reply with quote

This is PC software job, you do not need consider.
What I do is that:

PC send a specific char to PIC per min (or second).
PIC receive the char and means PC is alive.
captwiggum
Guest







PostPosted: Mon Apr 27, 2009 5:20 pm     Reply with quote

Code:
"""
tcp_disconnect.py
Echo network data test program in python. This program easily translates to C & Java.

By TCP rules, the only way for a server program to know if a client has disconnected,
is to try to read from the socket. Specifically, if select() says there is data, but
recv() returns 0 bytes of data, then this implies the client has disconnected.

But a server program might want to confirm that a tcp client is still connected without
reading data. For example, before it performs some task or sends data to the client.
This program will demonstrate how to detect a TCP client disconnect without reading data.

The method to do this:
1) select on socket as poll (no wait)
2) if no recv data waiting, then client still connected
3) if recv data waiting, the read one char using PEEK flag
4) if PEEK data len=0, then client has disconnected, otherwise its connected.
Note, the peek flag will read data without removing it from tcp queue.

To see it in action: 0) run this program on one computer 1) from another computer,
connect via telnet port 12345, 2) type a line of data 3) wait to see it echo,
4) type another line, 5) disconnect quickly, 6) watch the program will detect the
disconnect and exit.

I hope this is helpful to someone. John Masinter, 17-Dec-2008.
"""

import socket
import time
import select

HOST = ''       # all local interfaces
PORT = 12345    # port to listen

# listen for new TCP connections
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
# accept new conneciton
conn, addr = s.accept()
print 'Connected by', addr
# loop reading/echoing, until client disconnects
try:
    conn.send("Send me data, and I will echo it back after a short delay.\n")
    while 1:
        data = conn.recv(1024)                          # recv all data queued
        if not data: break                              # client disconnected
        time.sleep(3)                                   # simulate time consuming work
        # below will detect if client disconnects during sleep
        r, w, e = select.select([conn], [], [], 0)      # more data waiting?
        print "select: r=%s w=%s e=%s" % (r,w,e)        # debug output to command line
        if r:                                           # yes, data avail to read.
            t = conn.recv(1024, socket.MSG_PEEK)        # read without remove from queue
            print "peek: len=%d, data=%s" % (len(t),t)  # debug output
            if len(t)==0:                               # length of data peeked 0?
                print "Client disconnected."            # client disconnected
                break                                   # quit program
        conn.send("-->"+data)                           # echo only if still connected
finally:
    conn.close()

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group