I'm developing a server that accepts socket connections. However, the information received is binary and strange characters appear during printing.
IthinktheconversionorthewayIreadthedataisnotverycorrect(nottosaytotallywrong).Belowthecodesofar:
importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.io.OutputStreamWriter;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;publicclassServidor{publicstaticvoidmain(String[]args)throwsException{intporta=2952;ServerSocketm_ServerSocket=newServerSocket(porta);System.out.print("ESCUTANTO PORTA : "+porta+"\n\n");
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("CONECTOU : ID - " + clientID + " - " + 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();
System.out.println("ENVIOU :" + 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();
}
}
}
As I do not have much intimacy with Java yet.
I would like possible solutions to this problem.
I have in mind that I should convert the binary value to Hexa, but I have not yet achieved such a feat.