Connection reset by peer

张少驰 zhangsc at neusoft.com
Fri Dec 14 00:29:19 EST 2001


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/cfcade15/attachment.html>


More information about the Python-list mailing list