1

I'm trying to make a cliente/server application, but this problem occurs when I send the pong from server to client. Ping works fine, so what is happening? Here's the code for client class:

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);

}

Here's the server class:

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);
        }
    }
}

And finally the message class:

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);
    } 

}

Well, when I run client after server, this happens: run:

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

The server.java console doesn't show any error. The error occurs when the client try to receive the message from server (so, after the server receive his message fine) in this line of message.java:

 String entryJson = buffer.readLine();

What happens? I'm on this problem for days and nothing that I try works... Can you guys help me?

Thanks!

misc
  • 13
  • 3

1 Answers1

0

The problem with your thing is that the try block in sendMsg() will close the PrintWriter when it's done, which ends up closing the socket. To solve this problem, use a regular try block instead of the try-with-resources.

try {
    PrintWriter w = new PrintWriter(socket.getOutputStream());
    w.println(msg);
}
MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
  • I do this and this problem occurs: Exception in thread "main" java.net.SocketException: Connection reset. It's a problem on readMsg method when I try to read the buffer (line String entryJson = buffer.readLine();). What can I do? – misc Dec 01 '13 at 14:19
  • Does this happen when the client reads it or the server reads it? Anyway, was your original problem solved? – MultiplyByZer0 Dec 01 '13 at 19:57