bind question

张少驰 zhangsc at neusoft.com
Fri Dec 14 04:15:29 CET 2001


I have a question about socket bind,my program is follows:
from socket import *
import select
import re
import string
import time
import sys
HOST='10.1.1.27'
PORT=21567
BUFSIZ=1024
ADDR=(HOST,PORT)
ippat=re.compile(r'[0-9]+([.][0-9]+){3}')
udpCliSock=socket(AF_INET,SOCK_DGRAM)
data=""
try:
 while 1:
   while data=="":
     data=raw_input('ip address:')
     data=data.strip()
     if ippat.match(data) is not None:
       break
     else:
       print data,"does not like an ip address"
       print "input again!"
       data=""
   udpCliSock.sendto(data,ADDR)
   udpCliSock.close()
   HOST=gethostbyname(gethostname())
   ADDR=(HOST,PORT)
   udpCliSock=socket(AF_INET,SOCK_DGRAM)
   udpCliSock.bind(ADDR)
   f = udpCliSock.makefile()
   while 1:
     (r,w,e) = select.select([f], [], [], 5.0)
     if not r:
       print "out of time!"
       break    
     else:
       data, addr = udpCliSock.recvfrom(BUFSIZ)
       if data:
         print data
         break
   udpCliSock.close()
   HOST='10.1.1.27'
   ADDR=(HOST,PORT)
   udpCliSock=socket(AF_INET,SOCK_DGRAM)
except KeyboardInterrupt:
   sys.exit(0)
udpCliSock.close()

When I run it,it raise a error:
$ python c33.py
ip address: 202.112.237.15
out of time!
Traceback (most recent call last):
  File "c33.py", line 30, in ?
    udpCliSock.bind(ADDR)
socket.error: (112, 'Address already in use')

Why is it error? How to correct it? Any ideas will be appreciated!
         Thanks.
              Edward


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20011214/a06d06e8/attachment.html>


More information about the Python-list mailing list