我正在构建一个小型聊天应用程序,其中客户端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) { }
}
}
答案 0 :(得分:5)
当您的客户端连接到服务器时,您的服务器会为其创建Socket
,此处为Socket socket = ss.accept();
,您的套接字变量将持有该客户端。
现在,如果您继续将您的客户端套接字添加到arraylist
循环中的while
,您将获得与您的服务器主动关联的客户端列表,如:
接受后:
clients = new ArrayList<DataOutputStream>();
Socket socket = ss.accept();
os = new DataOutputStream(socket.getOutputStream());
clients.add(os);
现在你拥有clients
arraylist中的所有客户端,你可以遍历它,或者通过一些协议定义我应该在阅读后发送数据的客户端。
Iterator<DataOutputStream> 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交互来定义您要发送消息的所有客户端。
然后将这些客户端outputStream添加到ActiveClients
ActiveClients.add(clients.get(2));
或删除它们,如果你不想要它们。
ActiveClients.remove(clients.get(2));
现在只需遍历此arraylist
即可发送上述数据。
答案 1 :(得分:1)
您可以为每个客户创建消息队列:
Client C
的消息“嗨”。答案 2 :(得分:0)
如果我没有弄错,你必须遇到从服务器或SSocket类接收消息的问题。您的代码会发生什么情况,当您从客户端向服务器发送消息时,Server类接收您的消息也会在客户端中显示消息的回显。但是,当您从Server类发送消息时,您不会在客户端类中收到任何消息。
要使其正常工作,您必须以下列方式修改代码:
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....");
}