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

我正在构建一个小型聊天应用程序,其中客户端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);

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

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中的所有可用客户端并将发送给所有客户端.您可以定义仅发送给某些人的方式.

例如:

维护一个ActiveClient arraylist并与一些GUI交互可能或者可能定义你想要发送消息的所有客户端.

然后将这些客户端outputStreams添加到ActiveClients

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

或者删除它们,如果你不想要它们.

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

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

标签:java,sockets

来源: https://codeday.me/bug/20190530/1183519.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值