簡體   English   中英

通過Java套接字發送字符串和圖片:服務器未從android客戶端接收字符串或圖片

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

我正在嘗試通過套接字將字符串從Google Glass設備發送到台式機-它不斷等待,並確實告訴我該字符串為空。 我還嘗試在字符串之后發送圖片,但是如果我可以顯示字符串,那么這將對當前有很大幫助。

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

    }

非常感謝!

您的客戶端代碼使用readObject() 接收圖像,而不使用writeObject()發送圖像。 而且您的服務器還嘗試從客戶端讀取圖像,而不是使用readObject()來讀取圖像,因此無論如何您都不在使用兼容協議。

但是...這永遠都行不通。 您不能假設客戶端將神奇地順序連接到兩個套接字,並且客戶端之間的交錯將永遠不會發生。 文件名和圖片都使用一個套接字。 使用DataInput/OutputStreams,對文件名使用read/writeUTF() ,對於圖像數據,僅以普通方式使用普通的read()write()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM