如何在套接字编程中制作多条消息?
How to make multiple message in socket programming?
我有一个项目,我在其中制作了一个聊天服务器,人们(客户端)可以在其中连接并选择要与之聊天的伙伴。然而,只有当两个客户端都发送一条消息时,他们才能看到对方输入的内容,他们才会聊天。我想要的是像 Whatsapp 这样的东西,我们可以在不等待其他客户端发送的情况下一个接一个地发送许多消息。
我尝试创建一个线程 class 以调用聊天和其他内容,但从未成功。
//this is the client.java and this is the part of the code where they start chatting
do {
System.out.print(dis.readUTF());
System.out.println(dis.readUTF());
send = in.next();
dos.writeUTF(send);
b = dis.readBoolean();
} while (b);
//this is part of the chatserver.java where the connection is done they start chatting
class handleClient implements Runnable {
private Socket s1;
private Socket s2;
private String name1;
private String name2;
public handleClient(Socket s1, Socket s2, String n1, String n2) {
this.s1 = s1;
this.s2 = s2;
this.name1 = n1;
this.name2 = n2;
}
@Override
public void run() {
String msg1 = "", msg2 = "";
boolean b;
try {
DataOutputStream dos1 = new DataOutputStream(s1.getOutputStream());
DataInputStream dis1 = new DataInputStream(s1.getInputStream());
DataOutputStream dos2 = new DataOutputStream(s2.getOutputStream());
DataInputStream dis2 = new DataInputStream(s2.getInputStream());
do {
dos1.writeUTF(name1 + ": ");
dos2.writeUTF(name2 + ": ");
dos2.writeUTF(name1 + ": " + msg1);
msg1 = dis1.readUTF();
dos1.writeUTF(name2 + ": " + msg2);
msg2 = dis2.readUTF();
b = !msg1.equals(name1 + " is out") && !msg2.equals(name2 + " is out");
dos1.writeBoolean(b);
dos2.writeBoolean(b);
} while (b);
dos1.close();
dis1.close();
dos2.close();
dis2.close();
s1.close();
s2.close();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
}
正如@Mohsen fallahi 所说,使用 websockets(你得到了隐含的非阻塞功能)
对于纯 javascript 解决方案,请参阅:https://javascript.info/websocket
readUTF()
,嗯,读取一个UTF字符串。它不会 return 直到它读完一个。这就是为什么你想并行地从不同的客户端读取,所以哪一个首先发送消息并不重要,它会被读取并可以在之后转发,独立于其他人。一种方法是为每个客户端启动一个单独的 reader 线程。
这是一个简单的聊天室服务器(我不想处理选择客户端,抱歉):
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(5555);
List<DataOutputStream> doss = new ArrayList<>();
while (true) {
Socket client = server.accept();
synchronized(doss) {
doss.add(new DataOutputStream(client.getOutputStream()));
}
new Thread(new Runnable() {
public void run() {
try (DataInputStream dis = new DataInputStream(client.getInputStream())) {
while (true) { // <--------- per-client loop
String message=dis.readUTF();
synchronized (doss) {
for (DataOutputStream dos : doss)
dos.writeUTF(message);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}).start();
}
}
}
它有一个输出流的中央存储(doss
),每个客户端一个线程用于读取入站流。然后线程从他们的客户端读取一个字符串并将它转发到所有传出流,永远(内部 while(true)
循环)。
在客户端,循环实际上是 one-liners,一个用于读取消息并将其发送到服务器,另一个用于从服务器获取内容并将其打印在屏幕上:
public class ChatClient {
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
System.out.print("Enter your nick: ");
String name = s.nextLine();
Socket socket = new Socket("localhost", 5555);
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
new Thread(new Runnable() {
public void run() {
try {
while (true) // <------- receiver loop, in thread
System.out.println(dis.readUTF());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}).start();
dos.writeUTF(name + " has joined the conversation");
while (true) // <------- sender loop
dos.writeUTF(name + ": " + s.nextLine());
}
}
这个东西在端口 5555
上与硬编码 localhost
一起工作,根本不提供任何错误处理(当客户端离开时服务器死机)。本来是要短的。
我有一个项目,我在其中制作了一个聊天服务器,人们(客户端)可以在其中连接并选择要与之聊天的伙伴。然而,只有当两个客户端都发送一条消息时,他们才能看到对方输入的内容,他们才会聊天。我想要的是像 Whatsapp 这样的东西,我们可以在不等待其他客户端发送的情况下一个接一个地发送许多消息。
我尝试创建一个线程 class 以调用聊天和其他内容,但从未成功。
//this is the client.java and this is the part of the code where they start chatting
do {
System.out.print(dis.readUTF());
System.out.println(dis.readUTF());
send = in.next();
dos.writeUTF(send);
b = dis.readBoolean();
} while (b);
//this is part of the chatserver.java where the connection is done they start chatting
class handleClient implements Runnable {
private Socket s1;
private Socket s2;
private String name1;
private String name2;
public handleClient(Socket s1, Socket s2, String n1, String n2) {
this.s1 = s1;
this.s2 = s2;
this.name1 = n1;
this.name2 = n2;
}
@Override
public void run() {
String msg1 = "", msg2 = "";
boolean b;
try {
DataOutputStream dos1 = new DataOutputStream(s1.getOutputStream());
DataInputStream dis1 = new DataInputStream(s1.getInputStream());
DataOutputStream dos2 = new DataOutputStream(s2.getOutputStream());
DataInputStream dis2 = new DataInputStream(s2.getInputStream());
do {
dos1.writeUTF(name1 + ": ");
dos2.writeUTF(name2 + ": ");
dos2.writeUTF(name1 + ": " + msg1);
msg1 = dis1.readUTF();
dos1.writeUTF(name2 + ": " + msg2);
msg2 = dis2.readUTF();
b = !msg1.equals(name1 + " is out") && !msg2.equals(name2 + " is out");
dos1.writeBoolean(b);
dos2.writeBoolean(b);
} while (b);
dos1.close();
dis1.close();
dos2.close();
dis2.close();
s1.close();
s2.close();
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
}
正如@Mohsen fallahi 所说,使用 websockets(你得到了隐含的非阻塞功能)
对于纯 javascript 解决方案,请参阅:https://javascript.info/websocket
readUTF()
,嗯,读取一个UTF字符串。它不会 return 直到它读完一个。这就是为什么你想并行地从不同的客户端读取,所以哪一个首先发送消息并不重要,它会被读取并可以在之后转发,独立于其他人。一种方法是为每个客户端启动一个单独的 reader 线程。
这是一个简单的聊天室服务器(我不想处理选择客户端,抱歉):
public class ChatServer {
public static void main(String[] args) throws Exception {
ServerSocket server = new ServerSocket(5555);
List<DataOutputStream> doss = new ArrayList<>();
while (true) {
Socket client = server.accept();
synchronized(doss) {
doss.add(new DataOutputStream(client.getOutputStream()));
}
new Thread(new Runnable() {
public void run() {
try (DataInputStream dis = new DataInputStream(client.getInputStream())) {
while (true) { // <--------- per-client loop
String message=dis.readUTF();
synchronized (doss) {
for (DataOutputStream dos : doss)
dos.writeUTF(message);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}).start();
}
}
}
它有一个输出流的中央存储(doss
),每个客户端一个线程用于读取入站流。然后线程从他们的客户端读取一个字符串并将它转发到所有传出流,永远(内部 while(true)
循环)。
在客户端,循环实际上是 one-liners,一个用于读取消息并将其发送到服务器,另一个用于从服务器获取内容并将其打印在屏幕上:
public class ChatClient {
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(System.in);
System.out.print("Enter your nick: ");
String name = s.nextLine();
Socket socket = new Socket("localhost", 5555);
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
new Thread(new Runnable() {
public void run() {
try {
while (true) // <------- receiver loop, in thread
System.out.println(dis.readUTF());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}).start();
dos.writeUTF(name + " has joined the conversation");
while (true) // <------- sender loop
dos.writeUTF(name + ": " + s.nextLine());
}
}
这个东西在端口 5555
上与硬编码 localhost
一起工作,根本不提供任何错误处理(当客户端离开时服务器死机)。本来是要短的。