简体   繁体   中英

Java server socket reading

I have a very peculiar problem and my last resort was asking this on StackOverflow, so please be open minded!

Here are two programs:

A. Client: A java client running on an Android phone. B. Server: A java server running on a Computer.

Here is what the two programs do:

Client sends server coordinates (in string format) every 2 milliseconds (very fast), and the server must read all those coordinates that the client sends it. In order to achieve this (given that the server is located at 10.0.0.1 and is listening on port 54321), the server must have a socket via which it reads all the incoming info. And yes, it does receive all of the information, BUT, there is a catch!

Now that you have the background behind the story, here is the problem:

The client connects to the server and as soon as that happens,it starts sending coordinates (in string format) at an extremely fast rate. The server does get all the messages, but it does not stop reading unless the client has disconnected. What i need is for the server to read every single message individually as they received!

Here is the code i used to read from the socket (this is for the server, and it is on its own thread):

    while(true) {
        try {
            BufferedReader socketReader = 
            new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String str = socketReader.readLine();
            System.out.println("New Message: " + str);
            socketReader.close();
        } catch (IOException e) {
            //Client disconnected
            System.out.println("Client disconnected.");
            break;
        }
    }

This is the output i am getting from the client (where x and y are numbers):

 New Message: x,yx,yx,yx,yx,yx,yx,yx,yx,yx,yx,yx,yx,yx,yx,yx,yx,y...and so on

And that text only shows up AFTER the client has disconnected. I want it to show up AS THE MESSAGES come in.

Just to clarify, this is the desired output:

 New Message: x,y
 New Message: x,y
 New Message: x,y
 New Message: x,y
 ... and so on

In case this is of any use, here is the method of writing to the socket (this code is from the client)

PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
while(running) {
    writer = new PrintWriter(socket.getOutputStream(), true);
    writer.write(x+","+y);
    writer.flush();
}

So all in all, what i need is for my server to read the messages as they come in instead of reading them all at the same time after the client disconnects. PS in C# i have also written a server and that one reads the info as it comes in, but in Java this isnt working out!

You are calling readLine which, as its name suggests, is for reading lines . Your data does not consist of lines.

If possible, change your protocol so that each set of coordinates is terminated by a newline and/or carriage return.

Unfortunately, I don't think you're going to be able to get this to work. 2ms resolution is just not possible over commodity network hardware and all kinds of things will break if you try to force it. If you enable Nagle, you'll get 200ms delays from Nagling when an ACK is late. If you disable Nagle, dropping a data packet will shoot your latency through the roof.

Honestly, I'd rip up the design and start over thinking through precisely what your requirements are and how you're going to meet them.

Where did you find an Android device that can send 500 packets a second?! No Wifi I know of can do that, and what use is GPS over a wire?

(If you're not already doing things like shrinking socket buffers to the absolute minimum, using non-blocking write operations, and handling partial writes by aborting and dropping partial data in the receiver, you won't get anywhere close to meeting your requirements. You must not let data back up if there's a momentary drop in connection bandwidth.)

Do a flush() on your output socket in your client each time. The messages are actually being buffered until you close the socket now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM