473,889 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,Northea stern University,Chin a
ah******@gmail. com
http://www.herofit.cn

Jul 7 '07 #1
14 12835
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,Northea stern University,Chin a
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,Northea stern University,Chin a
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(s ocket.AF_INET, socket.SOCK_STR EAM)
s.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 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(3 2,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(s ocket.AF_INET, socket.SOCK_STR EAM)
s.connect((HOST , PORT))
for i in range(12):
print "time %d" % i
s.send('0123456 789')
#data = s.recv(1024)
#print "data %d" %i, data
#s.shutdown(soc ket.SHUT_WR)#no more write
data=s.recv(102 4)
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('0123456 789')
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,Northea stern University,Chin a
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(s ocket.AF_INET, socket.SOCK_STR EAM)
s.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 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(3 2,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(s ocket.AF_INET, socket.SOCK_STR EAM)
s.connect((HOST , PORT))
for i in range(12):
print "time %d" % i
s.send('0123456 789')
#data = s.recv(1024)
#print "data %d" %i, data
#s.shutdown(soc ket.SHUT_WR)#no more write
data=s.recv(102 4)
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('0123456 789')
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('0123456 789')
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('0123456 789')
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('0123456 789')
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,Northea stern University,Chin a
ah******@gmail. com
http://www.herofit.cn

Jul 8 '07 #6
"ahlongxp" <ah...p@gmail.c omwrote:
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.c omwrote:

>
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,Northea stern University,Chin a
ah******@gmail. com
http://www.herofit.cn

Jul 9 '07 #10

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

Similar topics

8
21921
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 until it gets to around 1500 to 1800 messages. At that time, I get a SocketException with the message "Only one usage of each socket address (protocol/network address/port) is normally permitted". This happen if you use localhost or between two distinct computers. And, for a period of time after the...
2
2891
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… We made a small games called reversi, now our job is to make a server, none of us know nothing about socket programming in C#, but we founds some guides for this, and now ,got a server running, but we have some problems though. We have 2 scenario, one where we use a telnet connection and one...
1
6305
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 playing around with a variation of the ".NET Framework Developer's Guide" (in the MSDN docs) "Using an Asynchronous Server Socket" sample. As usual with docs like this, the example isn't very good. I had to
13
2660
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 TCP. the remoting site works like a champ. my problem is executing the transactions against the remote server and returning the response to the remoting client. i can open the socket fine and, if i am executing one transaction at a time, everything works great. it's when my proxy server...
0
4712
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 shown at end of question. Each connection has a number of different types of data coming in. I have a databuffer for each type of data coming in.
1
17558
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 (myProblemProgram) that has a socket connected to another program for information passing. Upon receiving a particular "command" from the the information passing program, myProblemProgram will launch a separate thread to do individual communication with another file transfer program. The thread...
4
3611
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 results back to the client. The asynchronous socket client and asynchronous socket server example code provided in the .NET framework developers guide is a great start but I have not dealt with sockets before and I am struggling with something. From what I can tell the sample server code ...
2
3689
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 receives the packets. This process proceeds smoothly without any errors but,sometimes I get the following error. "An existing connection was forcibly closed by the remote host." Why is this happening when i am not closing the client program? Could someone guide me in this issue
1
7207
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 TCP socket. I found an example code on the MSDN site, which is actually for sending strings. I tried to adapt this code so that the client sends an image instead of a string. However, there is something wrong on the server side (i guess)... The server starts listening, the client starts sending...
0
9966
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10442
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7997
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7150
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5830
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4647
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.