RUDP Packets are not read from inputstream

175 Views Asked by At

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();
}

}
1

There are 1 best solutions below

2
Mr R On BEST ANSWER

The code doesn't work because it assumes that write has sent the bytes, however according to the Javadoc for OutputStream

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

So add this to the client will ensure something is sent.

  os.flush();

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

Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

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).