简体   繁体   中英

I can't find my port number at my IP address

I am trying to create a chat with Socket . And to do this, I need to use a class in JAVA called Socket . It has, in my case, two parameters : Socket(InetAddress address, int port) .
I know what is my IP, by the command in CMD: ipconfig

But I don't know what to put in my port number , because I know where I can find it. 知道在哪里可以找到它。
Is there any command, application or others things to get ?
I use textpad 8 to create JAVA programs .

There is two programs, the and the .


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");
        }

    }
}





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. 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 .

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

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. 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.

Port 8100 is a pretty safe bet. 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. An application listening on a specific TCP port should be the only application listening on that port. 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 ).

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.

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:

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. You can also leave off the | findstr xxxx | findstr xxxx portion and look at the results. It's sorted by port number, so you can just pick a port somewhere between two other acceptable TCP port numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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