Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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 自动打开端口?_Java_Sockets - Fatal编程技术网

Java 自动打开端口?

Java 自动打开端口?,java,sockets,Java,Sockets,当我安装任何其他程序时,我不必手动打开任何端口,但是当我使用套接字编写程序(用java编写)时,只有手动打开这些端口,程序才能工作。我是不是遗漏了什么?是否有一些技术可以自动打开这些端口?也许我可以使用一些始终打开的端口?(我觉得很奇怪)。欢迎任何建议 int port = 7000; Socket s = new Socket(ipAdress, port); DataInputStream din = new DataInputStream(s.getInputS

当我安装任何其他程序时,我不必手动打开任何端口,但是当我使用套接字编写程序(用java编写)时,只有手动打开这些端口,程序才能工作。我是不是遗漏了什么?是否有一些技术可以自动打开这些端口?也许我可以使用一些始终打开的端口?(我觉得很奇怪)。欢迎任何建议

    int port = 7000;

    Socket s = new Socket(ipAdress, port);

    DataInputStream din = new DataInputStream(s.getInputStream());
    DataOutputStream dout = new DataOutputStream(s.getOutputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    String str = "", str2 = "";
    while (!str.equals("stop")) {
        str = br.readLine();
        dout.writeUTF(str);
        dout.flush();
        str2 = din.readUTF();
        System.out.println("Server says: " + str2);
    }

    dout.close();
    s.close();
客户^

    int port = 7000;
    int id = 1;

    ServerSocket loginserver = null;
    try {
        loginserver = new ServerSocket(port);
        System.out.println("LoginServer started...");
    } catch (IOException e) {
        System.out.println("Could not listen on port: " + port);
        System.exit(-1);
    }

    while (true) {
        Socket clientSocket = loginserver.accept();
        ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
        cliThread.start();
    }

服务器^

请发布一些代码,显示您当前正在做的事情。@而且,Turner什么都没有,只是在java中创建了一个套接字,用于侦听客户端的一些输入。(使用Oracle教程)。它与localhost一起工作,但是当我在不同的网络上尝试它时,我需要打开手动使用的端口。当我安装通过互联网连接的任何程序(例如游戏)时,我通常不需要手动打开这些端口。我只是想知道到底发生了什么,我如何才能做到用户不必手动打开任何端口。实际上根本没有代码?难怪那些港口不开放。说真的,你必须有一些你正试图运行的代码,否则你只是在问为什么不运行。@AndyTurner好吧,好吧,我把代码放在op里了。只是想澄清一下,“打开端口”,你的意思是允许它们通过防火墙还是类似的东西?