I have done a scratch following this example: http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html
this is my worker thread implementation:
Code: Select all
public class TcpSocketRunnable implements Runnable {
protected Socket mClientSocket = null;
public interface TcpSocketListener {
public void onPacket(byte[] packet, int length);
public String getResponse(String sourceMacAddress);
}
private TcpSocketListener mListener = null;
public void setListener(TcpSocketListener listener) {
mListener = listener;
}
public TcpSocketRunnable(Socket clientSocket) {
this.mClientSocket = clientSocket;
}
private byte[] prepareReplyToPacket(byte[] pckt, String response) {
byte[] srcMac = new byte[6];
byte[] payloadPacket = response.getBytes();
int responsePacketLength = 16 + payloadPacket.length;
byte[] responsePacket = new byte[responsePacketLength];
/* Build response packet */
System.arraycopy(pckt, 10, srcMac, 0, 6);
System.arraycopy(pckt, 0, responsePacket, 0, 2); /* Copy ver, o, flags, proto */
responsePacket[2] = (byte)(responsePacketLength);
responsePacket[3] = (byte)(responsePacketLength>>8);
System.arraycopy(pckt, 4, responsePacket, 10, 6); /* Copy dst address into src field */
System.arraycopy(srcMac, 0, responsePacket, 4, 6); /* Copy src address into dst field */
System.arraycopy(payloadPacket,0,responsePacket,16,payloadPacket.length);
return responsePacket;
}
public void run() {
InputStream input = null;
OutputStream output = null;
try {
System.out.println("socket open!");
byte[] buffer = new byte[1024];
byte[] tmp;
int len;
int read_len;
input = new BufferedInputStream(mClientSocket.getInputStream());
output = new BufferedOutputStream(mClientSocket.getOutputStream());
/* Read first 4 packet byte */
read_len = input.read(buffer, 0, 4);
/* The lenght of the whole packet is in the last two byte */
len = (buffer[3]<<8) + buffer[2];
if (len <= 0) {
return;
}
/* Check if buffer len can store the packet */
if (len > 1024) {
tmp = new byte[len];
/* Copy header in the buffer */
System.arraycopy(buffer, 0, tmp, 0, 4);
buffer = tmp;
}
/* Read the whole packet less the 4 byte header */
read_len = input.read(buffer, 4, len-4);
if (mListener != null && read_len == (len-4)) {
byte[] srcMac = new byte[6];
String srcMacString, response;
System.out.println("Received packet len: "+(read_len+4));
/* Call listener's onPacket */
mListener.onPacket(buffer, len);
/* Check if we have to send a response */
System.arraycopy(buffer, 10, srcMac, 0, 6);
srcMacString = DatatypeConverter.printHexBinary(srcMac);
response = mListener.getResponse(srcMacString);
if (response != null) {
byte[] responsePacket = prepareReplyToPacket(buffer, response);
output.write(responsePacket);
}
}
} catch (IOException e) {
//report exception somewhere.
//e.printStackTrace();
} finally {
try {
if (output != null) {
output.close();
}
if (input != null) {
input.close();
}
this.mClientSocket.close();
System.out.println("socket close!");
} catch (IOException e) {}
mClientSocket = null;
mListener = null;
}
}
}
but it doesn't work well like the python server example. Anyone can help?