简体   繁体   English

通过Java套接字发送字符串和图片:服务器未从android客户端接收字符串或图片

[英]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. 我正在尝试通过套接字将字符串从Google Glass设备发送到台式机-它不断等待,并确实告诉我该字符串为空。 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() . 您的客户端代码使用readObject() 接收图像,而不使用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. 而且您的服务器还尝试从客户端读取图像,而不是使用readObject()来读取图像,因此无论如何您都不在使用兼容协议。

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. 使用DataInput/OutputStreams,对文件名使用read/writeUTF() ,对于图像数据,仅以普通方式使用普通的read()write()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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