Aequalitas
Aequalitas

Reputation: 5

Java client stuck in datastream of data from nodejs server

My problem is that when i send data ( with socket.write()) from the nodejs server to the java client the java client is stuck in the datastream as long as there is no FIN packet( which would be send when i use socket.end() in nodejs) from the server.

My quesiton is now wether there is a way java can read it without the FIN package.

I thought there has to be a way because it works perfectly when you create a client with the net module of NodeJS.

Server

var server = require("net").Server();

function cl(t){console.log(t)};

server.listen("5022","127.0.0.1", function(req,res){
cl("Server started...");

});


server.on("connection", function(socket){
var ip = socket.remoteAddress;

socket.setEncoding("utf8");

cl("connection --> "+ip);

socket.write("Welcome...");

socket.on("data",function(d){
    var data = JSON.stringify(d);
    cl("Data arrived "+data);

});

socket.on("end", function(){
    cl("end");
})

socket.on("close",function(){
    cl("Disconnect --> "+ip+"\n");
})

socket.on("error", function(err){
    cl("ERROR "+err);

});
});

Note: So as is said, when i would add socket.end() a FIN packet would be send and the java client gets out of the datastream and returns the data. So at the moment i can send data from the server once in the entire session.

part of Client

Socket sc = new Socket(ip, port);

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

DataOutputStream out = new DataOutputStream(sc.getOutputStream());
String input;

while(true)
{
   while (!in.ready()) {Thread.sleep(2000);}
   input = in.readLine();
   System.out.println("Message : " + input);
   out.writeUTF(input);
}

Note: Sending data to the server does work from this java client.

Upvotes: 0

Views: 518

Answers (1)

mscdex
mscdex

Reputation: 106746

in.readLine() is doing just what it says, reading a line, which means it's looking for a newline character to know when to stop reading. So in your node code just append a \n when you .write() so that the Java code can resume. Example: socket.write("Welcome...\n");

Upvotes: 1

Related Questions