I am using the RUDP protocol to send and receive packets using this very useful java library wich implements the RUDP protocol in java. The design of the library is very similar to TCP. It comparatively uses a ReliableServerSocket as a ServerSocket and a ReliableSocket as a Socket.
I do however stumble upon an error when i create a client connection to the server. The connection between the server and client is successfully created because everything past accept() method is executed. However the inputstream doesn't hold any bytes when trying to read from it.
Client:
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 static void main(String[] args) throws IOException {
new Client();
}
}
Server:
public class Server {
ReliableServerSocket rss;
ReliableSocket rs;
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());
}
}
public static void main(String[] args) throws Exception {
new Server();
}
}
The code doesn't work because it assumes that write has sent the bytes, however according to the Javadoc for OutputStream
So add this to the client will ensure something is sent.
It turns out this is the case with the RUDP library - assuming this is the actual source code for ReliableSocketOutputStream at line 109 is the only place in that class that the underlying socket is actually written to i.e. in
flush().On the server side - it should sit looping until the connection is closed. See Javadoc for InputStream
So if it's returning and there isn't have any data it's because of the other reasons (end of file is synonym for closed stream/socket).