Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java TCP服务器&;客户端:服务器响应客户端时引发IOException_Java_Sockets_Tcp - Fatal编程技术网

Java TCP服务器&;客户端:服务器响应客户端时引发IOException

Java TCP服务器&;客户端:服务器响应客户端时引发IOException,java,sockets,tcp,Java,Sockets,Tcp,我正在尝试创建一个应用程序(带有一个游戏),客户端可以向服务器发送一些int值,在未达到某个值之前,客户端可以与服务器交换,并将值发送回服务器 我的第一堂课是TCP应用程序的服务器。这里我有一个main()方法,它一直运行到游戏结束。运行以从客户端获取对象的getMoveFromClient()方法,以及将对象发送到客户端的sendRequestToClient(Game g,int-Response)。代码如下: public static void main(String[] args) {

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

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

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:软件导致的连接中止:套接字写入错误

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

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;
}
我有以下错误:
接收错误:套接字已关闭

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

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()
才能从服务器获取数据?这是错误的

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


在客户端中使用
accept()
在术语上是矛盾的。

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

在客户机中使用
accept()