Note that there are some explanatory texts on larger screens.

plurals
  1. POReceiving byte array over a socket
    primarykey
    data
    text
    <p>i have a program which accepts connection from a phone that sends a byte array, i am able to test the connection when it is made , however how do i know that i am actually receiving something? how can i "see" if any thing is being sent over the socket. Because from my code below i am not able to have the resulting file "saved.jpg" created. Does this mean that it did not receive anything?</p> <pre><code>public class wpsServer { //vars private int svrPort = 3334; private ServerSocket serverSocket; private Image image = null; public wpsServer() { try { serverSocket = new ServerSocket(svrPort); System.out.println("Server started on "+svrPort); } catch (IOException e) { System.out.println("Could not listen on port: "+svrPort); System.exit(-1); } } public void listenForClient() { Socket clientSocket = null; try { clientSocket = serverSocket.accept(); if(clientSocket.isConnected()) System.out.println("Connected"); byte[] pic = getPicture(clientSocket.getInputStream()); InputStream in = new ByteArrayInputStream(pic); BufferedImage image = ImageIO.read(in); File outputfile = new File("saved.jpg"); ImageIO.write(image, "jpg", outputfile); } catch (IOException e) { System.out.println("Accept failed: "+svrPort); System.exit(-1); } } public byte[] getPicture(InputStream in) { try { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int length = 0; while ((length = in.read(data))!=-1) { out.write(data,0,length); } return out.toByteArray(); } catch(IOException ioe) { //handle it } return null; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload