我已经制作了一个基本的聊天应用程序,可以在同一台PC上正常运行,但我希望它能在不同的机器上运行。
为简单起见,我省略了GUI代码。我从一个单独的应用程序接收IP,名称和端口,该应用程序将其传递给客户端的构造函数。我已经输入了运行服务器的机器的IP和端口。
客户端
public class ClientGui extends JFrame implements Runnable, KeyListener, ActionListener
{
private JPanel contentPane;
private Socket socket;
private String name , ip;
private int port;
private JTextField input;
private JTextArea console;
private JButton send;
private String message;
private DateFormat format;
private BufferedReader in;
private PrintWriter out;
private String reply;
public ClientGui(String name, String ip, int port)
{
format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
this.name = name;
this.ip = ip;
this.port = port;
new Thread(this).start();
}
public void run()
{
try {
socket = new Socket(ip, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
while (true) {
out.println(name);
reply = in.readLine();
if (reply.equals("NAME")) {
name = JOptionPane.showInputDialog("Name taken , Enter another");
} else {
input.setEditable(true);
setTitle(name);
break;
}
}
while (true) {
reply = in.readLine();
if (reply.startsWith("MESSAGE")) {
log(reply.substring(7));
}
}
} catch (Exception e) {
try {
out.close();
in.close();
socket.close();
} catch (Exception e2) {
dispose();
System.exit(0);
}
dispose();
System.exit(0);
}
}
}
服务器
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashSet;
public class Server implements Runnable
{
private static ArrayList<PrintWriter> writers;
private static HashSet<String> names;
private Socket socket;
private String name;
private String message;
public Server(Socket socket)
{
this.socket = socket;
}
public void run()
{
PrintWriter temp = null;
try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
try (PrintWriter out = new PrintWriter(socket.getOutputStream(), true)) {
while (true) {
name = in.readLine();
if (names.contains(name)) {
out.println("NAME");
} else {
out.println("NAMEACCEPTED");
names.add(name);
break;
}
}
writers.add(out);
temp = out;
while(true) {
message = in.readLine();
for (PrintWriter writer : writers) {
writer.println("MESSAGE" + name + " : " + message);
}
}
}
} catch (Exception e) {
try {
names.remove(name);
writers.remove(temp);
socket.close();
} catch (Exception ignored) {
}
}
}
public static void main(String[] args)
{
writers = new ArrayList<>();
names = new HashSet<>();
ServerSocket server = null;
try {
int port = Integer.parseInt(args[0]);
server = new ServerSocket(port);
} catch (Exception e) {
System.out.println("Enter a valid port");
System.exit(0);
}
while (true) {
try {
System.out.println("Waiting for clients....");
new Thread(new Server(server.accept())).start();
System.out.println("Client Received");
} catch (Exception e) {
System.out.println("Server can't accept clients");
break;
}
}
}
}
答案 0 :(得分:1)
我认为可能是因为 1)您尚未在两台设备上安装rigth jre-Version 2)您不在同一个网络中,或者您没有转发过Ip
您是否尝试在虚拟框中使用它?
(client1 == vbox1,Server == Computer,client2 == vbox2)
希望,这有助于
答案 1 :(得分:0)
很抱歉给所有人带来不便
问题在于我在切换它时使用的端口,它在不同的机器上工作
感谢您的所有帮助