哪些做法是错误的? (Java 服务器/套接字编程)

发布于 2024-10-01 15:46:27 字数 1304 浏览 0 评论 0原文

我在连接服务器和套接字时遇到了一些问题。我确保 IP 有效,所以这不是地址不匹配的问题。也不是端口不匹配(我也测试过这一点)。

以下是服务器处理程序

class ServerConnect extends Thread
{
   // ...
   public ServerConnect(int port, String pass) throws IOException
   {
      // ...
      this.start();
   }

   public void run()
   {
      try
      {
         // ...
         while (! server.isClosed()) // Make Connections
         {
            Socket newClient = server.accept();
            System.out.println("Connection Accepted!");
            // ...
         }

      }
      // ...
   }
}

这是套接字连接器

class ClientConnect extends Thread
{
   // ...
   public ClientConnect(InetAddress ip, int port, String pass)
   {
      try
      {
         System.out.println("Connecting to host:\t" + ip.toString() + ":" + port);
         client = new Socket(ip, port);
         output = new PrintWriter(client.getOutputStream(), true);
         input = new Scanner(client.getInputStream());
         // ...
      }
      // ...
   }
   // ...
}

首先,我尝试在本地进行测试。我输入了我的 IP 和 IP 127.0.0.1,两者都将我连接到我在本地设置的服务器。但是,然后我尝试从另一个IP,发生了这样的事情。建立连接的线程冻结了一小会儿,然后简单地跳到异常并告诉我它找不到主机,而主机从未说过它接受了连接。如果有帮助,我将使用端口 4424 来连接服务器。在我的代码中是否有什么可能导致这种情况,或者我没有采取什么措施来防止这种情况?

另外,如果我没有提供足够的信息,请告诉我。我已经做了很多测试,可以做更多的事情来帮助找出问题所在。

I have a little problem making servers and sockets connect. I made sure IPs are valid, so it's not an issue of the addresses not matching. Nor is it the port that doesn't match(I've tested this, as well).

The following is the server handler

class ServerConnect extends Thread
{
   // ...
   public ServerConnect(int port, String pass) throws IOException
   {
      // ...
      this.start();
   }

   public void run()
   {
      try
      {
         // ...
         while (! server.isClosed()) // Make Connections
         {
            Socket newClient = server.accept();
            System.out.println("Connection Accepted!");
            // ...
         }

      }
      // ...
   }
}

And this is the socket connector

class ClientConnect extends Thread
{
   // ...
   public ClientConnect(InetAddress ip, int port, String pass)
   {
      try
      {
         System.out.println("Connecting to host:\t" + ip.toString() + ":" + port);
         client = new Socket(ip, port);
         output = new PrintWriter(client.getOutputStream(), true);
         input = new Scanner(client.getInputStream());
         // ...
      }
      // ...
   }
   // ...
}

First, I tried testing locally. I entered my IP, and the IP 127.0.0.1, and both connected me to the server I had set up locally. But, then I tried from another IP, and here's what happened. The thread that was making the connection froze for a short while, then simply skipped down to the exception and told me that it couldn't find a host, and the host never says that it accepted a connection. If it helps, I'm using the port 4424 to connect with the server. Is there anything that could cause this in my code, or is there something I'm not doing to prevent this?

Also, please let me know if I'm not providing enough information. I've done plenty of testing, and can do more to help figure out what is wrong.

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

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

发布评论

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

评论(2

夜唯美灬不弃 2024-10-08 15:46:27

听起来像是网络问题。您是否尝试从客户端计算机 ping 服务器?您是否测试了端口是否可达(通过使用 telnet 或在线端口测试器)?

只需在您选择的控制台中键入这两个命令(应该适用于大多数操作系统),然后查看是否可以建立连接:

ping <hostname>
telnet <hostname> <port>

Sounds like a network problem. Did you try to ping the server from your client machine? Did you test whether the port was reachable (by using telnet or an online port tester)?

Just type those two commands in a console of your choice, should work in most OS's, and see if a connection can be established:

ping <hostname>
telnet <hostname> <port>
雪若未夕 2024-10-08 15:46:27

听起来客户端无法访问服务器。客户端能ping通服务器的IP地址吗?服务器和客户端之间有防火墙吗?服务器是否运行本地防火墙?

Sounds like the server is not reachable from the client. Can you ping the server's IP address from the client? Is there a firewall between the server and client? Does the server run a local firewall?

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