オブジェクトストリームを使用したサーバクライアントチャットプログラムでは、どのクライアントにメッセージを送信することができますか?接続方法についての私のリスニングでクライアント/サーバチャットプログラムの作成
は、私がメッセージ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; } }
私が推測するには、ネットワークにコマンドを書き込み、クライアントコードを提供する必要があります。 –
私はクライアントからsendメソッドを投稿しました –
クライアントのコードでクライアント接続とObjectOutputStreamを作成するにはどうすればいいですか?このクライアントのコードも投稿できますか? –