简体   繁体   English

Java TCP Socket编程引发IO异常

[英]Java TCP Socket programming throwing IO exceptions

I have written a client-server TCP Socket program such that the client will enter a string and that will be displayed by the server(echo program)... the condition is when I enter bye the program should exit... Well and good the client program is exiting ... but the server program is exiting with a java.io.DataInputStream.readUTF() and readFully exceptions.. plz help me.. whats wrong in the code... 我编写了一个客户端-服务器TCP套接字程序,以便客户端将输入一个字符串,并由服务器(回显程序)显示...条件是当我进入再见时,程序应退出...很好客户端程序正在退出...但是服务器程序正在退出,并带有java.io.DataInputStream.readUTF()和readFully异常..请帮助我..代码中有什么问题...

/*Client*/

import java.net.*;
import java.io.*;
public class echoclient {

public static void main(String args[]) throws Exception
{
    Socket soc = new Socket("127.0.0.1",4000);
    OutputStream sout = soc.getOutputStream();
    DataOutputStream out = new DataOutputStream(sout);
    DataInputStream din = new DataInputStream(System.in);
    while(true)
    {
    String message = din.readLine();
    if(message.equals("bye"))
        break;
    out.writeUTF(message);
    }
    soc.close();
}

} }

   /*Server*/
import java.io.*;
import java.net.*;
public class echoserver {

public static void main(String args[]) throws Exception
{
    ServerSocket server = new ServerSocket(4000);
    Socket soc = server.accept();
    System.out.println("Server Started....");

    InputStream sin = soc.getInputStream();
    DataInputStream in = new DataInputStream(sin);
    while(true)
    {
    String fromClient = in.readUTF();
    if(fromClient.equals("bye"))
        {break;}
    System.out.println("Client says : "+fromClient);
    }
    soc.close();server.close();
}
 }

Exception: 例外:

    Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
at java.io.DataInputStream.readUTF(DataInputStream.java:589)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at echoserver.main(echoserver.java:15)

I think that the answer is that that's just what happens. 我认为答案就是这样。 The EOFException is the only way that the DataInputStream tell the caller that it has reached EOF. EOFExceptionDataInputStream告诉调用方它已经到达EOF的唯一方法。 So if your server attempts to read when the socket has been closed by your client, that's what will happen. 因此,如果您的服务器尝试在客户端关闭套接字时读取,则将发生这种情况。

There are two solutions: 有两种解决方案:

  • Catch and handle the EOFException. 捕获并处理EOFException。

  • Modify your "protocol" so that the client (somehow) tells the server when it has sent the last message ... and is going to close the socket. 修改您的“协议”,以便客户端(以某种方式)告诉服务器何时发送了最后一条消息……并打算关闭套接字。


Closing the DataOutputStream on the client side is a good idea, but it probably won't solve the problem. 在客户端关闭DataOutputStream是一个好主意,但可能无法解决问题。 A DataOutputStream is not buffered and neither is a Socket's OutputStream , so there should be no data to be flushed. 没有缓冲DataOutputStream ,也不是Socket的OutputStream ,因此应该没有要刷新的数据。 (Any data outstanding data in the network protocol stack should be transmitted when the Socket or OutputStream is closed.) (关闭SocketOutputStream时,应传输网络协议堆栈中的所有未完成数据。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM