By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,115 Members | 894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

socket: connection reset by server before client gets response

P: n/a
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
ah******@gmail.com
http://www.herofit.cn

Jul 7 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a
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
Jul 7 '07 #2

P: n/a
ahlongxp wrote:
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

Post the code.

Without it we can only help when our magic crystal balls are back from service.
==irmen
Jul 8 '07 #3

P: n/a
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
ah******@gmail.com
http://www.herofit.cn

Jul 8 '07 #4

P: n/a
ahlongxp wrote:
>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.
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
Jul 8 '07 #5

P: n/a
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.

--
ahlongxp

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

Jul 8 '07 #6

P: n/a
"ahlongxp" <ah...p@gmail.comwrote:
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

Jul 8 '07 #7

P: n/a
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.

Jul 8 '07 #8

P: n/a
"ahlongxp" <ah...p@gmail.comwrote:

>
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.
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

Jul 9 '07 #9

P: n/a
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.
--
ahlongxp

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

Jul 9 '07 #10

P: n/a
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.

Jul 9 '07 #11

P: n/a
On Jul 9, 4:30 pm, Adriano Varoli Piazza <mora...@gmail.comwrote:
>
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.

Jul 9 '07 #12

P: n/a
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

Jul 9 '07 #13

P: n/a
On Jul 9, 7:03 pm, Adriano Varoli Piazza <mora...@gmail.comwrote:
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
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.

--
ahlongxp

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

Jul 9 '07 #14

P: n/a

"ahlongxp" <ahlongxp@....l.comwrote:
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

Jul 11 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.