简体   繁体   中英

Why is this node.js tcp server not writing back to Java client until it closes?

Here is an example of a tcp server written in node.js I found:

net.createServer(function(sock) {

    // We have a connection - a socket object is assigned to the connection automatically
    console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

    // Add a 'data' event handler to this instance of socket
    sock.on('data', function(data) {

        console.log('DATA ' + sock.remoteAddress + ': ' + data);
        // Write the data back to the socket, the client will receive it as data from the server
        sock.write('You said "' + data + '"');

    });

    // Add a 'close' event handler to this instance of socket
    sock.on('close', function(data) {
        console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
    });

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

I am trying to use this as a basis for a tcp server which will have to handle multiple incoming connections at the same time. Here is a Java program I wrote to test this:

public static final String MESSAGE = "Hellow world";
    public static Semaphore networkLock;

    public static void main(String[] args) 
    {
        //writeMessage(MESSAGE);

        Thread[] threadPool = new Thread[10];
        networkLock = new Semaphore(1);

        for (int i = 0; i < threadPool.length; i++) 
        {
            threadPool[i] = new Thread(new Runnable() {

                @Override
                public void run() {
                    writeMessage(MESSAGE);

                }
            });

            threadPool[i].start();
        }

    }

    public static void writeMessage(String test)
    {
        try {
            if(sock == null || sock.isClosed())
                sock = new Socket(HOST, PORT);

            DataOutputStream out =
                    new DataOutputStream(sock.getOutputStream());
            System.out.println("Writting message");

            //networkLock.acquire();
            out.writeUTF(test);
            //networkLock.release();

              BufferedReader in = new BufferedReader(
                        new InputStreamReader(sock.getInputStream()));

              System.out.println("Waiting for reply");
              String input = in.readLine();

              System.out.println(input);
//            in.close();
//          out.close();
//          sock.close();

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

When I start the client, the only output I get from the client is a "Writting message" and "Waiting for reply" a bunch of times. When I shut down the server, I finally get the responses of You said " Hellow world" along with a null or two thrown in usually. As for the server, it prints out the two print statements just fine. Do you think someone could help me out here?

The Java client is using in.readLine() which looks for a newline character in the input stream. However, the server is not writing a newline to the client socket.

So change this:

sock.write('You said "' + data + '"');

to this:

sock.write('You said "' + data + '"\n');

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