Search code examples
javanetwork-programmingudpgnunetcat

UNIX UDP Data Transfer with Java


I want to create a UDP OUTPUT=UPLOAD stream using java. I will get my INPUT=SOURCE data from a named pipe or contiguous file opened as a file input stream.

My problem is, ALL of the UDP examples i can find on the internet, only demonstrate console sessions. Echo servers and such.

I'm trying to create a way to stream continuous content such as audio/video, and i don't care what gets lost, i'm leaving that up to my user to be concerned with, however my code does need to allow setting the buffer size, and creating a UDP connection.

Ideally a GOOD example would show how to do upload and download mode connections (client mode and server mode)

Can you provide some code to do this, or show me a link on the internets? The fact that I cannot find a UDP stream client/server example is ridiculous. Using UDP to do console sessions tests the limits of a person's sanity! That should never even be considered optional, let alone useful. The client/server code i need must be compatible with GNU netcat. (to ensure correct performance)

I have tried this with a client:

byte[] buffer = new byte[udpPacketSize]; // 4096
int len;
while ((len = standardInput.read(buffer)) != -1) {
    udpSocket.send(new DatagramPacket(buffer, len, host, port));
}

But when I stop sending data, and disconnect, I cannot reconnect to send more data. I'm not sure if that is what is supposed to happen, because 1) I am completely out of my element here and 2) when I disconnect after sending the data, the remote instance of GNU netcat does not exit like it does in TCP mode.

HELP, I need a real network systems engineer, to show me how to implement UDP for practical applications!

[and somebody to remove all of that garbage from the internet, but let's keep it simple]

[further: please do not respond with libraries, packages, or shell commands as a solution. i must be able to execute on any embedded device which may not have the programs, and libraries are not teaching me or anyone else how to do anything on their own.]


Solution

  • But when I stop sending data, and disconnect, I cannot reconnect to send more data.

    That's the way GNU Netcat UDP mode works, as far as netcat to netcat on a single machine goes...

    Your client needs to read a response from the server before disconnecting. So, while acting as "middle-man", you should not be concerned with this, as long as you can connect your network-client's local-client, to the server's response mechanism.

    In other words, you need to provide a bi-directional-half-duplex-connection (1:1 communications), since you are not managing the protocol.

    Alternatively, you can use a different UDP server than Gnu Netcat. I have tested this one, and it works without the MUST-READ-REPLY-BUG, effectively meaning, there is nothing wrong with my example code. The must-read-reply feature has nothing to do with a correct UDP server implementation (unless you must connect with a Gnu Netcat 0.7.1 compatible server).

    It is worth nothing that it isn't very useful to use Gnu Netcat UDP server mode without a driver (script/program) behind it, especially if you want a continuous process, as you could lock your remote client out of access until the process is respawned.