简体   繁体   English

通过DataOutputStream.writeUTF()发送信息时出现奇怪的字符

[英]strange characters when sending information via DataOutputStream.writeUTF()

I'm trying to send information json format through socket but when the server sends and arrive at the beginning they have a certain special characters. 我正在尝试通过套接字发送信息json格式,但是当服务器发送并到达开头时,它们具有某些特殊字符。

like ♠ * {Trace: {listfile ....}} 像♠* {Trace:{listfile ....}} 在此处输入图片说明

Class Client 类客户

public static void Connect() throws UnknownHostException, IOException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException{
    String OUTPUT = getProperties();
    try{
    Tunnel = new Socket( HOSTNAME , PORT );
    InputStream = new DataInputStream(Tunnel.getInputStream());
    OutputStream = new DataOutputStream(Tunnel.getOutputStream());
    String OutSerial = "{ Trace: { "+OUTPUT+"}}";
    OutputStream.writeUTF(OutSerial);
    Tunnel.close();
    }catch(Exception e){}
}

Class Server 类服务器

public static void Listening(String port) throws IOException, InterruptedException{
        BufferedReader Input;
        int PORT = Integer.parseInt(port);
        sc =new ServerSocket(PORT);
        so =new Socket();
        so = sc.accept();
        Debug.DebugM("It is connected a bot.");
        Input = new BufferedReader(new InputStreamReader(so.getInputStream()));
        Output = new DataOutputStream(so.getOutputStream());
        String INPUT = null;
        while((INPUT = Input.readLine()) != null){

            System.out.println(INPUT);

        }}

How to solve? 怎么解决? Thanks 谢谢

What's happening is that the writeUTF() method is prepending some bytes. 发生的情况是writeUTF()方法在某些字节之前。 Use readUTF() instead of readLine() like you're doing. 像执行操作一样,使用readUTF()而不是readLine()

See DataInput for the byte structure of the UTF representation. 有关UTF表示形式的字节结构,请参见DataInput

Like so: 像这样:

Input = new DataInputStream(so.getInputStream());
String INPUT = Input.readUTF();
System.out.println(INPUT);

You shouldn't use writeUTF() for strings of arbitrary size. 您不应将writeUTF()用于任意大小的字符串。 It's limited to a 16-bit length. 仅限16位长度。 And you can't use it at all unless the peer is going to use readUTF() to read it. 而且,除非对等方将使用readUTF()来读取它,否则您将无法使用它。

I would use BufferedWriter.write()/newLine() to write, and BufferedReader.readLine() to read. 我将使用BufferedWriter.write()/newLine()进行写入,并使用BufferedReader.readLine()进行读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android socket DataOutputStream.writeUTF - android socket DataOutputStream.writeUTF 从DataOutputStream.writeUTF()读取时如何获取Java字符串的“原始”字节? - How to get 'original' bytes of a Java String when read from DataOutputStream.writeUTF()? 为什么 DataOutputStream.writeUTF() 在开头添加额外的 2 个字节? - Why does DataOutputStream.writeUTF() add additional 2 bytes at the beginning? 在线程中循环 DataOutputStream writeUTF - Looping DataOutputStream writeUTF in thread java.io.DataOutputStream 中的 writeUTF - writeUTF in java.io.DataOutputStream 通过DataOutputStream发送信息会引发异常 - Sending information over DataOutputStream throws exception 从DataOutputStream重新分配给BufferedOutputStream和FileOutputStream之后,再也无法使用writeUTF() - Not able to use writeUTF() anymore after reassigning to BufferedOutputStream and FileOutputStream from DataOutputStream 通过dataoutputstream发送字节数组后,接收到的字节数组不等于原始字节数组 - After sending byte arrays via dataoutputstream, the received byte arrays are not equal to original byte arrays ObjectOutputStream.writeUTF 在开始时写入损坏的字符 - ObjectOutputStream.writeUTF writes corrupt characters at the start Java TCP-使用DataOutputStream发送文件 - Java TCP - Sending a file using DataOutputStream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM