0

I am trying to run in a little script in a loop.. what is should do is open a socket, accept a command, run the command, send a 'finished to the client', close the socket.. and then open it again for a new session. This is a simple exanple of the script I run;

def Tcp_connect( HostIp, Port ):
    global s
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HostIp, Port))
    return

def Tcp_server_wait ( numofclientwait, port ):
        global s2
        s2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s2.bind(('',port))
        s2.listen(numofclientwait)

def Tcp_server_next ( ):
                global s
                s = s2.accept()[0]

def Tcp_Write(D):
   s.send(D + '\r')
   return

def Tcp_Read( ):
        a = ' '
        b = ''
        while a != '\r':
                a = s.recv(1)
                b = b + a
        return b

def Tcp_Close( ):
   s.close()
   return




while True:

    Tcp_server_wait ( 5, 17098 )
    Tcp_server_next()
    data=Tcp_Read()
    print data #Eventually do some stuff here
    Tcp_Write('Done')
    Tcp_Close()
    time.sleep(1)

When I run this it works for 1 time, then I get;

socket.error: [Errno 98] Address already in use

This while I do a close at the end.. any ideas?

Alex van Es
  • 1,141
  • 2
  • 15
  • 27

0 Answers0