0
votes

I am trying to encode/decode a network packet combining the sender IP address, destination IP address, sender port, destination port and payload data all together.

I've been reading the different ways to do this and everywhere suggests using the Socket class - however, we can't use real network classes like this in our program because it's just a simulated application with no real networking components.

I'm confused on how else I would create a network packet WITHOUT using classes like DatagramPacket or DatagramSocket. The payload data is in byte[] form and the IP addresses are InetAddress. I'm assuming I might have to use some kind of ByteArrayOutputStream to combine all these elements into one network packet of type byte[].

Any suggestions for how I could do this would be really appreciated.

3
Sockets are handled through the OS I believe. The java libraries just interface with the OS. I don't even think windows allows raw packet access. That is to say, there are some packets it won't even let you send - Cruncher
If the data goes nowhere surely you want to just store it in a byte[] and be done with it? - Flexo
that's exactly what I want to do, but I am confused as to how I would combine the sending/recieving IP address, the sending/recieving port number, and the payload in byte[] all in one byte[]. - user2995349

3 Answers

0
votes

Your use case is confusing. You are creating a networked application but you will never put your application on a network? That makes no sense to me.

In Java the SocketFactory creates Sockets (http://docs.oracle.com/javase/7/docs/api/javax/net/SocketFactory.html). You could implement your own SocketFactory to produce custom subclasses of Socket to create all the non-networky Socket things you need. Without an actual network layer, though, I have no idea how you would get these non-network Sockets to communicate so that you can test stuff like "did the server receive the packet I sent".

Further, I don't think you need an actual Network to do Socket stuff. localhost loopback serves this purpose.

I guess I'm just confused by your question.

0
votes

i think you just want to create/edit a byte[] or read a byte[]

because a datagram-packet is nothing else than a byte[]!!!

you might think of methods like:

byte[] package; //it sould be at least 20 byte and conform to TCP/IP IPv4

public static byte[] getSourceIp(byte[] package){
    byte[] sourceIp = new byte[4];
    //System.arraycopy(package, 96, sourceIp, 4); //way shorter
    for (int i = 0; i < 4; i ++){
        sourceIp [i] = package[i+12]; //12=offset for source ip @ipv4
    }
    return sourceIp;
}

public static void setSourceIp(byte[] sourceIp , byte[] package){
    for (int i = 0; i < 4; i ++){
        package[i+12] = sourceIp[i];  //12=offset for source ip @ipv4
    }
}

let's assert you byte[] package is conform to the TCP/IP IPv4 protocol and at least 160 bit (20byte) long... http://en.wikipedia.org/wiki/IPv4

-1
votes

You can use DatagramPacket. It doesn't do anything to the network.

But it seems to me you should be rolling your own packet class here.