Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试打开新的ServerSocket失败_Java_Sockets_Serversocket - Fatal编程技术网

Java 尝试打开新的ServerSocket失败

Java 尝试打开新的ServerSocket失败,java,sockets,serversocket,Java,Sockets,Serversocket,我的老师给了我下面的代码。我运行了一两次,效果很好。然而,我突然无法在Windows8上通过命令提示符运行它。无论我指定哪个端口,它都只打印“打开端口…”,并且永远不会继续。不会抛出任何异常。我已禁用防火墙和防病毒软件,但似乎无法正常工作。我已经添加了一个print语句作为try-catch块的第一行,它将进行打印,但不会创建新的套接字。我确信这是我的Windows设置中的问题,但我不确定该如何解决 // Server program // File name: "TCPServer.java"

我的老师给了我下面的代码。我运行了一两次,效果很好。然而,我突然无法在Windows8上通过命令提示符运行它。无论我指定哪个端口,它都只打印“打开端口…”,并且永远不会继续。不会抛出任何异常。我已禁用防火墙和防病毒软件,但似乎无法正常工作。我已经添加了一个print语句作为try-catch块的第一行,它将进行打印,但不会创建新的套接字。我确信这是我的Windows设置中的问题,但我不确定该如何解决

// Server program
// File name: "TCPServer.java"

import java.io.*;
import java.net.*;
public class TCPServer
{
     private static ServerSocket servSock;
     public static void main(String[] args)
    {
        System.out.println("Opening port...\n");
        try{

       // Create a server object 
              servSock = new ServerSocket(Integer.parseInt(args[0])); 
        }

        catch(IOException e){
             System.out.println("Unable to attach to port!");
             System.exit(1);
        }

       do 
       {  
            run();
       }while (true);

    }

    private static void run()
   {
        Socket link = null; 
        try{



        // Put the server into a waiting state
               link = servSock.accept(); 

        // Set up input and output streams for socket
              BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream())); 
              PrintWriter out = new PrintWriter(link.getOutputStream(),true);

    // print local host name
              String host = InetAddress.getLocalHost().getHostName();
              System.out.println("Client has estabished a connection to " + host);

    // Receive and process the incoming data 
             int numMessages = 0;
             String message = in.readLine(); 
             while (!message.equals("DONE"))
             {
                 System.out.println(message);
                 numMessages ++;
                 message = in.readLine();
             }

      // Send a report back and close the connection
            out.println("Server received " + numMessages + " messages"); 
      }

      catch(IOException e){
          e.printStackTrace();
      }

      finally{
           try{
                System.out.println("!!!!! Closing connection... !!!!!\n" + "!!! Waiting for the next connection... !!!");
                link.close(); 
           }

           catch(IOException e){
                 System.out.println("Unable to disconnect!");
                System.exit(1);
           }
    }

  }

}

这段代码运行良好。问题是客户端的代码。问题的答案已经写在代码中的注释中

    // Put the server into a waiting state
           link = servSock.accept(); 

服务器进入等待状态,直到获得连接。由于客户端未连接,因此可能会收到错误。如果客户端工作正常,代码将继续运行,您将获得额外的输出。

运行程序时,您将向程序传递哪些命令行参数?请演示如何尝试运行它。引发了什么异常?打印您自己的邮件是不够的。您必须打印或记录实际的异常。我认为它工作正常。在课堂上,老师可能有一个连接到服务器套接字的程序,但您似乎没有发布任何充当客户端和/或客户端未连接到正确地址/端口的代码。