RUDP icon indicating copy to clipboard operation
RUDP copied to clipboard

Example-Code?

Open florianewert opened this issue 3 years ago • 4 comments

Hello,

do you have an example-code or tutorial how I can use RUDP in Java?

best regards

florianewert avatar May 18 '22 09:05 florianewert

Hi,

there's a very basic example in the README:

The rule of thumb is to do exactly the same things you would do when using java.io from the standard library, just replace every instance of Socket with ReliableSocket and ServerSocket with ReliableServerSocket.

How to add the library to your classpath depends on what build system you're using so I can't give a general example here.

GermanCoding avatar May 18 '22 16:05 GermanCoding

So far the client and server to communicate in localhost on one device. I'm trying to get multiple devices to communicate with each other. Unfortunately, I cannot deduce how multiple devices should communicate. Do I have to use RUDP in addition to UDP or can RUDP alone communicate via broadcast? Sorry for the many questions. I'm still in training.

florianewert avatar May 23 '22 10:05 florianewert

Imagine the RUDP library as if using TCP. That means that just like in TCP we're connection-orientated and have a server/client model.

In (simple) TCP setups, you usually have a single TCP server on some machine and an arbitrary amount of TCP clients on other machines, all connected to the single server.

When using RUDP, just follow along any tutorial written for java.io sockets - setup a server socket, connect one or more client socket to that server i.e using this constructor

https://github.com/GermanCoding/RUDP/blob/1053c3e03feff0c2dbedc86d87d9f6bf808ed11f/RUDP/src/net/rudp/ReliableSocket.java#L114

and then exchange data using the Sockets InputStreams/OutputStreams. Any good Java socket tutorial should cover this.

If you're looking for non-TCP models such as mesh networks (i.e. all clients are connected with each other), I'm not sure if this library is the right fit for that. You can probably implement such a system with RUDP, but you will need to handle most of the logic required for that yourself (each client will likely need a server socket and establish outgoing connections to the other clients. You might also need discovery mechanisms etc). RUDP is essentially assigned as a drop-in replacement for TCP, if TCP is too heavy or can't be used for some reason.

GermanCoding avatar May 23 '22 13:05 GermanCoding

public class Client {

ReliableSocket rs;

public Client() throws IOException { rs = new ReliableSocket(); rs.connect(new InetSocketAddress("127.0.0.1", 3033));

String message = "Hello";

byte[] sendData = message.getBytes();

OutputStream os = rs.getOutputStream();

os.write(sendData, 0, sendData.length);

}

public Server() throws Exception { rss = new ReliableServerSocket(3033);

while (true) {
    rs = (ReliableSocket) rss.accept();
    
    System.out.println("Client connected");

    byte[] buffer = new byte[100];
    
    InputStream in = rs.getInputStream();
    
    in.read(buffer);
    
    //here the value doesn't return from the inputstream
    System.out.println("message from client: " + new String(buffer).trim());

}

}

example-code like this?

leonlu1983 avatar Jun 10 '22 00:06 leonlu1983