我正在使用Java Socket实现 P2P多播聊天程序。首先,这是我的目标:在一个拥有许多客户的网络中,我希望每个客户能够与另一个客户进行1-1聊天(一次很多客户,但是1-1聊天仅)。 所以这是我目前的代码:
首先,在注册服务器后,客户端将启动它自己的服务器"用于处理来自其他客户的1-1连接
Thread starter = new Thread(new ClientListenerStart());
starter.start();
现在这是ClientListenerStart
类的代码:
public class ClientListenerStart implements Runnable {
public void run() {
currentConnectedClientsWriter = new ArrayList();
currentChattingUsers = new ArrayList<String>();
try {
// create a ServerSocket to listen to another clients' requests
ServerSocket serverSock = new ServerSocket(port);
while (true) {
Socket _clientSocket = serverSock.accept();
PrintWriter _clientWriter = new PrintWriter(_clientSocket.getOutputStream());
currentConnectedClientsWriter.add(_clientWriter);
Thread clientToClientHandler = new Thread(new ClientToClientHandler(_clientSocket, _clientWriter));
clientToClientHandler.start();
} // end while
} // end try // end try
catch (Exception ex) {
System.out.println("ERROR at server: ServerStart > run " + ex.toString());
} // end catch
} // end go()
}
接下来,当客户点击对方的客户时&#39; jList 中的名称,它为该被点击的客户端服务器创建一个套接字。从那时起,他们将通过该套接字进行沟通。
// this method start the listening server in each client
private void jListOnlineUsersMouseClicked(java.awt.event.MouseEvent evt) {
JList list = (JList) evt.getSource();
if (evt.getClickCount() == 2) {
// if double-click
jListOnlineUsersIndex = list.locationToIndex(evt.getPoint());
String targetUsername = onlineUsernames.get(jListOnlineUsersIndex);
String targetIP = onlineUserIPs.get(jListOnlineUsersIndex);
int targetPort = onlineUserports.get(jListOnlineUsersIndex);
try {
Socket clientSock = new Socket(targetIP, targetPort);
InputStreamReader streamReader = new InputStreamReader(clientSock.getInputStream());
BufferedReader newClientReader = new BufferedReader(streamReader);
PrintWriter newClientWriter = new PrintWriter(clientSock.getOutputStream());
// send chat request message
ClientChatRequestMsg clientChatRequestMsg = new ClientChatRequestMsg(username);
newClientWriter.println(clientChatRequestMsg.getXMLContent());
newClientWriter.flush(); // flushes the buffer
// assign targetUsername to currentRequestingUser - a global value
currentRequestingUser = targetUsername;
// create new thread for listening to the target client's responses
Thread ClientToClientHandler = new Thread(new ClientToClientHandler(clientSock, newClientWriter));
ClientToClientHandler.start();
} catch (Exception ex) {
System.out.println("ERROR at client: jListOnlineUsersMouseClicked " + ex.toString());
}
}
}
这是ClientToClientHandler
类的代码:
public class ClientToClientHandler implements Runnable {
Socket clientSock;
//declare buffer & writer used for communicate with CLIENT
BufferedReader clientReader;
PrintWriter clientWriter;
public ClientToClientHandler(Socket clientSocket, PrintWriter _clientPrintWriter) {
// new inputStreamReader and then add it to a BufferedReader
clientWriter = _clientPrintWriter;
try {
clientSock = clientSocket;
InputStreamReader isReader = new InputStreamReader(clientSock.getInputStream());
clientReader = new BufferedReader(isReader);
} // end try
catch (Exception ex) {
System.out.println("ERROR at client: ClientToClientHandler constructor " + ex.toString());
} // end catch
} // end ClientToClientHandler()
public void run() {
String stream;
try {
while ((stream = clientReader.readLine()) != null) {
System.out.println("CLIENT " + username +"'s stream from another client: " + stream);
switch (getMsgType(stream)) {
// this part handle the requestMsg from another client
}
}
} catch (Exception ex) {
System.out.println("ERROR at client: IncomingReaderClient > run " + ex.toString());
}
}
}
这是我目前的错误说明: 注册3个客户端:A,B和C与服务器:
客户端A正常连接并与客户端B聊天。 (1)
然后客户端A正常连接并与客户端C聊天。 (2)
我已经坚持这个问题几个小时,真的希望你们能帮忙!任何帮助将不胜感激。非常感谢先进!