Java:客户端服务器,聊天广播
我正在为一个大学项目开发一个客户机-服务器聊天程序,我几乎没有编程背景。我上过三门课:ChatClient、ChatServer和ChatServerThread。目前我可以让多个客户机随时连接并与服务器通话 尽管我遇到的最大困难之一是:“从一个客户机键入的任何消息都将发送到所有其他客户机”以及“应显示发送和接收的消息” 在过去的几天里,我一直在努力让这个额外的功能正常工作,但运气不佳 我已经阅读并四处寻找了一段时间,但我很难将在线示例应用到我的工作中。我读到我应该创建一个套接字列表,然后遍历该列表并将数据发送给列表中的每个人,这在我的头脑中是有意义的,但在我尝试实现它时让我头疼。这方面的任何帮助都会非常感谢。如果有人能给我一些关于如何加密发送数据的见解,我会额外加分 聊天客户端Java:客户端服务器,聊天广播,java,client-server,chat,Java,Client Server,Chat,我正在为一个大学项目开发一个客户机-服务器聊天程序,我几乎没有编程背景。我上过三门课:ChatClient、ChatServer和ChatServerThread。目前我可以让多个客户机随时连接并与服务器通话 尽管我遇到的最大困难之一是:“从一个客户机键入的任何消息都将发送到所有其他客户机”以及“应显示发送和接收的消息” 在过去的几天里,我一直在努力让这个额外的功能正常工作,但运气不佳 我已经阅读并四处寻找了一段时间,但我很难将在线示例应用到我的工作中。我读到我应该创建一个套接字列表,然后遍历该
import java.net.*;
import java.io.*;
public class ChatClient {
private Socket socket = null;
private DataInputStream console = null;
private DataOutputStream streamOut = null;
private String myName = null;
private BufferedReader StreamIn = null;
private String response = null;
public ChatClient(String serverName, int serverPort) {
try {
console = new DataInputStream(System.in);
System.out.println("What is your name?");
myName = console.readLine();
System.out.println(myName + " <" + InetAddress.getLocalHost() + "> ");
} catch (IOException ioe) {
System.out.println("Unexpected exception: " + ioe.getMessage());
}
System.out.println("Establishing connection. Please wait ...");
try {
socket = new Socket(serverName, serverPort);
System.out.println("Connected: " + socket);
StreamIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
streamOut = new DataOutputStream(socket.getOutputStream());
streamOut.writeUTF(":" + myName + " <" + InetAddress.getLocalHost() + "> HAS JOINED");
streamOut.flush();
} catch (UnknownHostException uhe) {
System.out.println("Host unknown: " + uhe.getMessage());
} catch (IOException ioe) {
System.out.println("Unexpected exception: " + ioe.getMessage());
}
String line = "";
while (!line.equals(".bye")) {
try {
line = console.readLine();
streamOut.writeUTF(myName + " <" + InetAddress.getLocalHost() + "> : " + line);
streamOut.flush();
} catch (IOException ioe) {
System.out.println("Sending error: " + ioe.getMessage());
}
}
}
public void stop() {
try {
if (console != null) console.close();
if (streamOut != null) streamOut.close();
if (socket != null) socket.close();
} catch (IOException ioe) {
System.out.println("Error closing ...");
}
}
public static void main(String args[]) {
ChatClient client = null;
if (args.length != 2)
System.out.println("Usage: java ChatClient host port");
else
client = new ChatClient(args[0], Integer.parseInt(args[1]));
}
}
加密数据是(某种程度上)直截了当…您只需使用SSL套接字而不是常规TCP套接字。您可以获得一些非常好的信息。如果您对代码进行了注释并详细说明了您正在执行的操作,这将对我有所帮助。粗略地看一眼,我不明白多个客户端如何连接到您的服务器。您有
addThread(server.accept());
但服务器一次只能接受一个连接(在一个端口上)。您在哪里关闭此连接以接受更多连接?服务器如何与每个客户端保持联系(即,在哪里为每个新客户端建立另一个连接)?我可能也会对您的体系结构感到困惑。您是在做点对点客户端,即每个客户端都相互连接,还是在做更多的客户端-服务器模型(这是我的假设),其中每个客户端连接到服务器,服务器充当每个客户端的代理,即客户端将其文本发送到服务器,然后服务器将消息分发给每个客户端。请看我的帖子,我在同一上下文中回答了。我在那里一步一步地解释了它。请看输出。
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer implements Runnable {
private ServerSocket server = null;
private Thread thread = null;
private ChatServerThread client = null;
private String clientSentence = null;
private int peers = 0;
private List clients = new ArrayList();
final List sockets = new ArrayList();
public ChatServer(int port) {
try {
System.out.println("Binding to port " + port + ", please wait ...");
server = new ServerSocket(port);
System.out.println("Server started: " + server);
start();
} catch (IOException ioe) {
System.out.println(ioe);
}
}
public void run() {
while (thread != null) {
try {
System.out.println("Waiting for a client ...");
addThread(server.accept());
} catch (IOException ie) {
System.out.println("Acceptance Error: " + ie);
}
}
}
public void addThread(Socket socket) {
System.out.println("Client accepted: " + socket);
client = new ChatServerThread(this, socket);
try {
client.open();
client.start();
} catch (IOException ioe) {
System.out.println("Error opening thread: " + ioe);
}
}
public void start() {
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
public void stop() {
if (thread != null) {
thread.stop();
thread = null;
}
}
public void increment(String sentence) {
peers++;
String[] info = sentence.split(" ");
String name = info[0].replace(":", "");
System.out.println(name + " Has joined the room, we now have " + peers + " peer(s).");
clients.add(name);
}
public Boolean isAllowed(String name, Socket socket) {
try {
String stringSearch = name;
BufferedReader bf = new BufferedReader(new FileReader("allowed.txt"));
int linecount = 0;
String line = "";
System.out.println("Searching for " + stringSearch + " in file...");
while ((line = bf.readLine()) != null) {
linecount++;
String[] words = line.split(" ");
for (String word : words) {
if (word.equals(stringSearch)) {
System.out.println("User is allowed");
registerSocket(socket);
return true;
}
}
}
bf.close();
} catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
}
System.out.println("User is not allowed");
return false;
}
public void showAll() {
for (int i = 0; i < clients.size(); i++) {
System.out.print(clients.get(i));
}
}
public void registerSocket(Socket socket) {
//socket = new DataOutputStream(socket.getOutputStream());
sockets.add(socket);
for (int i = 0; i < sockets.size(); i++) {
System.out.println(sockets.get(i));
}
}
public static void main(String args[]) {
ChatServer server = null;
if (args.length != 1)
System.out.println("Usage: java ChatServer port");
else
server = new ChatServer(Integer.parseInt(args[0]));
}
}
import java.net.*;
import java.io.*;
public class ChatServerThread extends Thread {
private Socket socket = null;
private ChatServer server = null;
private int ID = -1;
private DataInputStream streamIn = null;
private String clientSentence = null;
public String newGuy = null;
DataOutputStream streamOut = null;
public ChatServerThread(ChatServer _server, Socket _socket) {
server = _server;
socket = _socket;
ID = socket.getPort();
}
public void run() {
System.out.println("Server Thread " + ID + " running.");
while (true) {
try {
String sentence = streamIn.readUTF();
//System.out.println(sentence);
char c = sentence.charAt(0);
String[] command = null;
command = sentence.split(" ");
String name = command[0].substring(1);
System.out.println("Sending out: " + sentence + " via ");
streamOut.writeBytes(sentence);
if (c == ':') {
if (server.isAllowed(name, socket))
server.increment(sentence);
else {
close();
}
}
} catch (IOException ioe) {
}
}
}
public void open() throws IOException {
streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
}
public void close() throws IOException {
if (socket != null) socket.close();
if (streamIn != null) streamIn.close();
}
}