gpt4 book ai didi

java 聊天应用程序无法在其他机器上运行

转载 作者:行者123 更新时间:2023-11-30 07:14:51 24 4
gpt4 key购买 nike

我制作了一个基本的聊天应用程序,它可以在同一台 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;
}
}
}
}

最佳答案

我认为可能是这样,因为1) 您尚未在两台设备上安装正确的 jre 版本2)您不在同一个网络中或者您没有转发您的IP

您尝试过在虚拟盒子中使用它吗?

(客户端1 == vbox1,服务器==计算机,客户端2 == vbox2)

希望这有帮助

关于java 聊天应用程序无法在其他机器上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38588004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com