我要把我的插座改成什么?

发布于 2024-12-27 13:17:57 字数 1829 浏览 2 评论 0原文

我创建了一个简单的基于文本的角色扮演游戏,经过大量工作后设法将其放在网站上(www.worldofthedrakon.com)。我刚刚创建了服务器和客户端,但用户从计算机访问服务器时遇到问题。我的套接字设置为:

Socket socket = new Socket("localhost", 8800);

现在我已经测试了将 localhost 更改为我的 IP,但无济于事。我收到的错误是连接超时和连接被拒绝。有人能指出我正确的方向吗?如果我的问题看起来含糊不清,我深表歉意,可以提供更多代码。有很多,所以我不想轰炸你:) 谢谢。 服务器端:

public Server() {
        setLayout(new BorderLayout());
        add(new JScrollPane(jta), BorderLayout.CENTER);

        setTitle("Multi-Thread Server");
        setSize(500, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        try {
            ServerSocket serverSocket = new ServerSocket(8800);
            jta.append("MultiThreadServer started at " + new Date() + '\n');

            int clientNo = 1;

            while(true) {
                Socket socket = serverSocket.accept();
                jta.append("Server Thread for client " + clientNo + " at " + new Date() + '\n');

                InetAddress inetAdress = socket.getInetAddress();
                jta.append("Client " + clientNo + "'s host name is " + inetAdress.getHostName() + "\n");
                jta.append("Client " + clientNo + "'s IP Address is " + inetAdress.getHostAddress() + "\n");

                HandleAClient task = new HandleAClient(socket);

                new Thread(task).start();

                clientNo++;
            }
        } catch(IOException ex) {
            System.err.println(ex);
        }

客户端:

try {
            Socket socket = new Socket("localhost", 8800);

            fromServer = new DataInputStream(socket.getInputStream());
            toServer = new DataOutputStream(socket.getOutputStream());
        } catch (IOException ex) {
            jta_TextArea.setText(ex.toString() + '\n');
        }

alI created a simple text based RPG and after alot of work managed to put it on a website (www.worldofthedrakon.com). I just created a server and client but I am having problems with users accessing the server from their computer. I have my socket set up as:

Socket socket = new Socket("localhost", 8800);

Now i have tested changing localhost out for my IP, to no avail. The errors I'm getting are connection timed out, and connection refused. Could someone point me in the right direction? I apologize if my problem seems vague, more code can be provided. Theres alot of it so I didnt want to bombard you :) Thank you.
ServerSide:

public Server() {
        setLayout(new BorderLayout());
        add(new JScrollPane(jta), BorderLayout.CENTER);

        setTitle("Multi-Thread Server");
        setSize(500, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

        try {
            ServerSocket serverSocket = new ServerSocket(8800);
            jta.append("MultiThreadServer started at " + new Date() + '\n');

            int clientNo = 1;

            while(true) {
                Socket socket = serverSocket.accept();
                jta.append("Server Thread for client " + clientNo + " at " + new Date() + '\n');

                InetAddress inetAdress = socket.getInetAddress();
                jta.append("Client " + clientNo + "'s host name is " + inetAdress.getHostName() + "\n");
                jta.append("Client " + clientNo + "'s IP Address is " + inetAdress.getHostAddress() + "\n");

                HandleAClient task = new HandleAClient(socket);

                new Thread(task).start();

                clientNo++;
            }
        } catch(IOException ex) {
            System.err.println(ex);
        }

Client Side:

try {
            Socket socket = new Socket("localhost", 8800);

            fromServer = new DataInputStream(socket.getInputStream());
            toServer = new DataOutputStream(socket.getOutputStream());
        } catch (IOException ex) {
            jta_TextArea.setText(ex.toString() + '\n');
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唯憾梦倾城 2025-01-03 13:17:57

您的套接字绑定到localhost,这意味着只能为本地客户端提供服务。如果您希望其他人看到您的服务器,第一步就是绑定到其他人可见的 IP 地址!

Your socket is bound to localhost this means in can only serve the local client. If you want others to see your server the first step is to bind to the IP address that is visible to those others!

雨巷深深 2025-01-03 13:17:57

我想你想使用 ServerSocket而不是普通的插座。

I think you want to be using a ServerSocket and not a regular socket.

鱼窥荷 2025-01-03 13:17:57

“localhost”就是这样;它是您的本地主机或您的机器。如果您尝试将套接字连接到其他主机(例如解析为 www.worldofthedrakon.com 的主机),则需要获取该主机名或其 IP 地址。

Socket socket = new Socket("worldofthedrakon.com", 8800);

然后你就可以解决防火墙问题了。连接被拒绝通常表明存在一两个防火墙。您知道您的新主机上打开了端口 8800 吗?

"localhost" is exactly that; it is your local host or your machine. If you are trying to connect your socket to some other host (like whatever resolves to www.worldofthedrakon.com), you need to get that hostname or its IP address in there.

Socket socket = new Socket("worldofthedrakon.com", 8800);

Then you get to fight the firewall issues. Connection Refused is usually an indication that there is a firewall or two in the way. Do you know that port 8800 is open on your new host?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文