2016-02-06 276 views
0

我正在尝试创建一个客户端可以向服务器发送一些int值的应用程序(带游戏),直到未达到某个值,客户端可以与服务器进行交换,然后将值发回。TCP服务器和客户端:服务器响应客户端时引发IOException

我的第一堂课是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

另一方面,在客户端上我有几乎相同的代码,直到工作正常对象游戏应返回:

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

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

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()在我的客户端类从服务器获取数据?这是错误的。

回答

3

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

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