Java 坚持使用多客户端聊天应用程序
我正在尝试制作一个多客户端聊天应用程序,其中聊天在客户端窗口中实现。我已经尝试了同样的服务器和客户端代码。我有两个问题: 答:我相信代码应该可以工作,但是,服务器到客户端的连接很好,但是信息不会在客户端之间传输。 B.我需要一种实现私有一对一聊天的方法,如果有两个以上的客户端,我使用了一个类来存储为每个正在建立的连接返回的套接字对象的信息,但我不知道如何实现它 服务器代码是:Java 坚持使用多客户端聊天应用程序,java,multithreading,swing,sockets,Java,Multithreading,Swing,Sockets,我正在尝试制作一个多客户端聊天应用程序,其中聊天在客户端窗口中实现。我已经尝试了同样的服务器和客户端代码。我有两个问题: 答:我相信代码应该可以工作,但是,服务器到客户端的连接很好,但是信息不会在客户端之间传输。 B.我需要一种实现私有一对一聊天的方法,如果有两个以上的客户端,我使用了一个类来存储为每个正在建立的连接返回的套接字对象的信息,但我不知道如何实现它 服务器代码是: import java.io.*; import java.net.*; class ClientInfo { So
import java.io.*;
import java.net.*;
class ClientInfo {
Socket socket;
String name;
public ClientInfo(Socket socket, String name) {
this.socket = socket;
this.name = name;
}
}
public class server {
private ObjectInputStream input[] = null;
private ObjectOutputStream output[] = null;
private String value = null;
private static ServerSocket server;
private Socket connection = null;
private static int i = -1;
public static void main(String args[]) {
try {
server = new ServerSocket(1500, 100);
while (true) {
System.out.println("Waiting for connection from client");
Socket connection = server.accept();
i++;
System.out.println("Connection received from " + (i + 1) + " source(s)");
//System.out.println(i);
new ClientInfo(connection, "Client no:" + (i + 1));
innerChat inc = new server().new innerChat(connection);
}
} catch (Exception e) {
System.out.println("Error in public static void main! >>>" + e);
}
}// end of main!!!
class innerChat implements Runnable {
private Socket connection = null;
public innerChat(Socket connection) {
this.connection = connection;
Thread t;
t = new Thread(this);
t.start();
}
public void run() {
try {
output[i] = new ObjectOutputStream(connection.getOutputStream());
output[i].flush();
input[i] = new ObjectInputStream(connection.getInputStream());
} catch (Exception e) {
}
}
}
}
而客户端代码是
import java.net.*;
import java.io.*;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.event.*;
public class ChatappClient {
private static int port = 1500;
JFrame window = new JFrame("Chat");
JButton sendBox = new JButton("Send");
JTextField inputMsg = new JTextField(35);
JTextArea outputMsg = new JTextArea(10, 35);
private ObjectInputStream input;
private ObjectOutputStream output;
public static void main(String[] args) throws Exception {
ChatappClient c = new ChatappClient();
c.window.setVisible(true);
c.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.run();
}
public ChatappClient() {
inputMsg.setSize(40, 20);
sendBox.setSize(5, 10);
outputMsg.setSize(35, 50);
inputMsg.setEditable(true);
outputMsg.setEditable(false);
window.getContentPane().add(inputMsg, "South");
window.getContentPane().add(outputMsg, "East");
window.getContentPane().add(sendBox, "West");
window.pack();
sendBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
output.writeObject(inputMsg.getText());
outputMsg.append("\n" + "Client>>>" + inputMsg.getText());
output.flush();
} catch (IOException ie) {
outputMsg.append("Error encountered! " + ie);
}
inputMsg.setText("");
}
});
inputMsg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
output.writeObject(inputMsg.getText());
outputMsg.append("\n" + "Client>>>" + inputMsg.getText());
output.flush();
} catch (IOException ie) {
outputMsg.append("Error encountered! " + ie);
}
inputMsg.setText("");
}
});
}
private void run() throws IOException {
Socket clientSocket = new Socket("127.0.0.1", port);
output = new ObjectOutputStream(clientSocket.getOutputStream());
output.flush();
input = new ObjectInputStream(clientSocket.getInputStream());
outputMsg.append("I/O Success");
String value = null;
while (true) {
try {
value = (String) input.readObject();
} catch (Exception e) {
}
outputMsg.append(value + "\n");
}
}
}
您的代码看起来可以改进很多。例如,您的主方法中不应该包含这些代码。它应该启动您的服务器类,就是这样(请注意,根据我强烈建议您遵循的Java标准,类名应该以大写字母开头) 我正在尝试制作一个多客户端聊天应用程序,其中服务器除了侦听和创建连接之外什么都不做 服务器将不得不做更多的事情。它将需要创建客户端,它将需要维护一个集合,例如客户端的ArrayList,例如
ArrayList
,否则您希望服务器如何将两个客户端连接在一起
我认为在开始编写代码之前,您需要更深入地思考这个程序的结构和连接。您的代码看起来可以改进很多。例如,您的主方法中不应该包含这些代码。它应该启动您的服务器类,就是这样(请注意,根据我强烈建议您遵循的Java标准,类名应该以大写字母开头) 我正在尝试制作一个多客户端聊天应用程序,其中服务器除了侦听和创建连接之外什么都不做 服务器将不得不做更多的事情。它将需要创建客户端,它将需要维护一个集合,例如客户端的ArrayList,例如
ArrayList
,否则您希望服务器如何将两个客户端连接在一起
我认为,在开始编写代码之前,您需要更深入地思考这个程序的结构和连接。如果您能解释一下所需的更改,请再解释一下。谢谢。如果你能再解释一下需要做的改动。谢谢。请看一下。请看一下