472,791 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,791 software developers and data experts.

Problem with socket

My problem is not really python specific but as I do my implementation
in python I hope someone here can help me.

I have two programs that talk through a socket. Here is the code :

<server>
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

sock.settimeout(5.0)

sock.bind(("", port)
# We only handle one connection
sock.listen(1)

while 1:
newsock, address = sock.accept()
handle(newsock, address)

def handle(sock, address) :
print "Connection from", address
dataReceived = newsock.recv(1024)
while 1:
try:
newsock.send(data)
except socket.timeout, err:
print "Connection timeout %s" % err
break
except socket.error, err:
print "Connection broken %s" % err
break

</server>

<client>
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5.0)
sock.connect((host, port))
sock.send("Gimme a piece of information, please\r\n")

while 1:
r, w, e = select.select([s], [], [], 1.0)
if r != []:
handle_connection(s)
def handle_connection(sock):
try:
response_data = sock.recv(1024)
except socket.timeout:
print "Socket timeout"
return
manage(response_data)

</client>

Ok I hope it's clear. My problem is that "select" only tests if there's
data on the socket, and not the state of the socket. I want to be able
to know if socket is "alive" or something like that. But I don't want
to make "send" in the client (stay passive). Then, if I know that my
socket is closed or my link down, I can try to reconnect periodically.

I would rewrite the while in the client like that :
<client>
while 1:
if not test_connect(s) :
reconnect(s)
# else continue normally
r, w, e = select.select([s], [], [], 1.0)
if r != []:
handle_connection(s)
</client

My env: python2.3, linux/win32.

Thanks in advance,

--
Thomas Herve <sorry for my english>
Jul 18 '05 #1
3 2592
In article <40***********************@news.free.fr>,
Thomas Herve <th****@neocles.com> wrote:
....
Ok I hope it's clear. My problem is that "select" only tests if there's
data on the socket, and not the state of the socket. I want to be able
to know if socket is "alive" or something like that. But I don't want
to make "send" in the client (stay passive). Then, if I know that my
socket is closed or my link down, I can try to reconnect periodically.


Try a SO_KEEPALIVE option on the socket (setsockopt). That
should (I would sort of expect anyway) raise an exception when
the keepalive negotiation fails. The reaction won't be immediate,
it may take hours to notice a dead connection.

Donn Cave, do**@u.washington.edu
Jul 18 '05 #2
On 2004-04-28, Thomas Hervé <th****@neocles.com> wrote:
Ok I hope it's clear. My problem is that "select" only tests
if there's data on the socket, and not the state of the
socket.
That's not true. If the socket is closed, it will return from
select as readable.
I want to be able to know if socket is "alive" or something
like that. But I don't want to make "send" in the client (stay
passive). Then, if I know that my socket is closed or my link
down, I can try to reconnect periodically.


You'll know if the socket is closed because select will mark it
as readable, and you'll get 0 bytes when you read it.

If you want to know if the network or the other host has "gone
away", set the SO_KEEPALIVE option on the socket. That will
generate an error if the link is down. IIRC, it takes 75
minutes (or is it 150?) to time out after the other end goes
away.

--
Grant Edwards grante Yow! I wonder if there's
at anything GOOD on tonight?
visi.com
Jul 18 '05 #3
Grant Edwards wrote:
On 2004-04-28, Thomas Hervé <th****@neocles.com> wrote:

Ok I hope it's clear. My problem is that "select" only tests
if there's data on the socket, and not the state of the
socket.
That's not true. If the socket is closed, it will return from
select as readable.


Exact.
I want to be able to know if socket is "alive" or something
like that. But I don't want to make "send" in the client (stay
passive). Then, if I know that my socket is closed or my link
down, I can try to reconnect periodically.


You'll know if the socket is closed because select will mark it
as readable, and you'll get 0 bytes when you read it.


It seems that it's not true : when I read it I have a "Connection reset
by peer" error.
If you want to know if the network or the other host has "gone
away", set the SO_KEEPALIVE option on the socket. That will
generate an error if the link is down. IIRC, it takes 75
minutes (or is it 150?) to time out after the other end goes
away.


Yes, I've seen this option, but the delay is a bit too large (2 hours ?
depending on OS it seems) and not configurable. So I made it by send
some hearbeat regulary, even if I didn't want to I haven't found another
way. And it's better for server side, from which I can make some recv
that make the break found earlier.

--
Thomas Herve
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gonçalo Rodrigues | last post by:
Hi, I have a problem with threads and sockets. I'll try to describe the problem in words with pseudo-code. I've been working on a few classes to make it easier to work with threads. This...
4
by: Sa¹o Zagoranski | last post by:
Hi! I'm writing a simple 3D First person shooter game. It is a multiplayer game, where all the players connect to one server.
4
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with...
0
by: Usman | last post by:
Hi I'm having problem with a scenarion where I have a server written in C# and client written in VC6++. Here is the server code that i'm using including the Callback function for handling...
2
by: Changhao | last post by:
Hi, friends, I am implementing a protocol on top of 'asyncore.dispatcher' to send streaming multimedia data over TCP socket. However, I found that the throughput of my current implementation is...
0
by: Ben | last post by:
I modified the logmonitor sdk example so it would work over a network. It works great when the client and server are running on the same PC and have administrator privileges. So I have two...
10
by: Clayton | last post by:
Hi all, I'm trying to develop a server that listens to incoming calls using the asycnhronous methods BeginAccept / EndAccept. I start the server (till this point it is ok) and few seconds later...
0
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is...
16
by: =?iso-8859-1?q?|-|e|=5F|=5F_B0=DD?= | last post by:
hi all! I got a problem. I declared a SOCKET var in my C program but when i compiled the program it displayed like *--------------------------------------------------------------* *'SOCKET':...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.