Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - sending packets via UDP
    primarykey
    data
    text
    <p>In my android app, I am sending messages via UDP. I followed <a href="https://stackoverflow.com/questions/3997459/send-and-receive-serialize-object-on-udp-in-java">this</a> and <a href="http://www.javaworld.com/javaworld/javatips/jw-javatip40.html?page=2" rel="nofollow noreferrer">this</a>. However, I am not able to send the initial packet from the client to the server. Please let me know if I am doing something wrong in my code below:</p> <pre><code>class serverCommunicationThread implements Runnable { public void run() { try { serverSocket = new DatagramSocket(STATICPORT); ServerReceiveMessageThread rcvMsgThread = new ServerReceiveMessageThread(serverSocket); new Thread(rcvMsgThread).start(); } catch (IOException e) { e.printStackTrace(); } } </code></pre> <p>This is the ServerReceiveMessageThread class:</p> <pre><code>class ServerReceiveMessageThread implements Runnable { DatagramSocket clientSocket; byte[] buffer = new byte[108]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); ServerReceiveMessageThread(DatagramSocket clientSocket) { this.clientSocket = clientSocket; } @Override public void run() { int counter = 0; MsgStruct recvMsgStruct = null; try { ByteArrayOutputStream baosServer = new ByteArrayOutputStream(); serializeServer = new ObjectOutputStream(baosServer); clientSocket.receive(packet); ByteArrayInputStream baosRecv = new ByteArrayInputStream(buffer); deserializeServer = new ObjectInputStream(baosRecv); while(true) { try { recvMsgStruct = (MsgStruct) deserializeServer.readObject(); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } boolean retVal = serverSendMessage(clientSocket, recvMsgStruct, baosServer.toByteArray()); } catch (IOException e2) { e2.printStackTrace(); } } } </code></pre> <p>The serverSendMessage method:</p> <pre><code>public boolean serverSendMessage(DatagramSocket clientSocket, MsgStruct msgStruct, byte[] buf) { MsgStruct recvMsgStruct = new MsgStruct(); recvMsgStruct.pingPong = true; recvMsgStruct.msgId = msgStruct.msgId; recvMsgStruct.bufferMsg = msgStruct.bufferMsg; try { serializeServer.writeObject(recvMsgStruct); serializeServer.flush(); DatagramPacket packet = new DatagramPacket(buf, buf.length, InetAddress.getByName(mobileIP), STATICPORT); clientSocket.send(packet); } catch (IOException e) { e.printStackTrace(); } return true; } </code></pre> <p>The code for the client send/receive is similar to that of the server. I get a NullPointerException here. </p> <pre><code>serializeClient.writeObject(mobileSendMsgStruct); </code></pre> <hr> <pre><code>public boolean mobileSendMessage(int msgId) { MsgStruct mobileSendMsgStruct = new MsgStruct(); mobileSendMsgStruct.bufferMsg = new byte[100]; mobileSendMsgStruct.pingPong = false; mobileSendMsgStruct.msgId = msgId; rand.nextBytes(mobileSendMsgStruct.bufferMsg); try { sendTime = System.nanoTime(); serializeClient.writeObject(mobileSendMsgStruct); /*I get a NullPointerException here*/ serializeClient.flush(); byte[] sendBuf = baosSend.toByteArray(); DatagramPacket packet = new DatagramPacket(sendBuf, sendBuf.length, InetAddress.getByName(staticIP), STATICPORT); mobileSocket.send(packet); outPing.write(Long.toString(sendTime) + ", " + String.valueOf(mobileSendMsgStruct.msgId) + ", " + String.valueOf(mobileSendMsgStruct.bufferMsg) + " | "); } catch (IOException e1) { e1.printStackTrace(); return false; } return true; } </code></pre>
    singulars
    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.
    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