server side socket program hangs

Anthony McDonald tonym1972/at/club-internet/in/fr
Fri Oct 10 04:41:46 EDT 2003


<anuradha.k.r at sify.com> wrote in message
news:57efaf35.0310092102.1231b120 at posting.google.com...
> hi,
>   was stuck with another work.now bac to python program.
> i tried out using
> PORT = socket.htons(9999)
>
> it still doesn't work.here is the code.it still hangs.can some one
> tell me what could be te problem?
>
> #from socket import *
> import socket
> import sys
>
> HOST = ''       #any address
> PORT = socket.htons(9222)        #same port address as client
>
> class Sock:
>      def __init__ (self,parent):
>           try:
>                self.s = socket(AF_INET,SOCK_STREAM)
>           except socket.error:
>                 print 'socket not created'
>           try:
>                self.s.bind((HOST,PORT))
>           except self.error:
>                print 'error in bind'
>           try:
>                self.s.listen(5)
>           except self.error:
>                print 'error in listen'
>           conn, addr = self.s.accept()
>              #  print 'Connected by',`addr`
>           while 1:
>                data = conn.recv(1024)
>                if not data: break
>                conn.send(data)
>           conn.close()
>
> thanx,
> AKR.
>
Sorry for my brief answer last time, but I had hoped the code fragment I
posted would explain what was happening with your code.

Your code is almost a verbatum copy of the Python example, except you've
chosen to massage the port value using the HTONS function. So for most
architectures the port value 9999 or 9222 would be changed to somewhere in
the 3xxx port range. Any client trying to connect on those ports will fail.
Hence you noted success on a C server which isn't using that function, but
failure on your Python server which is. The solution is to just assign the
port value you want unchanged.

-- 
Anthony McDonald

This email address is protected by SpamBayes
www.spambayes.org






More information about the Python-list mailing list