Connection reset by peer 
Author Message
 Connection reset by peer

I encountered a question,when I deal with UDP socket connection,my programs existed.I have two programs,one is server.py,another is client.py ,I run them in two computers,server.py is at 10.1.1.27,client is at 10.1.1.67 ,my programs are follows:

server.py #It works at IP address of 10.1.1.27

import socket,struct
import os
import string
os.system("ifconfig eth0 promisc")
Ethhdr='!BBBBBBBBBBBBH'
Eth_len=struct.calcsize(Ethhdr)
IPhdr='!bbhhhbbhll'
IPhdr_len=struct.calcsize(IPhdr)
UDPhdr='!HHHH'
UDPhdr_len=struct.calcsize(UDPhdr)
ipsock=socket.socket(socket.AF_INET,10,8)
while 1:
  recvpack=ipsock.recv(1024)
  if len(recvpack) <= (Eth_len+IPhdr_len+UDPhdr_len):
     continue
  data=struct.unpack(Ethhdr,recvpack[:Eth_len])
  if data[12]==2048:
    ipdata=struct.unpack(IPhdr,recvpack[Eth_len:Eth_len+IPhdr_len])
    if ipdata[6]==socket.IPPROTO_UDP:
      udpdata=struct.unpack(UDPhdr,recvpack[Eth_len+IPhdr_len:Eth_len+IPhdr_len+UDPhdr_len])
      if udpdata[1]==21567 or udpdata[0]==21567:
        #I get udpaddr from IP header of 32bit source ip address,
        #and I get udpdata[0] from UDP header of 16bit source port number.
        udpaddr="%i.%i.%i.%i" % (ipdata[8]>>24 & 255,ipdata[8]>>16 & 255,\
                                ipdata[8]>>8 & 255,ipdata[8] & 255)
        addr=udpaddr,udpdata[0]
        print type(addr)
        print addr
        #for some program design reason,I can't use data,addr=udpSerSock.recvfrom(BUFSIZ) to get addr,
        #because data,addr=udpSerSock.recvfrom(BUFSIZ) is conflict with recvpack=ipsock.recv(1024)

        udpSerSock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
        data='hello! I have received message'
        udpSerSock.sendto(data,addr)
        udpSerSock.close()

client.py #It works at IP address of 10.1.1.67

from socket import *
import string
HOST='10.1.1.27'
PORT=21567
BUFSIZ=1024
ADDR=(HOST,PORT)
udpCliSock=socket(AF_INET,SOCK_DGRAM)
data=raw_input('>')
udpCliSock.sendto(data,ADDR)
print udpCliSock.getsockname()
data, addr = udpCliSock.recvfrom(BUFSIZ)
if data:
  print data
udpCliSock.close()

First,I run server.py at 10.1.1.27
$python server.py

Then I run client.py at 10.1.1.67
$python client.py

Quote:
>hello

The running result of server.py:
<type 'tuple'>
('10.1.1.67', 1165)
$

The running result of client.py,it raise a error:
('0.0.0.0', 1165)
Traceback (most recent call last):
  File "client.py", line 11, in ?
    data, addr = udpCliSock.recvfrom(BUFSIZ)
socket.error: (104, 'Connection reset by peer')

I know that my server.py don't use normal way to return data to client.py ,but I need this way! How to correct my programs? Any ideas will be appreciated!
      Thanks.
               Edward              



Tue, 01 Jun 2004 13:29:19 GMT  
 Connection reset by peer

What the heck are you trying to do here?  I've been looking at this code for some time now and I can't
figure out exactly what you are doing or why; this is NOT a normal UDP socket connection because
you have screwed with the ethernet adapter and then attempted to handle the IP packet directly.

Probably the reason no one will answer you is they are as puzzled as me.  Why go to so much trouble
to send UDP data when you can just use the socket module "normally" to do so, in much less code?

Quote:
  ----- Original Message -----
  From:
  To: python-list
  Sent: Thursday, December 13, 2001 11:29 PM
  Subject: Connection reset by peer

  I encountered a question,when I deal with UDP socket connection,my programs existed.I have two programs,one is server.py,another is client.py ,I run them in two computers,server.py is at 10.1.1.27,client is at 10.1.1.67 ,my programs are follows:

  server.py #It works at IP address of 10.1.1.27

  import socket,struct
  import os
  import string
  os.system("ifconfig eth0 promisc")
  Ethhdr='!BBBBBBBBBBBBH'
  Eth_len=struct.calcsize(Ethhdr)
  IPhdr='!bbhhhbbhll'
  IPhdr_len=struct.calcsize(IPhdr)
  UDPhdr='!HHHH'
  UDPhdr_len=struct.calcsize(UDPhdr)
  ipsock=socket.socket(socket.AF_INET,10,8)
  while 1:
    recvpack=ipsock.recv(1024)
    if len(recvpack) <= (Eth_len+IPhdr_len+UDPhdr_len):
       continue
    data=struct.unpack(Ethhdr,recvpack[:Eth_len])
    if data[12]==2048:
      ipdata=struct.unpack(IPhdr,recvpack[Eth_len:Eth_len+IPhdr_len])
      if ipdata[6]==socket.IPPROTO_UDP:
        udpdata=struct.unpack(UDPhdr,recvpack[Eth_len+IPhdr_len:Eth_len+IPhdr_len+UDPhdr_len])
        if udpdata[1]==21567 or udpdata[0]==21567:
          #I get udpaddr from IP header of 32bit source ip address,
          #and I get udpdata[0] from UDP header of 16bit source port number.
          udpaddr="%i.%i.%i.%i" % (ipdata[8]>>24 & 255,ipdata[8]>>16 & 255,\
                                  ipdata[8]>>8 & 255,ipdata[8] & 255)
          addr=udpaddr,udpdata[0]
          print type(addr)
          print addr
          #for some program design reason,I can't use data,addr=udpSerSock.recvfrom(BUFSIZ) to get addr,
          #because data,addr=udpSerSock.recvfrom(BUFSIZ) is conflict with recvpack=ipsock.recv(1024)

          udpSerSock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
          data='hello! I have received message'
          udpSerSock.sendto(data,addr)
          udpSerSock.close()

  client.py #It works at IP address of 10.1.1.67

  from socket import *
  import string
  HOST='10.1.1.27'
  PORT=21567
  BUFSIZ=1024
  ADDR=(HOST,PORT)
  udpCliSock=socket(AF_INET,SOCK_DGRAM)
  data=raw_input('>')
  udpCliSock.sendto(data,ADDR)
  print udpCliSock.getsockname()
  data, addr = udpCliSock.recvfrom(BUFSIZ)
  if data:
    print data
  udpCliSock.close()

  First,I run server.py at 10.1.1.27
  $python server.py

  Then I run client.py at 10.1.1.67
  $python client.py
  >hello

  The running result of server.py:
  <type 'tuple'>
  ('10.1.1.67', 1165)
  $

  The running result of client.py,it raise a error:
  ('0.0.0.0', 1165)
  Traceback (most recent call last):
    File "client.py", line 11, in ?
      data, addr = udpCliSock.recvfrom(BUFSIZ)
  socket.error: (104, 'Connection reset by peer')

  I know that my server.py don't use normal way to return data to client.py ,but I need this way! How to correct my programs? Any ideas will be appreciated!
        Thanks.
                 Edward              



Tue, 01 Jun 2004 21:11:19 GMT  
 Connection reset by peer

Quote:

> udpCliSock=socket(AF_INET,SOCK_DGRAM)
> data=raw_input('>')
> udpCliSock.sendto(data,ADDR)
> print udpCliSock.getsockname()
> data, addr = udpCliSock.recvfrom(BUFSIZ)

Hmm.  It is definitely weird to get ECONNRESET back from a
non-connection-oriented socket.

Well, here's my guess at what's happening.
 * Client sends a UDP packet; this gets sent to the server
   over the network.
 * Server IP layer receives the packet.
 * Server IP layer provides a copy of the packet to the server,
   since the server is listening on a raw socket.
 * Server IP layer then notices that there is no UDP socket
   there, on the server, bound to the specified port.
 * Server IP layer therefore generates an ICMP "Port Unreachable"
   error packet and sends it back to the client.
 * Client IP layer receives the error packet and sets a flag on
   the udpCliSock saying "hey, you got an error"
 * The next time you call udpCliSock.recvfrom(), the error gets
   reported.

This is just a guess though.

If the guess is correct, then you can make this work by
creating a udpServerSock on the server; you can set it to
nonblocking, occasionally do udpServerSock.recvfrom() on it,
and discard the results.

Good luck.

--
Jason Orendorff    http://www.jorendorff.com/



Wed, 02 Jun 2004 00:52:15 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. newbie - winsock error 10054 - connection reset by peer

2. VNC: Connection reset by peer

3. connection reset by peer while running exec

4. SocketServer - Connection Reset By Peer (Randomly)

5. Win2k Peer to Peer and max connections

6. Cool new peer - to - peer tool

7. Peer to Peer

8. CPD 2: networking Windows98 peer-to-peer

9. Printing to a particular printer on Win'98 peer-to-peer Networks

10. Clipper on Win'98 peer-to-peer Networks

11. Win 95 Peer to Peer Network

12. Win 95 Peer to Peer Network (Summary)

 

 
Powered by phpBB® Forum Software