1
I am reading strings sent via socket to a Java server.
I get a line but when I read it it comes with break, looking like two lines.
What I got should be:
78780103554880201238560006d0d8
But it comes:
7878
0103554880201238560006d0d8
The code I’m using is as follows::
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Servidor {
public static void main(String[] args) throws Exception {
ServerSocket m_ServerSocket = new ServerSocket(2952);
int id = 0;
while (true) {
Socket clientSocket = m_ServerSocket.accept();
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
cliThread.start();
}
}
}
class ClientServiceThread extends Thread {
Socket clientSocket;
int clientID = -1;
boolean running = true;
ClientServiceThread(Socket s, int i) {
clientSocket = s;
clientID = i;
}
public void run() {
// System.out.println("Accepted Client : ID - " + clientID + " : Address - " + clientSocket.getInetAddress().getHostName());
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
while (running) {
String clientCommand = in.readLine();
byte[] b = clientCommand.getBytes();
clientCommand = bin2Hex(b);
System.out.println(clientCommand);
if (clientCommand.equalsIgnoreCase("quit")) {
running = false;
System.out.print("Stopping client thread for client : " + clientID);
} else {
out.println(clientCommand);
out.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String bin2Hex(byte bytes[]) {
StringBuffer retString = new StringBuffer();
for (int i = 0; i < bytes.length; ++i) {
retString.append(
Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1));
}
return retString.toString();
}
}
I don’t have much intimacy with Java, I think it may be the way I read or the way I define the data for reading.
Some help ?
Tip: You don’t have to keep putting the language name in the question title, the tags you choose already have that role. It’s worth a read here: When to put the language name in the title?
– emanuelsn
Okay, tip noted, I’ll go no further :)
– Pedro Augusto
Checks whether it helps
clientCommand= clientCommand.replaceAll("\\r|\\n", "");
and then you do theSystem.out.println(clientCommand);
– jsantos1991
Same thing, even removing the line breaks
– Pedro Augusto
That means you get 2 real strings right? first string "7878" and then you get "0103554880201238560006d0d8" right? you do not receive a string that splits during printing?
– jsantos1991
The problem is this, the same socket in another language I get only one string, this one in java seems to be two.
– Pedro Augusto
Just out of curiosity: run this System.out.println(clientCommand); before calling bin2hex.
– cantoni
If I do this, appears characters that can not read, the string comes in binary, then the console goes crazy.
– Pedro Augusto