简体   繁体   中英

Sending string and picture over java sockets: server not receiving string or picture from android client

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.

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);
   }
}

 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 < img.getWidth(); x++) {
                for (int y = 0; y < 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());
        } 

    }

Thanks so much in advance!

Your client code is receiving an image, with readObject() , not sending one, with writeObject() . And your server is also trying to read an image from the client, and not with readObject() , so you aren't using compatible protocols anyway.

BUT ... This is never going to work. You can't assume that the client is magically going to connect to both sockets sequentially and that interleaving as between clients will never occur. Use a single socket for both the filename and the picture. Use DataInput/OutputStreams, use read/writeUTF() for the filenames, and just the ordinary read() and write() methods in the normal way for the picture data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM