0

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

}
duh ikal
  • 161
  • 1
  • 8
  • Try and do a bit more debugging before asking -- for instance, what does InputStream.read return? That might give you a clue. – tgdavies Mar 30 '21 at 03:07
  • It's not really that, there is no error in my code because when i replace ReliableServerSocket with ServerSocket and ReliableSocket with Socket then the "hello" message arrives fine at the server and it's read successfully. – duh ikal Mar 30 '21 at 03:16
  • Have you considered closing any of your sockets? Or are you content for them to just disappear? – user207421 Mar 30 '21 at 03:34
  • 1
    Perhaps the source might enlighten - https://github.com/GermanCoding/RUDP/blob/master/RUDP/src/net/rudp/ReliableSocketOutputStream.java - notice that sock is only written on `flush()`. And in general what if the client closes their end (it breaks on appplication quit). – Mr R Mar 30 '21 at 03:36
  • @user207421 Lol for an testing application like this it's not needed. – duh ikal Mar 30 '21 at 04:09

1 Answers1

0

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

Mr R
  • 754
  • 7
  • 19
  • So we can't use the ReliableSocketOutputStream & ReliableSocketInputStream to send messages, there is no method in ReliableSocket that returns these classes. – duh ikal Mar 30 '21 at 22:02
  • @duhikal those are the classes that ReliableSocket uses - see lines 1135/36 of [ReliableSocket](https://github.com/GermanCoding/RUDP/blob/master/RUDP/src/net/rudp/ReliableSocket.java). You can use - you should just always `flush()` when you've finished writing a whole "message" .. it's designed so you could write(byte), write(byte), write(bytes), flush() - and it doesn't go until you've compiled a whole message - otherwise the otherside could start processing an incomplete message. – Mr R Mar 30 '21 at 22:10