简体   繁体   English

我在我的IP地址上找不到我的端口号

[英]I can't find my port number at my IP address

I am trying to create a chat with Socket . 我正在尝试与Socket聊天。 And to do this, I need to use a class in JAVA called Socket . 为此,我需要在JAVA中使用一个称为Socket It has, in my case, two parameters : Socket(InetAddress address, int port) . 就我而言,它具有两个参数Socket(InetAddress address, int port)
I know what is my IP, by the command in CMD: ipconfig 我知道什么是我的IP,通过CMD中的命令: ipconfig

But I don't know what to put in my port number , because I don't know where I can find it. 但是我不知道在端口号中放什么,因为我知道在哪里可以找到它。
Is there any command, application or others things to get port number ? 是否有任何命令,应用程序或其他东西来获取端口号
I use textpad 8 to create JAVA programs . 我使用textpad 8创建JAVA程序

There is two programs, the transmitter and the receiver . 有两个程序, 发射器接收器

Transmitter code 发射器代码

import java.net.*;
import java.io.*;
public class Program
{

    public static void main(String[] args)
    {
        try
        {

            ServerSocket request = new ServerSocket(12345);
            Socket connection = request.accept();
            PrintWriter transmitter = new PrintWriter(connection.getOutputStream());
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

                String line = null;

                do
                {

                    line = keyboard.readLine();
                    transmitter.println(line);
                    transmitter.flush();

                }
                while (line==null || !line.toUpperCase().equals("END"));

                    transmitter.close();
                    connection.close();
                    request.close();

        }

        catch(Exception error)
        {
            System.out.println("Communication error");
        }

    }
}



Receiver code 接收方代码

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

public class Programa
{

public static void main(String[] args) {

            try
            {
            Socket connection = new Socket("123.456.78.9",12345);
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
            BufferedReader receptor = new BufferedReader(new InputStreamReader(conexao.getInputStream()));

                String line = null;
                do
                {

                        line=receptor.readLine();
                        System.out.println(line);

                }
                while(line==null || !line.toUpperCase().equals("END"));
                receptor.close();
                connection.close();
            }

            catch(Exception error)
            {
                System.out.println("Communication error");
                System.out.println("Error: " + error);
            }
        }
 }

"port" argument in this constructor is the port on which you want to run your service. 此构造函数中的“端口”参数是您要在其上运行服务的端口。 It is not related to the system. 它与系统无关。 You can pass it as any port number on which you want this service to run. 您可以将其作为要运行此服务的任何端口号进行传递。

The address gets you to the machine. 该地址将您带到机器。 The machine the receives this request, and based on the added port number directs the network traffic to a running program. 机器接收到该请求,并根据添加的端口号将网络流量定向到正在运行的程序。

If you don't know which program is supposed to receive your request, you need to find out. 如果您不知道应该接收哪个程序的请求,则需要查找。 Then you need to find out what port number that program is listening to. 然后,您需要找出程序正在侦听的端口号。 What the phrase "listening to" really means is "what port number the operating system will interpert as traffic that needs to be sent to the program". 短语“监听”的真正含义是“操作系统将交互哪个端口号作为需要发送到程序的流量”。

If you were writing both a client and a server, then a parameter of the ServerSocket you will create in the server, will be the port number to "accept requests" on. 如果同时编写客户端和服务器,则将在服务器中创建的ServerSocket的参数将是“接受请求”的端口号。 For the server, this is actually a request to the operating system to have traffic tagged with this port number to be redirected as input ready to be read on the ServerSocket . 对于服务器,这实际上是对操作系统的请求,要求使用此端口号标记的流量重定向为输入,以准备在ServerSocket上读取。

If you have no idea what ports are being handled by server programs, and you are logged into that server, and the server is a Linux server, you can run the command 如果您不知道服务器程序正在处理哪些端口,并且已登录到该服务器,并且该服务器是Linux服务器,则可以运行以下命令

lsof -iTCP -sTCP:LISTEN

which will list all the ports where a server like program is listening. 它将列出服务器之类的程序正在侦听的所有端口。

If you are not on that machine, you can use a tool like nmap but I would advise against using it on someone else's network without prior permission. 如果您不在那台计算机上,则可以使用诸如nmap类的工具,但是我建议您不要在未经事先允许的情况下在其他人的网络上使用它。 Many people use nmap to discover possible routes of attacking a server, so an miscommunication in your intention could result in their believing you are a malicious attacker. 许多人使用nmap来发现攻击服务器的可能途径,因此,意图不当的沟通可能会导致他们认为您是恶意攻击者。

Port 8100 is a pretty safe bet. 端口8100是一个非常安全的选择。 That said, a port is a whole number less than 65536 that an application can listen and respond to on one or more IP addresses. 也就是说,端口是一个小于65536的整数,应用程序可以在一个或多个IP地址上侦听和响应该端口。 An application listening on a specific TCP port should be the only application listening on that port. 侦听特定TCP端口的应用程序应该是侦听该端口的唯一应用程序。 Some port assignments are "well known" and you can avoid potential conflicts by looking them up even if you don't happen to be running one of those applications at the time. 有些端口分配是“众所周知的”,您可以通过查找它们避免潜在的冲突,即使您当时没有碰巧正在运行这些应用程序之一。 (See, for example, https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers ). (例如,请参阅https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers )。

If you might want to share your Java app with a friend who uses Linux some time in the future, you should avoid using ports under 1024 or they'll need to configure special permissions. 如果您将来可能希望与使用Linux的朋友共享Java应用程序,则应避免使用1024以下的端口,否则他们将需要配置特殊权限。

So, the conflict-avoidance game is played by first avoiding commonly-used ports and ports and low-numbered ports, then pick from the remaining ones available and verify to see that your system isn't already using it. 因此,避免冲突游戏的玩法是首先避开常用的端口,端口和低编号的端口,然后从其余可用端口中进行选择,并验证您的系统是否尚未使用它。 You can do so on Windows with the netstat command like so: 您可以在Windows上使用netstat命令执行此操作,如下所示:

netstat -a -b | findstr 8100

If you see output with the word "LISTENING", then pick another, perhaps 8101, 8102, etc. If no results show up after, say, 10 seconds, then you've picked a winner. 如果看到带有“ LISTENING”字样的输出,则选择另一个,例如8101、8102等。如果在10秒钟后仍未显示结果,则说明您选择了一个获胜者。 You can also leave off the | findstr xxxx 您也可以不使用| findstr xxxx | findstr xxxx portion and look at the results. | findstr xxxx部分并查看结果。 It's sorted by port number, so you can just pick a port somewhere between two other acceptable TCP port numbers. 它按端口号排序,因此您只需在其他两个可接受的TCP端口号之间的某个位置选择一个端口即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM