【问题标题】:TCP Server & Client : IOException raised when Server respond to ClientTCP 服务器和客户端:服务器响应客户端时引发 IOException
【发布时间】:2016-02-07 08:47:08
【问题描述】:

我正在尝试创建一个应用程序(带有游戏),其中客户端可以向服务器发送一些 int 值,并且在未达到某个值之前,客户端可以与发送值的服务器进行交换背部。

我的第一堂课是 TCP 应用程序的服务器。这里我有一个main() 方法,它一直运行到游戏结束。运行以从客户端获取对象的 getMoveFromClient() 方法,以及将对象发送到客户端的 sendRequestToClient(Game g, int reponse)。这是代码:

public static void main(String[] args) {
    serverLife = true;
    cm = new ConnectionManagerServer(); // this one instanciates my Server options
    Game g;
    while (serverLife) { // boolean value that allows me to continue over
        g = getMoveFromClient(); // get data from client, here every thing is ok 
        sendRequestToClient(g, 1); // send data to client, here it crashes.
        serverLife = g.life; // the object has a parameter that tells if the value is reached or not. (end of the game)
    }
}

public static Game getMoveFromClient() {
    // this method get data from clients, and works fine.
}

到这里为止,一切正常。但是使用这种方法,将数据发送到客户端:

private static void sendRequestToClient(Game g, int reponse) {
    try {
        g.setResponse(reponse);
        OutputStream out = cm.socket.getOutputStream();
        try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
            oos.writeObject(g); 
            oos.flush();
            oos.close();
        }
    } catch (IOException ex) {
        System.out.println("OutpuStreamError : " + ex.getMessage());
    }
}

我有以下错误:OutpuStreamError : Software caused connection abort: socket write error

另一方面,在客户端,我有几乎相同的代码,在应该返回对象 Game 之前工作正常:

public static Game getRequestFromServer() {
    Game g = null;
    try {
        InputStream in = mc.socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(in);
        g = (Calcul) ois.readObject();
    } catch (IOException ex) {
        System.out.println("error reception" + ex.getMessage());
    } catch (ClassNotFoundException ex) {
        System.out.println("erreur lecture de l'objet" + ex.getMessage());
    }
    return jeu;
}

我有以下错误:error reception : Socket is closed

我的 Game 对象有一个类,另外两个用于处理客户端和服务器的连接端口和套接字。

public ConnectionManagerServer() {
    try {

        this.serverPort = 6464;
        this.serverSocket = new ServerSocket(this.serverPort);
        this.socket = this.serverSocket.accept();
    } catch (IOException ex) {
        System.out.println("serverSocket probleme d'initialisation : " + ex.getMessage());
    }
}

第二个:

 public ConnectionManagerClient() {
    try {
        this.hostAdress = InetAddress.getByName("localhost");             // adresse du serveur 
        this.serverPort = 6464;
        this.socket = new Socket(this.hostAdress, this.serverPort);

    } catch (UnknownHostException ex) {
        System.out.println("Erreur d'initalisation de l'adresse de l'hote : " + ex.getMessage());
    } catch (IOException ex) {
        System.out.println("Erreur d'initalisation de la connexion : " + ex.getMessage());

    }
}

我不明白的是,当我尝试从客户端发送到服务器时,它工作正常,并且服务器能够读取对象内容,但是当我尝试将它从服务器发送到客户端,我无法从服务器读取对象。是因为我没有打开插座吗?

编辑: 我必须在我的客户端类中使用accept() 来从服务器获取数据吗?这是错误的。

【问题讨论】:

    标签: java sockets tcp


    【解决方案1】:

    关闭套接字的输入流或输出流会关闭套接字。不要关闭流,只需刷新对象输出流。并且在套接字的整个生命周期中使用相同的流,而不是每条消息的新流。

    在客户端中使用accept() 是自相矛盾的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 2012-01-04
      • 2013-02-17
      • 2012-11-03
      相关资源
      最近更新 更多