title: Python socket tcp communication with subprocess on ROS
body: <p>I tried to send/receive a large video capture image by tcp python socket on ROS-melodic.
  I need to use subprocess because ROS-melodic is running on python2 but I need python3 libraries.</p>
  
  <p>I have a Server and a Client.</p>
  
  <ul>
  <li><p>Client : It is a ROS node that sending the received video capture image(its type is 'list') from camera node after <code>subprocess.Popen(['python3', Serverfile])</code> and <code>client.connect(HOST_IP, HOST_PORT)</code></p></li>
  <li><p>Server : It is a python3 code that receiving the data and doing its functions and then sending the new image to Client.</p></li>
  </ul>
  
  <p>Here is my socket code.</p>
  
  <pre><code>import socket
  import json
  import sys
  ​
  BUFSIZE = 4096
  ​
  def _send(socket, send_data):
      json_data = json.JSONEncoder().encode(send_data)
      socket.sendall(json_data.encode())
  ​
  ​
  def _recv(socket):
      recv_data = socket.recv(BUFSIZE)
      json_data = json.loads(recv_data.decode())
  
      return json_data
  ​
  class Server(object):
      backlog = 1
      client = None
  ​
  ​
      def __init__(self, host, port):
          self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # Error for using port
          self.socket.bind((host, port))
          self.socket.listen(self.backlog)
  
      def __del__(self):
          self.close()
  ​
      def accept(self):
          if self.client:
              self.client.close()
  
          self.client, self.client_addr = self.socket.accept()
          return self
  ​
      def send(self, data):
          if not self.client:
              raise Exception('Cannot send data, no client is connected.')
  ​
          _send(self.client, data)
          return self
  ​
      def recv(self):
          if not self.client:
              raise Exception('Cannot receive data, no client is connected.')
  
          return _recv(self.client)
  ​
      def close(self):
          if self.client:
              self.client.close()
              self.client = None
  
          if self.socket:
              self.socket.close()
              self.socket =None
  ​
  ​
  class Client(object):
      socket = None
  ​
      def __init__(self):
          self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  ​
      def __del__(self):
          self.close()
  ​
      def connect(self, host, port):
          self.socket.connect((host, port))
  
          return self
  ​
      def send(self, data):
          if not self.socket:
              raise Exception('You have to connect first before sending data.')           
  
          _send(self.socket, data)
          return self
  
      def recv(self):
          if not self.socket:
              raise Exception('You have to connect first before receiving data.')
  
          return _recv(self.socket)
  ​
      def close(self):
          if self.socket:
              self.socket.close()
              self.socket = None
  </code></pre>
  
  <p>It outputs 'Message too long' when I use <code>socket.SOCK_DGRAM</code>. So I changed it <code>socket.SOCK_STREAM</code> then it now outputs 'Connection refused'</p>