472,353 Members | 1,258 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

socket: connection reset by server before client gets response

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
14 12516
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
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
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
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
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
"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
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
"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
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
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
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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine...
2
by: Rene Sørensen | last post by:
We are 4 students working on a assignment, that our teacher gave use, normally we do this is C++, but the 4 of us, use C# more often that C++ so…...
1
by: Chris Morse | last post by:
WARNING: Verbosity: skip to the very bottom paragraph for succinct version of my question.) Hi- I can't seem to find an answer to this. I am...
13
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is...
1
by: Mr. Beck | last post by:
Hello, Please Help..... I have been working with some tcp/ip socket communication within a C# program recently. Basicly, I have a program...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the...
2
by: manasap | last post by:
Hi all! I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server...
1
by: keksy | last post by:
Hi every1, I am writing a small client/server application and in it I want to send an image asynchronous from the client to the server through a...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.