【发布时间】:2020-12-15 06:04:04
【问题描述】:
我们用Java制作了socket聊天程序。但是当有 2 个或更多聊天成员时,我们会遇到一个问题。如果有成员A、B、C(按字母顺序输入),当A(或B)离开房间时,最后一个成员(C)与A(或B)一起离开房间,没有错误。如果有成员 D,则前一个成员离开房间,最后一个成员(本例是成员 D)与前一个成员一起离开房间,没有错误。我们该如何解决?
//处理发生的错误//
A 进入 > B 进入 > C 进入 > D 进入 > A 或 B 或 C 离开(D 也离开了,为什么???)只有最后一个成员与前一个成员离开
服务器
public class Server {
public static final int PORT = 7777;
private ServerSocket serverSocket;
private Socket socket;
private ServerGUI gui;
private String msg;
private int count = 0;
private int NMC=0;
private Map<String, DataOutputStream> clientsMap = new HashMap<String, DataOutputStream>();
public final void setGUI(ServerGUI gui) {
this.gui = gui;
}
public void setting() throws IOException {
Collections.synchronizedMap(clientsMap);
serverSocket = new ServerSocket(PORT);
String hostAddress = InetAddress.getLocalHost().getHostAddress();
while (true) {
System.out.println("waiting connection - " + hostAddress + ":" + PORT);
socket = serverSocket.accept();
System.out.println("connected from" + socket.getInetAddress());
Receiver receiver = new Receiver(socket);
receiver.start();
}
}
public void addClient(String name, DataOutputStream out) throws IOException {
clientsMap.put(name, out);
sendMessage(name + "entered\n");
gui.appendMsg(name + "entered\n");
count++;
gui.appendMsg("joining member : " + count + "\n");
}
public void sendMessage(String msg) {
Iterator<String> member = clientsMap.keySet().iterator();
String key = "";
while (member.hasNext()) {
key = member.next();
try {
clientsMap.get(key).writeUTF(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Receiver extends Thread {
private DataInputStream in;
private DataOutputStream out;
private String name;
public Receiver(Socket socket) throws IOException {
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
name = in.readUTF();
if (count != 0) {
NMC=NameMatch(name);
}
if (NMC == 1 || count == 0) {
addClient(name, out);
} else {
out.writeUTF("not available");
socket.close();
out.close();
in.close();
}
}
public void run() {
try {
while (in != null) {
msg = in.readUTF();
sendMessage(msg);
gui.appendMsg(msg);
}
} catch (IOException e) {
try {
if (NMC == 0) {
socket.close();
in.close();
out.close();
NMC++;
} else {
socket.close();
in.close();
out.close();
clientsMap.remove(name);
count--;
gui.appendMsg(name + "has left.\n");
gui.appendMsg("joining member : " + count + "\n");
sendMessage(name + "has left.\n");
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
public int NameMatch(String name) {
Iterator<String> member = clientsMap.keySet().iterator();
String key = "";
while (member.hasNext()) {
int i = 0;
if (i == count) {
break;
}else{
key = member.next();
if (key.equals(name)) {
return 0;
}
}
i++;
}
return 1;
}
public static void main(String[] args) throws IOException {
Server server = new Server();
server.setting();
}
}
''' 客户
package chat.client;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JOptionPane;
public class Client {
private Socket socket;
private DataInputStream in;
private DataOutputStream out;
private ClientGUI gui;
private String msg;
private String Name;
private int PORT = 0;
private String IP = "";
public final void setGUI(ClientGUI gui) {
this.gui = gui;
}
class connect extends Thread {
public connect() {
try {
socket = new Socket(IP, PORT);
System.out.println("Connected.");
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
out.writeUTF(Name);
if (in.readUTF().equals("not available")) {
out.close();
in.close();
socket.close();
JOptionPane.showMessageDialog(null, "not available");
System.exit(1);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
try {
while (in != null) {
msg = in.readUTF();
gui.appendMsg(msg);
}
} catch (IOException e) {
try {
out.close();
in.close();
socket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
public static void main(String[] args) throws IOException {
Client client = new Client();
client.setConnect();
}
public void setConnect() {
connect c = new connect();
c.start();
}
public void sendMessage(String msg) {
try {
out.writeUTF(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
public void setNickname(String Name) {
this.Name = Name;
}
public void Port(int port) {
this.PORT = port;
}
public void Ip(String ip) {
this.IP = ip;
}
}
【问题讨论】:
标签: java swing sockets tcp network-programming