Java套接字编程 - 套接字在连接完成之前超时

0 java sockets

我有一个套接字编程代码..

Server Program

import java.net.*;
import java.io.*;

public class GreetingServer extends Thread
{
private static int port;
  private ServerSocket serverSocket;

  public GreetingServer(int port) throws IOException
   {
  serverSocket = new ServerSocket(port);
  serverSocket.setSoTimeout(50000);
   }

 public void run()
  {
     while(true)
     {
        try
         {
           System.out.println("Waiting for client on port " +
           serverSocket.getLocalPort() + "...");
           Socket server = serverSocket.accept();
           System.out.println("Just connected to "
                + server.getRemoteSocketAddress());
           DataInputStream in =
              new DataInputStream(server.getInputStream());
           System.out.println(in.readUTF());
           DataOutputStream out =
             new DataOutputStream(server.getOutputStream());
            out.writeUTF("Thank you for connecting to "
             + server.getLocalSocketAddress() + "\nGoodbye!");
        server.close();
     }catch(SocketTimeoutException s)
     {
        System.out.println("Socket timed out!");
        break;
     }catch(IOException e)
     {
        e.printStackTrace();
        break;
       }
    }
 }
     public static void main(String [] args)
     {
       port=9000;
       try
     {
       Thread t = new GreetingServer(port);
       t.start();
     }catch(IOException e)
    {
      e.printStackTrace();
    }
   }
 }
Run Code Online (Sandbox Code Playgroud)

而客户端代码是......

import java.io.*;
import java.net.*;
public class GreetingClient
 {
   private static String serverName;
   public static void main(String [] args)
    {
      String sName = "MyServerName";
      int port = 9000;
      try
       {
        System.out.println("Connecting to " + sName
                         + " on port " + port);
     Socket client = new Socket(sName, port);
     System.out.println("Just connected to "
                  + client.getRemoteSocketAddress());
     OutputStream outToServer = client.getOutputStream();
     DataOutputStream out =
                   new DataOutputStream(outToServer);

     out.writeUTF("Hello from "
                  + client.getLocalSocketAddress());
     InputStream inFromServer = client.getInputStream();
     DataInputStream in =
                    new DataInputStream(inFromServer);
     System.out.println("Server says " + in.readUTF());
     client.close();
  }catch(IOException e)
    {
    }
  }
 }
Run Code Online (Sandbox Code Playgroud)

代码编译得很好.但是当我运行程序时,首先是服务器,然后是客户端,服务器会显示该程序

Waiting for client on port 9000...
Socket timed out!
Run Code Online (Sandbox Code Playgroud)

并且客户端显示

Connecting to MyServerName on port 9000
Run Code Online (Sandbox Code Playgroud)

代码有什么问题?我已经尝试增加和减少超时值,但它提供相同的输出.

use*_*421 5

代码有什么问题?

您的代码没有任何问题.您设置了50秒的接受超时,接受阻止50秒而没有客户端尝试连接,因此SocketTimeoutException抛出了a .这里的一切都按设计工作.

当然有可能:

  • 你的接受超时太短了
  • 你不想因为接受超时而中止你的服务器
  • 你根本不想要接受超时.

所有这些都是取决于您的要求的设计决策.

您也可能在客户端中输入了错误的主机名,但是因为您忽略了客户端中的异常:

catch(IOException e)
{
}
Run Code Online (Sandbox Code Playgroud)

你永远无法找到答案.千万不要忽视IOExceptions.