Reading via Socket

Asked

Viewed 88 times

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

inserir a descrição da imagem aqui

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 ?

  • 2

    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?

  • Okay, tip noted, I’ll go no further :)

  • Checks whether it helps clientCommand= clientCommand.replaceAll("\\r|\\n", ""); and then you do the System.out.println(clientCommand);

  • Same thing, even removing the line breaks

  • 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?

  • The problem is this, the same socket in another language I get only one string, this one in java seems to be two.

  • Just out of curiosity: run this System.out.println(clientCommand); before calling bin2hex.

  • If I do this, appears characters that can not read, the string comes in binary, then the console goes crazy.

Show 3 more comments

1 answer

1


The problem seems to be with the charset. Force the charset for UTF-8 on the following lines (34, 35):

BufferedReader   in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "UTF-8"));
PrintWriter   out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(), "UTF-8"));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.