title: Sending string and picture over java sockets: server not receiving string or picture from android client
body: <p>I’m trying to send a string over a socket from a google glass device to a desktop PC - It keeps continuously waiting and did tell me one time that the string was null. I’m also trying to send a picture after the string, but if I can get the string to show up, that would be a huge help for now.</p>
  
  <pre><code>Client:
  
  
  private void createConnection()
  {
     //Create the java socket and send the file to the android device
     //Need to send out the voice command to the Android phone and the picture
     //for now, send the voice data
  
  
     try {
  
         try {
  
             try {
  
                 String ip = "192.168.1.149";
                 Socket skt = new Socket();
                 skt = new Socket(ip, 40404);
                 ///////////////////////////////////////////////////////////////
                 /////////////////////////////////////////////////////
                 //send the string.
  
                 OutputStream outstream = skt.getOutputStream();
                 PrintWriter out = new PrintWriter(outstream);
                 out.print(voiceglobal);
                 Log.d("sending", voiceglobal);
  
                 long length = pictureToSend.length();
  
                 //Create a new socket for sending the picture.
  
                         Socket pic_socket = new Socket(ip, 50505);
                         byte bytes[];
                         ObjectInputStream ois = new ObjectInputStream(pic_socket.getInputStream());
                         FileOutputStream fos = null;
                         try {
  
                             bytes = (byte[])ois.readObject();
                             fos = new FileOutputStream(pictureToSend);
                             fos.write(bytes);
  
                         } catch (ClassNotFoundException e)
                         {
  
                             e.printStackTrace();
  
  
                         } finally {
                             if(fos!= null)
                             {
                                 fos.close();
                             }
                         }
  
             } catch (UnknownHostException u)
             {
              System.err.print(u);
  
             }
         } catch (UnknownHostException e) {
  
             //Handle this.
             System.err.print(e);
         }
     } catch (IOException v) {
         // handle this.
         System.err.print(v);
     }
  }
  </code></pre>
  
  <hr>
  
  <pre><code> Server:
  
   //server
       void createConnection()
      {
  
          try {
  
              ServerSocket serv = new ServerSocket(40404);
              ServerSocket servimage = new ServerSocket(50505);
  
              while(true)
              {
  
                  System.out.println("Listening ... \n");
                  Socket client = serv.accept();
                  BufferedReader enter = new BufferedReader(new InputStreamReader(client.getInputStream()));
                  enter.readLine();
                  String data = enter.readLine();
                  PrintWriter pw = new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true);
                  pw.println(data);
                  System.out.println("String was " + data);
                  Globals.voicedata = data;
  
                  //Now recieve the image from the secondary serversocket.
  
  
                  System.out.println("Recieving image ..  \n");
                  Socket clientimage = servimage.accept();
                  System.out.println("image srv connected to: " + clientimage.getInetAddress());
  
  
            //Bufferedimage should go to an imageview on the GUI.
            BufferedImage img=ImageIO.read(ImageIO.createImageInputStream(clientimage.getInputStream()));
  
  
           //Convert the bufferedimage into an image.
          //reason is that bufferedimages can't be displayed in a javafx imageview.        
          WritableImage wr = null;
          if (img != null) {
              wr = new WritableImage(img.getWidth(), img.getHeight());
              PixelWriter pw2 = wr.getPixelWriter();
              for (int x = 0; x &lt; img.getWidth(); x++) {
                  for (int y = 0; y &lt; img.getHeight(); y++) {
                      pw2.setArgb(x, y, img.getRGB(x, y));
                  }
              }
  
                Globals.img = wr;
          }
          else
          {
              System.out.println("Image was null. \n");
          }
  
  
  
  
              } //while true
  
  
          } catch (IOException e)
          {
              System.out.println("Something went wrong. \n");
              System.out.println(e.fillInStackTrace());
          } 
  
      }
  </code></pre>
  
  <p>Thanks so much in advance!</p>