Hi, i am current having a very simple program that does receive UDP packets every second with 32 bytes in length and sending the same packet back without modifying it. I use Sniffer to track packet info but I couldn't get any clue about this; the hex value has extra info in front of the actual value that was sent by another computer. Please help, here is my code:
package ca.product.plc;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class Response implements Runnable {
private DatagramPacket packet;
private int destination;
private int source;
public Response(){}
public Response(DatagramPacket packet){
this.packet = packet;
}
public void run() {
try{
DatagramSocket server = new DatagramSocket();
packet.setPort(destination);
server.send(packet);
server.close();
} catch (SocketException se) {
se.printStackTrace();
} catch(IOException ioe){
ioe.printStackTrace();
}
}
public DatagramPacket getPacket() {
return packet;
}
public void setPacket(DatagramPacket packet) {
this.packet = packet;
}
public int getDestination() {
return destination;
}
public void setDestination(int destination) {
this.destination = destination;
}
public int getSource() {
return source;
}
public void setSource(int source) {
this.source = source;
}
}
package ca.product.plc;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
/**
* Simulate UDP server that receive packets from port 2000
*
* @author Andy Leung
*
*/
public class Test {
public static void main(String[] args) {
try {
InetAddress ip = InetAddress.getLocalHost();
int source = 2000;
int destination = 2001;
DatagramSocket server = new DatagramSocket(source);
int length = 4096;
byte[] buf = null;
System.out.println("Broadcasted: " +server.getBroadcast()
+ ", Bound:"+server.isBound()
+ ", connected:"+server.isConnected()
+ ", host ip:" + ip.getHostAddress()
+ ", Local port:" + server.getLocalPort()
+ ", Receive buffer size:" + server.getReceiveBufferSize());
System.out.println();
int counter = 0;
do{
buf = new byte[length];
DatagramPacket packet = new DatagramPacket(buf, length);
server.receive(packet);
Response response = new Response();
response.setPacket(packet);
response.setSource(source);
response.setDestination(destination);
new Thread(response).start();
// packet = new DatagramPacket(packet.getData(),packet.getOffset(), packet.getLength(),packet.getAddress(), destinationPort);
// packet.setData(packet.getData(), packet.getOffset(), packet.getLength());
// server.send(packet);
}while(counter++ < 86400);
server.close();
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
} catch (SocketException se) {
se.printStackTrace();
} catch(IOException ioe){
ioe.printStackTrace();
}
}
}