Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

socket: connection reset by server before client gets response

19 views
Skip to first unread message

ahlongxp

unread,
Jul 7, 2007, 10:51:47 AM7/7/07
to
Hi, everyone,

I'm implementing a simple client/server protocol.

Now I've got a situation:
client will send server command,header paires and optionally body.
server checks headers and decides whether to accept(read) the body.
if server decided to throw(dump) the request's body, it'll send back a
response message, such as "resource already exists" and close the
connection.
the problem is, client will never get the response but a "peer reset"
exception.


any comments or help will be appreciated.

--
ahlongxp

Software College,Northeastern University,China
ahlo...@gmail.com
http://www.herofit.cn

ahlongxp

unread,
Jul 7, 2007, 12:58:34 PM7/7/07
to
me again.

"Connection reset by peer" happens about one in fifth.
I'm using python 2.5.1 and ubuntu 7.04.

--
ahlongxp

Software College,Northeastern University,China
ahlon...@gmail.comhttp://www.herofit.cn


Irmen de Jong

unread,
Jul 7, 2007, 8:05:20 PM7/7/07
to

Post the code.

Without it we can only help when our magic crystal balls are back from service.


==irmen

ahlongxp

unread,
Jul 8, 2007, 12:53:59 AM7/8/07
to
> Post the code.
ok.
here is the code:

# Echo server program
import socket

HOST = '' # Symbolic name meaning the local host
PORT = 50007 # Arbitrary non-privileged port
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)
conn, addr = s.accept()
print 'Connected by', addr
conn.settimeout(1)
toread = 99
retrytime = 0
reads = 0
while reads < toread and retrytime < 10:
try:
data = conn.recv(min(32,toread-reads))
if not data: continue
print data
reads += len(data)
except:
retrytime += 1
print "timeout %d" % retrytime
continue
if reads == toread:
conn.send("OK")
else:
conn.send("NOT OK")
conn.close()

****************I'm the separate
line*********************************************

# Echo client program
import socket

HOST = 'localhost' # The remote host
PORT = 50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
for i in range(12):
print "time %d" % i
s.send('0123456789')
#data = s.recv(1024)
#print "data %d" %i, data
#s.shutdown(socket.SHUT_WR)#no more write
data=s.recv(1024)
s.close()
print 'Received', repr(data)

client is supposed to get the response, either "OK" or "NOT OK".
but the fact is, client gets "Connection reset by peer" (as shown
below) about one in fifth.
----------------------------------
Traceback (most recent call last):
File "c.py", line 10, in <module>
s.send('0123456789')
socket.error: (104, 'Connection reset by peer')
----------------------------------

anyway, server is doing well all the time.

any comments on the design or implementation will be greatly
appreciated.

--
ahlongxp

Software College,Northeastern University,China
ahlo...@gmail.com
http://www.herofit.cn

Frank Swarbrick

unread,
Jul 8, 2007, 2:59:55 AM7/8/07
to

So, umm, what exactly are you trying to accomplish?
Here are the results I'm getting:

Server:
Connected by ('127.0.0.1', 53536)
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
0123456789
012345678

Client:
time 0
time 1
time 2
time 3
time 4
time 5
time 6
time 7
time 8
time 9
time 10
time 11


Traceback (most recent call last):

File "echo_c.py", line 10, in <module>
s.send('0123456789')
socket.error: (32, 'Broken pipe')

It looks like what is happening is the server only accepts 99 bytes. It
then does the send and the close.

The client wants to send 120 bytes, 10 bytes at a time. By the time is
does the 12th send the server has already finished, closing its socket
and exiting. So when the client attempts send #12 the socket is already
closed. Thus the error.

I'm not sure why you are getting the 'connection reset' instead of
'broken pipe'. Probably different OS. (I'm using Mac OS X 10.4.10.)

Anyway, I changed your code slightly, wrapping the send in a try/except
block like this:
try:
s.send('0123456789')
except socket.error ,e:
print "Socket Error:", e
break

Now here are my results for the client:
time 0
time 1
time 2
time 3
time 4
time 5
time 6
time 7
time 8
time 9
time 10
time 11
error: (32, 'Broken pipe')
Received 'OK'

The way you had it the program crashed before it could do the receive.

Frank

ahlongxp

unread,
Jul 8, 2007, 4:20:38 AM7/8/07
to

> So, umm, what exactly are you trying to accomplish?

> It looks like what is happening is the server only accepts 99 bytes. It


> then does the send and the close.

yes. What I want is that, server sends response to client and closes
connection when it feels recieving enough information, and make sure
at the same time ,client will definitely get the response from server.

> The client wants to send 120 bytes, 10 bytes at a time. By the time is
> does the 12th send the server has already finished, closing its socket
> and exiting. So when the client attempts send #12 the socket is already
> closed. Thus the error.
>

> I'm not sure why you are getting the 'connection reset' instead of
> 'broken pipe'. Probably different OS. (I'm using Mac OS X 10.4.10.)
>

as I saied before, running results will varies. And most of the time
they are working quite well. But that's not enough.


> Anyway, I changed your code slightly, wrapping the send in a try/except
> block like this:
> try:
> s.send('0123456789')
> except socket.error ,e:
> print "Socket Error:", e
> break
>
> Now here are my results for the client:
> time 0
> time 1
> time 2
> time 3
> time 4
> time 5
> time 6
> time 7
> time 8
> time 9
> time 10
> time 11
> error: (32, 'Broken pipe')
> Received 'OK'

This really helped.
Now I know the client will still get the response even under
'Connection reset by peer' or 'Broken pipe'.


> The way you had it the program crashed before it could do the receive.
>
> Frank

Frank, thanks a lot.

Hendrik van Rooyen

unread,
Jul 8, 2007, 4:30:47 AM7/8/07
to ahlongxp, pytho...@python.org
"ahlongxp" <ah...p@gmail.com> wrote:

> me again.
>
> "Connection reset by peer" happens about one in fifth.
> I'm using python 2.5.1 and ubuntu 7.04.
>

Try to wait a while in the server thread, after sending the
message before closing the connection, to give the message
time to get transmitted.

time.sleep(0.5) should do it...

- Hendrik

ahlongxp

unread,
Jul 8, 2007, 9:08:06 AM7/8/07
to

> Try to wait a while in the server thread, after sending the
> message before closing the connection, to give the message
> time to get transmitted.
>
> time.sleep(0.5) should do it...
>
> - Hendrik

OMG, it works.
I can't believe the problem can be solved so easily.

Thanks very much.

Hendrik van Rooyen

unread,
Jul 9, 2007, 1:59:57 AM7/9/07
to pytho...@python.org
"ahlongxp" <ah...p@gmail.com> wrote:

It's a pleasure.

Sometimes I think that all would be programmers should be
forced to write a "Hello World" to transmit out of a serial port
in assembler on hardware that carries no OS - just to teach
them about interrupts and time.

I would require them to hand assemble the code too, to make
them appreciate what a compiler or an interpreter does.

And if you fail the test, you get taken out and fed to the
sacred crocodiles.

- Hendrik

--
For training with a bite, enrol now in Heavy Henry's
Wholesome Hackadamy for Precocious Programmers

ahlongxp

unread,
Jul 9, 2007, 3:21:29 AM7/9/07
to
> It's a pleasure.
>
> Sometimes I think that all would be programmers should be
> forced to write a "Hello World" to transmit out of a serial port
> in assembler on hardware that carries no OS - just to teach
> them about interrupts and time.
>
> I would require them to hand assemble the code too, to make
> them appreciate what a compiler or an interpreter does.
>
Then there will be more crocodiles than programmers.

> And if you fail the test, you get taken out and fed to the
> sacred crocodiles.
>
> - Hendrik
>
> --
> For training with a bite, enrol now in Heavy Henry's
> Wholesome Hackadamy for Precocious Programmers

I feel a little embarrassed now.

Adriano Varoli Piazza

unread,
Jul 9, 2007, 4:30:44 AM7/9/07
to
Hendrik van Rooyen wrote:
> Sometimes I think that all would be programmers should be
> forced to write a "Hello World" to transmit out of a serial port
> in assembler on hardware that carries no OS - just to teach
> them about interrupts and time.
>
> I would require them to hand assemble the code too, to make
> them appreciate what a compiler or an interpreter does.
>
> And if you fail the test, you get taken out and fed to the
> sacred crocodiles.
>
> - Hendrik
>
> --
> For training with a bite, enrol now in Heavy Henry's
> Wholesome Hackadamy for Precocious Programmers

Gives a whole new meaning to chomp(), byte, nybble, and more :)
I wholeheartedly endorse this effort. I'm sick of reading about
students from WTF University (check thedailywtf.com if you don't know,
but beware. There be dragons).

--
Adriano
Once bitten, one arm less to code snafus with.

ahlongxp

unread,
Jul 9, 2007, 5:16:11 AM7/9/07
to
On Jul 9, 4:30 pm, Adriano Varoli Piazza <mora...@gmail.com> wrote:

>
> Gives a whole new meaning to chomp(), byte, nybble, and more :)
> I wholeheartedly endorse this effort. I'm sick of reading about
> students from WTF University (check thedailywtf.com if you don't know,
> but beware. There be dragons).
>
> --
> Adriano
> Once bitten, one arm less to code snafus with.

I feel officially offended.

Adriano Varoli Piazza

unread,
Jul 9, 2007, 7:03:29 AM7/9/07
to
ahlongxp wrote:
> I feel officially offended.

I didn't intend to offend you, I was joking. I apologise in any case.
There's a few things to be said, though:

As per your message in another thread, it isn't that you don't express
yourself clearly in English, but that you were too quick to claim a
standard function was buggy without first thinking if your own code
could be buggy instead. That presumption isn't related to the language.

Second, if you do say that you aren't comfortable in English, try to
assume that people aren't trying to insult you by default. I was
speaking generallyin my message.

Third, this is usenet. Although probably not in this newsgroup, if you
continue to make the assumptions I and others pointed out, you will
receive this kind of answer. Perhaps with much more insult than right
now. So it is a great idea to grow a thick skin.

--
Saludos
Adriano

ahlongxp

unread,
Jul 9, 2007, 8:27:27 AM7/9/07
to
You don't have to apologise. I was joking too.

But I know I have to change my way of thinking and questioning.
I'll consider more carefully before speaking.
I hope I can talk like you very soon.

Thanks.

Hendrik van Rooyen

unread,
Jul 10, 2007, 2:01:38 AM7/10/07
to pytho...@python.org

"ahlongxp" <ahlongxp@....l.com> wrote:

> I feel a little embarrassed now.

There is nothing to be embarrassed about.
Experience is a thing that is hard won.

As someone once said:

"no Pain, no Gain"

- Hendrik

0 new messages