Open florianewert opened 2 years ago
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.
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.
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
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.
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?
Hello,
do you have an example-code or tutorial how I can use RUDP in Java?
best regards