2011-11-10 8 views
1

オブジェクトストリームを使用したサーバクライアントチャットプログラムでは、どのクライアントにメッセージを送信することができますか?接続方法についての私のリスニングでクライアント/サーバチャットプログラムの作成

は、私がメッセージexhangesを処理するサーバ内のスレッドに、このクライアントを渡すその後、接続

public void listenForConnections() 
{ 
    String sUserName=""; 

    try{ 
     do { 
      System.out.println("Waiting on connections"); 
      Socket client = servSocket.accept(); 
      System.out.println("Connection From: " + client.getInetAddress());   


      //pass message handling to thread and listen 
      ClientHandler handler = new ClientHandler(client,this); 
      handler.start();//As usual, this method calls run.     

     } while (true); 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

を受け入れます。

i、e;

  //pass message handling to thread and listen 
      ClientHandler handler = new ClientHandler(client,this); 
      handler.start();//As usual, this method calls run. 

どのように、私は接続しているクライアントのリストを保持していますか?

私は、ユーザー名とObjectOutPutStreamのキーを持つhastableを考えました。そして接続が受け入れられた後に送信されているオブジェクトを読み取るが、私は問題に遭遇した。メッセージは、ユーザー名とコマンドを入力するログインコマンドでした。

私のコードは次のようになりました。

System.out.println( "接続待ち"); ソケットクライアント= servSocket.accept(); System.out.println( "接続元:" + client.getInetAddress());それが破損したストリームに関するエラーを出したので、私はコメントアウト

  ObjectOutputStream clientOOS = new ObjectOutputStream(client.getOutputStream()); 

      outputStreams.put(sUserName, oos); 


      //big problem here 
      //serializeation 
      /*ois = new ObjectInputStream(client.getInputStream()); 
      oos = new ObjectOutputStream(client.getOutputStream()); 

      //ask for the username /authenticate 
      System.out.println("SERVER: getting username"); 
      myMessage inMessageLogin = (myMessage) ois.readObject(); 

      if(inMessageLogin.getCOMMAND()==ServerCommands.CMD_LOGIN) 
      { 
       sUserName=inMessageLogin.getsUserName(); 
       System.out.println("SERVED User " + sUserName + " connected."); 
       //save stream 
       outputStreams.put(sUserName, oos); 
       //oos.close(); 
       //oos.flush(); 
       //ois.close(); 
       ois=null; 
       //oos=null; 
      } 
  //end of problem!!!!!*/ 

、任意のアイデア?

おかげ

クライアントからサーバーにメッセージを送信します。

//default cmd is to send to all 
public void sendMessage(String sText,int iCommand) 
{ 
    System.out.println("sendMessage"); 

    outMessage=new myMessage(); 

    outMessage.setsUserName(sCurrentUser); 
    //set command 
    outMessage.setCOMMAND(iCommand); 

    outMessage.setsMessage(sText); 

    System.out.println("send msg" + outMessage.displayMessage()); 

    try { 
     oos.writeObject(outMessage); 
     oos.flush(); 
     oos.reset(); 
     //clear up send message from txbox 
     txtMessage.setText(""); 
    } catch (IOException ex) { 
     Logger.getLogger(myClientGUI.class.getName()).log(Level.SEVERE, null, 

EX)。 } }

サーバーに接続するクライアントコード。

public void connectToServer() 
{ 
    String sServer=txtServer.getText(); 
    PORT=Integer.parseInt(txtPort.getText()); 
    try { 
     //host = InetAddress.getByName("localhost");//InetAddress.getLocalHost(); 
     host = InetAddress.getByName(sServer); 
     clientSocket = new Socket(host, PORT); 
    } 
    catch (Exception e){ 
     e.printStackTrace(); 
    } 
} 

public boolean createStreams() 
{ 
    try{ 
     //serial 
     //******************************************************************************* 
     // open I/O streams for objects - serialization streams 
     oos = new ObjectOutputStream(clientSocket.getOutputStream()); 
     ois = new ObjectInputStream(clientSocket.getInputStream()); 



     return true; 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
     return false; 
    } 
} 
+0

私が推測するには、ネットワークにコマンドを書き込み、クライアントコードを提供する必要があります。 –

+0

私はクライアントからsendメソッドを投稿しました –

+0

クライアントのコードでクライアント接続とObjectOutputStreamを作成するにはどうすればいいですか?このクライアントのコードも投稿できますか? –

答えて

0

次のコードは私のために完璧に適しています。 ServerクラスはクラスパスのMessageクラスにアクセスできる必要があり、MessageクラスはSerializableを実装していることに注意してください。

クライアント:

class Client { 
    private Socket clientSocket; 

    public void connectToServer() 
    { 
     String sServer="localhost"; 
     int PORT = 8181; 
     try { 
      InetAddress host = InetAddress.getByName(sServer); 
      clientSocket = new Socket(host, PORT); 
     } 
     catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 

    public void sendMessage(String sText,int iCommand) throws IOException { 
     Message outMessage = new Message(); 

     outMessage.setCOMMAND(iCommand); 
     outMessage.setsMessage(sText); 

     ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream()); 
     try { 
      oos.writeObject(outMessage); 
      oos.flush(); 
      oos.reset(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     Client c = new Client(); 
     c.connectToServer(); 
     try { 
      c.sendMessage("test message", 42); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

class Message implements Serializable { 
    private int iCommand; 
    private String sText; 

    public void setCOMMAND(int iCommand) { 
     this.iCommand = iCommand; 
    } 

    public void setsMessage(String sText) { 
     this.sText = sText; 
    } 

    @Override 
    public String toString() { 
     return "Message{" + 
       "iCommand=" + iCommand + 
       ", sText='" + sText + '\'' + 
       '}'; 
    } 
} 

サーバー:

class Server { 
    public static void main(String[] args) throws IOException { 
     ServerSocket serverSocket = new ServerSocket(8181); 

     do { 
      Socket s = serverSocket.accept(); 
      try { 
       processClient(s); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 
     } while (true); 
    } 

    private static void processClient(Socket s) throws IOException, ClassNotFoundException { 
     ObjectInputStream ois = new ObjectInputStream(s.getInputStream()); 
     Message message = (Message) ois.readObject(); 
     System.out.println(message.toString()); 
    } 
}