DataInputStream readUTF崩溃程序

huangapple 未分类评论46阅读模式
英文:

DataInputStream readUTF crashing program

问题

以下是翻译好的内容:

发送部分:

while(scanning){
    try {
        clientSocket = new Socket(HOST, PORT);
        dataInput = new DataInputStream(clientSocket.getInputStream());
        dataOutput = new DataOutputStream(clientSocket.getOutputStream());
        System.out.println("Connected to server");
        receiverThread = new ReceiverThread(clientSocket, dataInput, dataOutput);
        receiverThread.start();
        scanning = false;
        Map<String, String> jsonMap = new HashMap<String,String>();
        jsonMap.put("UserName", "TestName");
        jsonMap.put("FileName", "TestFileName.txt");
        jsonMap.put("FileSize", "1234");

        //发送文件
        File f = new File("test.txt");
        System.out.println("F length" + f.length());
        dataOutput.writeByte(1);
        dataOutput.writeUTF(String.valueOf(jsonMap));
        dataOutput.writeByte(2);
        dataOutput.write(Integer.parseInt(String.valueOf(f.length())));
        //发送文件内容
        //sendFile(f.getAbsolutePath(), f.length(), receiverThread.getDataOutputStream());
        //发送结束

    } catch (ConnectException e) {
        System.out.println("Server not available");
    } catch (SocketException e) {
        System.out.println("Connection broken");
    } catch (Exception e) {
        System.out.println("ERROR " + e);
    } finally {
        if (receiverThread != null)
            try {
                receiverThread.interrupt();
            } catch (Exception e) {
            }
    }
}

接收部分:

while (true) {
    try {
        messageType = dataInput.readByte();
        switch (messageType){
            case 1:
                System.out.println("TEST  " + dataInput.readUTF());
                message = dataInput.readUTF();
                System.out.println("TTTT " + message);
                stringToMap(message);
                System.out.println("TEST MAP UN1 ");
                System.out.println("TEST  map " + map.get("UserName"));
                System.out.println("TEST MAP UN2 ");
                break;
            case 2:
                saveFile(clientSocket);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + messageType);
        }
        System.out.println("TEST MESSAGE ");
    } catch (Exception e) {
        System.out.println("ERROR " + Arrays.toString(e.getStackTrace()));
        System.out.println("ERROR " + e.getMessage());
        System.exit(2);
    }
}

关于以下代码段:

System.out.println("TEST  " + dataInput.readUTF());

它可以正常工作,但是当您尝试将其分配给字符串变量时,程序不会崩溃、不会抛出错误,只是停在System.out.println语句上,您有没有任何想法为什么?

可能的原因是,您在读取UTF格式的字符串后,尝试将其分配给字符串变量。然而,在这之后,还有另一个读取UTF字符串的操作,这可能导致数据读取的不同步,从而影响后续的代码执行。您可以尝试将接收代码修改为:

case 1:
    System.out.println("TEST  " + dataInput.readUTF());
    message = dataInput.readUTF(); // 只读取一次
    System.out.println("TTTT " + message);
    stringToMap(message);
    System.out.println("TEST MAP UN1 ");
    System.out.println("TEST  map " + map.get("UserName"));
    System.out.println("TEST MAP UN2 ");
    break;

这样可以确保在进行其他操作之前只读取一次UTF字符串。这有助于保持数据同步,避免读取不正确的数据。

英文:

I have question, I'm trying to send Map<String,String> using socket like below

 while(scanning){
        try {
            clientSocket = new Socket(HOST, PORT);
            dataInput = new DataInputStream(clientSocket.getInputStream());
            dataOutput = new DataOutputStream(clientSocket.getOutputStream());
            System.out.println(&quot;Connected to server&quot;);
            receiverThread = new ReceiverThread(clientSocket, dataInput, dataOutput);
            receiverThread.start();
            scanning = false;
            Map&lt;String, String&gt; jsonMap = new HashMap&lt;String,String&gt;();
            jsonMap.put(&quot;UserName&quot;, &quot;TestName&quot;);
            jsonMap.put(&quot;FileName&quot;, &quot;TestFileName.txt&quot;);
            jsonMap.put(&quot;FileSize&quot;, &quot;1234&quot;);

            //sending file
            File f = new File(&quot;test.txt&quot;);
            System.out.println(&quot;F length&quot;+ f.length());
            dataOutput.writeByte(1);
            dataOutput.writeUTF(String.valueOf(jsonMap));
            dataOutput.writeByte(2);
            dataOutput.write(Integer.parseInt(String.valueOf(f.length())));
            //sendFile(f.getAbsolutePath(),f.length(), receiverThread.getDataOutputStream());
            //end sending

        } catch (ConnectException e) {
            System.out.println(&quot;Server not available&quot;);
        } catch (SocketException e) {
            System.out.println(&quot;Connection broken&quot;);
        } catch (Exception e) {
            System.out.println(&quot;ERROR &quot; + e);
        } finally {
            if (receiverThread != null)
                try {
                    receiverThread.interrupt();
                } catch (Exception e) {
                }
        }

and I'm receiving like this

while (true) {
        try {
            messageType = dataInput.readByte();
            switch (messageType){
                case 1:
                    System.out.println(&quot;TEST  &quot; + dataInput.readUTF());
                    message = dataInput.readUTF();
                    System.out.println(&quot;TTTT &quot; + message);
                    stringToMap(message);
                    System.out.println(&quot;TEST MAP UN1 &quot;);
                    System.out.println(&quot;TEST  map &quot; + map.get(&quot;UserName&quot;));
                    System.out.println(&quot;TEST MAP UN2 &quot;);
                    break;
                case 2:
                    saveFile(clientSocket);
                    break;
                default:
                    throw new IllegalStateException(&quot;Unexpected value: &quot; + messageType);
            }
            System.out.println(&quot;TEST MESSAGE &quot;);
        } catch (Exception e) {
            System.out.println(&quot;ERROR &quot; + Arrays.toString(e.getStackTrace()));
            System.out.println(&quot;ERROR &quot; + e.getMessage());
            System.exit(2);
        }

and when i do this :

System.out.println(&quot;TEST  &quot; + dataInput.readUTF());

It's works, but when i try to assigne to String variable program do nothing, it's not crashing, not throwing error just stop on the System.out.println...

someone have any idea why ?

huangapple
  • 本文由 发表于 2020年5月30日 02:43:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/62092714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定