Java套接字编程。在服务器端获取错误

时间:2018-03-30 21:54:11

标签: java sockets serialization client-server

我编写了一个java套接字编程,其中两个字符串将从客户端提供给服务器。服务器将返回连接的字符串和连接字符串的长度。 目标是在获得所需结果后将“exit”字符串发送到服务器以关闭连接。我得到了所需的结果(服务器发送连接的字符串和大小)但我在服务器端收到错误。

我也在服务器端获得了四份输入的字符串,而不是获得一份副本。 这是输出截图 enter image description here

这是我的服务器端代码:

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

           ServerSocket providerSocket;
           Socket connection = null;
           ObjectOutputStream out;
           ObjectInputStream in;
           String message;
           String x="";
           int count=0;
           Provider1(){}
           void run()
           {
                try{
                //1. creating a server socket
                providerSocket = new ServerSocket(2004, 10);
                //2. Wait for connection
                System.out.println("Waiting for connection");
                connection = providerSocket.accept();
                System.out.println("Connection received from " + connection.getInetAddress().getHostName());
                //3. get Input and Output streams
                out = new ObjectOutputStream(connection.getOutputStream());
                out.flush();
                in = new ObjectInputStream(connection.getInputStream());
                sendMessage("Connection successful");
              //4. The two parts communicate via the input and output streams
               do{
                      try{
                          message="";
                          message = (String)in.readObject();
                          System.out.println("client>" + message);
                          x+=message;
                          count++;
                          if(count==2)
                          {
                              sendMessage("The concatenated string : "+x);
                              sendMessage("The length of concatenated string : "+x.length());
                              message="exit";
                           }
                           if (message.equals("exit"))
                               sendMessage(message);
                       }
                       catch(ClassNotFoundException classnot){
                             System.err.println("Data received in unknown format");
                       }
              }while(!message.equals("exit"));
         }
         catch(IOException ioException){
               ioException.printStackTrace();
          }
          finally{
             //4: Closing connection
              try{
                    in.close();
                    out.close();
                    providerSocket.close();
                 }
                 catch(IOException ioException){
                      ioException.printStackTrace();
                 }
           }
     }
         void sendMessage(String msg)
         {
             try{
                out.writeObject(msg);
                out.flush();
              }
              catch(IOException ioException){
                   ioException.printStackTrace();
              }
          }
          public static void main(String args[])
          {
                Provider1 server = new Provider1();
                while(true)
                {
                     server.run();
                }
           }
   }

这是我的客户端代码

    import java.io.*;
    import java.net.*;
    public class Requester1{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
Requester1(){}
void run()
{
    try{
        //1. creating a socket to connect to the server
        requestSocket = new Socket("localhost", 2004);
        System.out.println("Connected to localhost in port 2004");
        //2. get Input and Output streams
        out = new ObjectOutputStream(requestSocket.getOutputStream());
        out.flush();
        in = new ObjectInputStream(requestSocket.getInputStream());
        //3: Communicating with the server
        do{
            try{
                message = (String)in.readObject();
                System.out.println("server>" + message);
                sendMessage("Hello");
                sendMessage("There");
                message = "exit";
                sendMessage(message);
            }
            catch(ClassNotFoundException classNot){
                System.err.println("data received in unknown format");
            }
        }while(!message.equals("exit"));
    }
    catch(UnknownHostException unknownHost){
        System.err.println("You are trying to connect to an unknown host!");
    }
    catch(IOException ioException){
        ioException.printStackTrace();
    }
    finally{
        //4: Closing connection
        try{
            in.close();
            out.close();
            requestSocket.close();
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
    }
}
void sendMessage(String msg)
{
    try{
        out.writeObject(msg);
        out.flush();
        //System.out.println("client>" + msg);
    }
    catch(IOException ioException){
        ioException.printStackTrace();
    }
}
public static void main(String args[])
{
    Requester1 client = new Requester1();
    client.run();
}
    }

1 个答案:

答案 0 :(得分:1)

读取循环继续,直到读取"bye",客户端关闭套接字而不发送它。所以这就是发生的事情。

您只需要通过显式捕获EOFException并在获取读取循环时终止读取循环,使您的阅读代码更加健壮。

您的客户端还需要阅读发送给它的消息。否则,您将激活连接重置。