1
I am developing a server that accepts connections via socket.
But the information received is in binary and during printing appear strange characters.
I believe that the conversion or the way I read the data is not very correct (not to say totally wrong).
Below the code so far:
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 {
int porta = 2952;
ServerSocket m_ServerSocket = new ServerSocket(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 don’t have much intimacy with Java yet.
I would like possible solutions for this my impassable.
I have in mind that I must convert the binary value to Hexa, but I have not yet achieved such feat.
Done. I did not seek another way. But I already have starting point for study. Thank you !
– Pedro Augusto