title: Sockets from Java to Python and back again
body: <p>Is it possible to communicate with a Raspberry Pi from an Android phone through an app? I've tried and succeded in sending information form the Android App to the RaspberryPi. But when I try to connect to the phone from the RPi by calling <code>phoneServer.connect(addr)</code>, python just times out. So is it possible to send a command from the Python Socket to the Java Socket?</p>
  <p>Initialy I had one socket in the python script and I just used <code>tcpCliSock.sendto(data.encode(),addr)</code> to try and send the command to the java socket, but that resulted in a broken pipe errer (32), so I thought I might be able to fix it by creating a new socket, but it hasn't fixed the issue.</p>
  <p>I've currently tried setting up sockets in Python and in Java. The ones in Python (on RPi) looks something like this:</p>
  <pre><code>HOST = ''
  PORT = 21567
  BUFSIZE = 1024
  ADDR = (HOST,PORT)
  
  tcpSerSock = socket(AF_INET, SOCK_STREAM)
  tcpSerSock.bind(ADDR)
  tcpSerSock.listen(5)
  
  PORTSEND = 7801
  while True:
      print('Waiting for connection')
      tcpCliSock,addr = tcpSerSock.accept()
      data = ''
      print('...connected from :', addr)
      try:
          while True:
              d = tcpCliSock.recv(BUFSIZE)
              data = data + d.decode(&quot;utf-8&quot;)
              print(data)    
              if data == 'XButton':
                  print('XButton Pressed')
                  lst = list(addr)
                  lst[1] = PORTSEND
                  addr = tuple(lst)
                  phoneServer = socket(AF_INET, SOCK_STREAM)
                  phoneServer.connect(addr)
                  phoneServer.send(data.encode())
                  phoneServer.close()
                  break
              else:
                  if not d:
                      break
      except KeyboardInterrupt:
          print('keyboard pressed')
  
  tcpSerSock.close()
  </code></pre>
  <p>And the ones in Java (on the Android) looks something like this:</p>
  <pre><code>public class MainActivity extends AppCompatActivity  {
      EditText iPField;
      Button xButton;
      public static String wifiModuleIp = &quot;&quot;;
      public static int wifiModulePort = 0;
      public static String CMD = &quot;0&quot;;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Thread myThread = new Thread(new MyServerThread());
          myThread.start();
  
          iPField = (EditText) findViewById(R.id.IPEditField);
          xButton = (Button) findViewById(R.id.XButton);
  
          xButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  getIPandPort();
                  CMD = &quot;XButton&quot;;
                  Socket_AsyncTask cmd_verify = new Socket_AsyncTask();
                  cmd_verify.execute();
              }
          });
  
      public void getIPandPort()
      {
          String iPandPort = iPField.getText().toString();
          Log.d(&quot;MYTEST&quot;,&quot;IP String: &quot;+ iPandPort);
          String temp[]= iPandPort.split(&quot;:&quot;);
          wifiModuleIp = temp[0];
          wifiModulePort = Integer.valueOf(temp[1]);
          Log.d(&quot;MY TEST&quot;,&quot;IP:&quot; +wifiModuleIp);
          Log.d(&quot;MY TEST&quot;,&quot;PORT:&quot;+wifiModulePort);
      }
      public class Socket_AsyncTask extends AsyncTask&lt;Void,Void,Void&gt;
      {
          Socket socket;
  
          @Override
          protected Void doInBackground(Void... params){
              try{
                  InetAddress inetAddress = InetAddress.getByName(MainActivity.wifiModuleIp);
                  socket = new java.net.Socket(inetAddress,MainActivity.wifiModulePort);
                  DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
                  dataOutputStream.writeBytes(CMD);
                  dataOutputStream.close();
                  socket.close();
              }catch (UnknownHostException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}
              return null;
          }
      }
  
      class MyServerThread implements Runnable
      {
          Socket s;
          ServerSocket ss;
          InputStreamReader isr;
          BufferedReader bufferedReader;
          String message;
          Handler h = new Handler();
  
          @Override
          public void run() {
              try
              {
                  ss = new ServerSocket(7801);
                  while(true)
                  {
                      s = ss.accept();
                      isr = new InputStreamReader(s.getInputStream());
                      bufferedReader = new BufferedReader(isr);
                      message = bufferedReader.readLine();
  
                      h.post(new Runnable() {
                          @Override
                          public void run() {
                              Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();
                          }
                      });
  
                  }
  
              }catch(IOException e)
              {
                  e.printStackTrace();
              }
          }
      }
  }
  </code></pre>