title: Socket Programming Multiple Clients - One Server Python
body: <p>Sorry for the long post. I am writing a TCP socket server which communicates with two different clients (client1 and client2). The server receives request from client1 and on receiving the request, the server sends data to client2 and in turn client2 responds with required data.</p>
  
  <p>In detail, client1 is an android device requesting some location information from the server. On receiving the request, the server communicates with the computer (client2) that hosts the location information and the information is given from client2 to server. The received information is further communicated from server to client1 (android device).</p>
  
  <p>For implementing this, I have the following steps written:<br/>
  1. Since there may be number of android devices (client1) connecting to the server, there is a thread handling the connections with client1 over a port (port1).<br/>
  2. Once the server gets a request from android device, it communicates with location client (client2) and get the required data.<br/>
  3. Communication with client2 happens over same port and that works fine.<br/>
  4. After receiving the data from client2, the server has to transfer the obtained data back to client1.<br/></p>
  
  <p>I am facing problem in step4. ie, I created a socket inside the function that transfers the data to client2 in a different port (port2) and transferred the data. But I am getting the following error "Address already in use".</p>
  
  <p>To avert this, I tried transferring the information over the socket connection (created on port1) from within the function. The data transfer is failing, that is client2 is not receiving the data sent over the socket.</p>
  
  <pre><code>class ClientThread(Thread): 
      def __init__(self,ip,port,obj_socket_connect): 
          Thread.__init__(self) 
          self.obj_socket_connect = obj_socket_connect
  
      def run(self):
          read_data = conn.recv(2048)
          if read_data == "loc_info_req":
              self.obj_socket_connect.request_for_location()
  </code></pre>
  
  <p>Another class that communicates the data from client2 and send the data to client1. </p>
  
  <pre><code>class socket_connect:
      def __init__(self):
          self.write_android_request_port = Port2
  
      def request_for_location(self):
          # Information requested to client 2
          self.read_loc_data(c_write)
  
      def read_loc_data(self,c_read): 
          read_data =  c_read.recv(15) 
          # Data processing for sending the info to client1          
          self.write_to_android_dev_data(read_data)
  
      # Writing the required information to android on another port
      # The problem of "Address already in use occurs here"
      def write_to_android_dev_data(self,data_to_write):
          s2 = socket.socket()         # Create a socket object
          s2.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
          host = socket.gethostname()
          s2.bind(('', self.write_android_request_port))
          s2.listen(5)
          c_write,address = self.s2.accept()
          c_write.send(data_to_write)
          s2.close()
  </code></pre>
  
  <p>The main function is given below:</p>
  
  <pre><code>if __name__ == "__main__":
      obj_socket_connect = socket_connect()
      print "Read write ports assigned"
      # Multithreaded Python server : TCP Server for android devices connection
      TCP_PORT = Port1
      BUFFER_SIZE = 20
  
      tcpServer = socket.socket() 
      tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
      tcpServer.bind(('', TCP_PORT)) 
      threads = []
  
      # Thread is created everytime when new Android device is connected         (client1) 
      while True: 
          tcpServer.listen(4) 
          print "Multithreaded Python server : Waiting for connections from TCP clients..." 
          (conn, (ip,port)) = tcpServer.accept()
          # Calling the thread function and creating the object
          newthread = ClientThread(ip,port,obj_socket_connect) 
          newthread.start() 
          threads.append(newthread) 
  
      for t in threads: 
          t.join()
  </code></pre>
  
  <p>All the above pieces of code, belongs to same file and this acts as the TCPserver.</p>
  
  <p>Client1 : (Android device)
  The android device sends the request through writing on the Port1, sends the string "loc_info_req". This works fine as the information is available in server after request. On receiving the request the server communicates with client 2 (another computer)</p>
  
  <p>Client 2 is another computer sending data on request.
  Server - Client2 data transfer works fine, but the problem is when data has to be transferred back to client1 from server. </p>
  
  <p>For receiving the data on Android device, I have the following lines of code:</p>
  
  <pre><code># When tried to receive the data on Port1, the information is not getting
  # transferred here. So, receiving data on another port.
  android_loc_client = new Socket(TCP_IP, Port2);
  android_loc_client.setSoTimeout(100000);
  BufferedReader input_info = new BufferedReader(new   
  InputStreamReader(android_loc_client.getInputStream()));
  message = input_info.readLine();
  </code></pre>
  
  <p>Note: I am not having enough credits to upload images for better explanation. Or I have a pictorial representation of what is written here. The data transfer works fine for few iterations and after sometime, the error of "Address already in use" error comes. Tried closing and shutdown the socket inside server. Still the error persists. </p>