Connection reset by peer

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Fri Dec 14 08:11:19 EST 2001


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?
  ----- 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               
   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20011214/d9347c0a/attachment.html>


More information about the Python-list mailing list