使用套接字的客户端 – 服务器 – 客户端通信

我正在构建一个小型聊天应用程序,其中客户端A希望通过服务器B向客户端C发送内容。 首先,这是解决问题的正确方法吗? 我能够向服务器发送数据和从服务器接收数据,但它仅限于客户端。例如,如果客户端A向服务器B发送数据并且客户端C正在向服务器B发送数据,那么我可以将数据发送回A和C就像一个echo服务器。 但我想要的是通过服务器B将来自客户端A的数据转发到客户端C.

以下是服务器代码:

public class Server { public static void main(String[] args) { int port = 666; //random port number try { ServerSocket ss = new ServerSocket(port); System.out.println("Waiting for a client...."); System.out.println("Got a client :) ... Finally, someone saw me through all the cover!"); System.out.println(); while(true) { Socket socket = ss.accept(); SSocket sSocket = new SSocket(socket); Thread t = new Thread(sSocket); t.start(); System.out.println("Socket Stack Size-----"+socketMap.size()); } } catch (Exception e) { } } } class SSocket implements Runnable { private Socket socket; public SSocket(Socket socket) { this.socket = socket; } @Override public void run() { try { InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); DataInputStream dIn = new DataInputStream(in); DataOutputStream dOut = new DataOutputStream(out); String line = null; while (true) { line = dIn.readUTF(); System.out.println("Recievd the line----" + line); dOut.writeUTF(line + " Comming back from the server"); dOut.flush(); System.out.println("waiting for the next line...."); } } catch (Exception e) { } } } 

客户端代码是:

 public class Client { public static void main(String[] args) { int serverPort = 666; try { InetAddress inetAdd = InetAddress.getByName("127.0.0.1"); Socket socket = new Socket(inetAdd, serverPort); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); DataInputStream dIn = new DataInputStream(in); DataOutputStream dOut = new DataOutputStream(out); BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks."); System.out.println(); String line = null; while (true) { line = keyboard.readLine(); System.out.println("Wrinting Something on the server"); dOut.writeUTF(line); dOut.flush(); line = dIn.readUTF(); System.out.println("Line Sent back by the server---" + line); } } catch (Exception e) { } } } 

当您的客户端连接到服务器时,您的服务器为它创建一个Socket ,这里是Socket socket = ss.accept(); ,你的socket变量将持有该客户端。

现在,如果您只是将您的客户端套接字添加到while循环中的arraylist ,您将拥有一个与您的服务器主动连接的客户端列表,如:

接受后:

 clients = new ArrayList(); Socket socket = ss.accept(); os = new DataOutputStream(socket.getOutputStream()); clients.add(os); 

现在,当您拥有该客户clients arraylist中的所有客户clients ,您可以遍历它,或者使用某些协议定义在读取后我应该发送数据的客户端。

 Iterator it = clients.iterator(); while ((message = reader.readLine()) != null) { //reading while (it.hasNext()) { try { DataOutputStream oss = it.next(); oss.write(message);//writing oss.flush(); } catch (Exception e) { } } } 

这将循环遍历arraylist中的所有可用客户端并将发送给所有客户端。 您可以定义仅发送给某些人的方式。

例如:维护一个ActiveClients arraylist并与某些GUI交互可能或者可能定义您想要发送消息的所有客户端。 然后将这些客户端outputStreams添加到ActiveClients

 ActiveClients.add(clients.get(2)); 

如果你不想要它们,请删除它们。

 ActiveClients.remove(clients.get(2)); 

现在只需循环遍历此arraylist即可发送上述数据。

您可以为每个客户端创建消息队列:

  1. 客户端A向服务器B发送带有地址Client C消息“Hi”。
  2. 服务器B接收消息并将其添加到客户端C的消息队列中。
  3. 服务器B中的线程与客户端C检查消息队列通信,检索消息并将其发送给客户端C.
  4. 客户端C接收消息。

如果我没有弄错,你必须遇到从服务器或SSocket类接收消息的问题。 您的代码会发生什么情况,当您从客户端向服务器发送消息时,Server类接收您的消息也会在客户端中显示消息的回显。 但是,从Server类发送消息时,您不会在客户端类中收到任何消息。

要使其工作,您必须以下列方式修改代码:

SSocket类

 String line = null; while (true) { line = dIn.readUTF(); System.out.println("Recievd the line----" + line); dOut.writeUTF(line + " Comming back from the server"); dOut.flush(); System.out.println("waiting for the next line...."); } 

你应该添加这些行:

 String Line2 = take.nextLine(); //The user types a message for the client dOut.writeUTF(Line2 + " Comming back from the server"); //The message is sent to the client 

用这个替换while循环,它将正常工作。

 while (true) { line = dIn.readUTF(); //Takes the msg from the client System.out.println("Recievd the line----" + line); //Prints the taken message String Line2 = take.nextLine(); //The user types a message for the client dOut.writeUTF(Line2 + " Comming back from the server"); //The message is sent to the client dOut.flush(); System.out.println("waiting for the next line...."); }