Question

I've set up this simple script on my local machine:

#!/usr/bin/python

import socket
from datetime import datetime

print "Connecting to test server"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("mydomain.com", 9090))

start = datetime.now()

totalbytes = 0

try:
    while True:
        data = s.recv(512)
        totalbytes += len(data)
        print "Got %d bytes of data" % (len(data))
except Exception as e: 
    print "ERROR", e
    pass

end = datetime.now()

print "Lasted for", end - start, "with a total of", totalbytes/1000/1000, "megabytes"

And this script on my local server (running on the same network, but note that I am going through the external ip-address here)

#!/usr/bin/python

import socket
from datetime import datetime

print "Listening for test client"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("192.168.2.100", 9090))
s.listen(5)

start = datetime.now()

try:
    while True:
        clientsocket, address = s.accept()

        # Start sending infinite data until I can't
        while True:
            clientsocket.send("*" * 511 + "+")
except:
    pass

end = datetime.now()

print "This lasted", end - start

On to my problem: The connection is dropped after a couple of minutes. For example, the last time a run this script the server just stopped and reported: "This lasted 0:02:27.517715", the time before that the client could receive data for about 7 minutes before it dropped the connection. Now, I haven't done a lot of network programming in my days, so I don't know if it is reasonable to to expect that they stay connected a lot longer than that? How long can one expect an active TCP connection to live? How common are disconnections like these?

I have conducted these tests in order to test my own local network, as I have been having problems with MSN logging out once an hour, downloads stopping after a couple of minutes and other strange behavior. My question is not how to fix this, but just if the results of my tests are reasonable, or point to problems with my network?

Was it helpful?

Solution

A TCP connection can stay up for years* and pass an unlimited amount of data. What are the errors you are getting from the client and the server when they are disconnected? This would be the first step toward tracking down the problem.

[*] I'd say indefinitely in theory but I've only personally observed "years".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top