我的申请中“套接字已关闭”

时间:2013-12-01 07:38:36

标签: java sockets

我正在尝试创建客户端/服务器应用程序,但是当我将pong从服务器发送到客户端时会出现此问题。 Ping工作正常,发生了什么事?这是客户端类的代码:

public static void pingServer(Socket socket) throws UnknownHostException, ParseException, IOException {

    String msgString, reply;
    Message msg = new Message();

    CommonMessage4Parameters msgJson = new CommonMessage4Parameters("pcmj", "ping", getIpClient(), serverIp);
    msgString = msgJson.jsonToString4Param(msgJson);
    msg.sendMsg(msgString,socket);
}

public static void main(String[] args) throws Exception {

    String msgIn = new String();
    String msgOut = new String();
    Message readSocket = new Message();
    String local = new String();        
    String ip_adress = getIpClient();

    setIpsServer();        
    Socket socket = new Socket(serverIp, 9876);

    pingServer(socket);
    System.out.println("PING SENDED. WAITING FOR PONG!");

    msgIn = readSocket.readMsg(socket);
    System.out.println("MESSAGE RECEIVED: " + msgIn);

}

这是服务器类:

public static void pongClient(Socket socket, String receptor) throws UnknownHostException, ParseException, IOException {

    String msgString;
    Message msg = new Message();

    CommonMessage5ParamStatus msgJson = new CommonMessage5ParamStatus("pcmj", "pong", "100", getIpSer(), receptor);
    msgString = msgJson.jsonToString5ParamStatus(msgJson);
    msg.sendMsg(msgString,socket); 
}

public static void main(String[] args) throws Exception {

    CommonMessage4Parameters msg4param = new CommonMessage4Parameters();
    CommonMessage4Parameters msg4param_test = new CommonMessage4Parameters();
    String msgIn;
    String msgOut;
    String local;

    ServerSocket server = new ServerSocket(9876);

    while (true) {

        Message readSocket = new Message();
        System.out.println("SERVER ON - Waiting...");

        Socket socket = server.accept();
        msgIn = readSocket.readMsg(socket);
        System.out.println("MESSAGE RECEIVED: " + msgIn);
        msg4param_test = msg4param.stringToObjec4Param(msgIn);

        if (msg4param_test.getProtocol().equals("pcmj") && msg4param_test.getCommand().equals("ping")) {
            System.out.println("IT'S A PING MESSAGE!");
            String ip_cliente = msg4param_test.getSender();
            System.out.println("IP DE ORIGEM DA MENSAGEM: " + ip_cliente);
            System.out.println("SENDING PONG...");
            pongClient(socket,ip_cliente);
        }
    }
}

最后是消息类:

public String readMsg(Socket entrySocket) throws IOException {

    BufferedReader buffer = new BufferedReader(new InputStreamReader(entrySocket.getInputStream()));
    String entryJson = buffer.readLine();
    return entryJson;
}

public static void sendMsg(String msg, Socket socket) throws IOException {

    try (PrintWriter w = new PrintWriter(socket.getOutputStream())) {
         w.println(msg);
    } 

}

好吧,当我在服务器之后运行客户端时,会发生这种情况: 运行:

PING SENDED. WAITING FOR PONG!
     Exception in thread "main" java.net.SocketException: Socket is closed
     at java.net.Socket.getInputStream(Socket.java:872)
     at Message.readMsg(Message.java:26)
     at Client.main(Client.java:78)
Java Result: 1

server.java控制台未显示任何错误。当客户端在这行message.java中尝试从服务器接收消息时(因此,在服务器收到他的消息之后),会发生错误:

 String entryJson = buffer.readLine();

会发生什么?我已经解决了这个问题好几天了,我尝试的都没有...你们能帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的问题是sendMsg()中的try块会在完成时关闭PrintWriter,最终会关闭套接字。要解决此问题,请使用常规try块而不是try-with-resources

try {
    PrintWriter w = new PrintWriter(socket.getOutputStream());
    w.println(msg);
}