【问题标题】:Java Socket Programming. Getting error at the server side [closed]Java 套接字编程。在服务器端出错[关闭]
【发布时间】:2018-09-09 22:28:33
【问题描述】:

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

我还在服务器端获得了四份我输入的字符串,而不是一份。 这是输出屏幕截图

这是我的服务器端代码:

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

【问题讨论】:

标签: java sockets serialization client-server


【解决方案1】:

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

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

您的客户还需要阅读发送给它的消息。否则会引发连接重置。

【讨论】:

  • @Piotr 如果这是一条评论,则应将其发送给 OP。如果是答案,则应将其发布为答案。如果这是对我的回答的评论,那就是多余的。
  • @Piotr 我的回答中没有“它想发送响应但没有人在听”部分。那是你的话。这是您的评论中的一个部分。所以你的评论是你评论的延伸?所以是你自己的评论不清楚吗?这没有任何意义。而且和这里的实际问题没有任何关系。
  • 正确,复制和粘贴错误的片段。我想粘贴这个“客户端还需要阅读发送给它的消息”。我认为 user3804152 可能不清楚客户端和服务器之间的确切消息流是什么,以及它需要读取正在发送的消息意味着什么(鉴于他/她的代码通常质量低,我假设需要一个广泛的例子来解释问题)。如果您认为有必要,请随意对我的评论投反对票:-)
  • @Piotr '阅读意味着什么' 不是一个需要回答的问题。他已经在阅读现有代码中的消息。
猜你喜欢
  • 2016-02-06
  • 2015-04-07
  • 2013-01-16
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多