Question

s = new Socket(InetAddress.getByName(address), port);
Thread.sleep(250);
DataOutputStream outToServer = new DataOutputStream(s.getOutputStream());       
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(s.getInputStream()));

outToServer.writeBytes("MY_QUERY");

String rs = inFromServer.readLine();

s.close();

Nothing happens, but if i use .read() i receive the first character of the string.

I don't know what is the message lenght i'll receive, in python i use: sock.recv(1024) where 1024 is the max lenght i can receive.

Is there any way to do the same thing in java?

Pas de solution correcte

Autres conseils

According to javadoc of readLine():

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Make sure that the String has the correct structure.

You are reading lines but you aren't writing lines. writeBytes() writes exactly what you provide to it, no more, no less, and no new lines.

And you should be either using two streams or a Reader and a Writer, not a mixture of both.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top