socket question

Steve Holden sholden at holdenweb.com
Fri Dec 14 08:13:50 EST 2001


"张少驰" <zhangsc at neusoft.com> wrote ...
> 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
>
> ....
> #I get udpaddr from IP header of 32bit source ip address,
> #and I get udpdata[0] from UDP header of 16bit source port number.
>  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
>  udpSerSock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
>  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!

The server should call udpSerSock.bind( ('10.1.1.27', 21567) ) before it
issues the recvfrom() call - otherwise, how is the server's UDP layer to
know which interface and address to listen on?

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list