java - 为什么这个套接字程序每次连接时都会占用不同的端口

标签 java sockets port

服务器端程序

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

public class GreetingServer extends Thread
{
    private ServerSocket serverSocket;
    public GreetingServer(int port) throws IOException
    {
        serverSocket = new ServerSocket(port);
        serverSocket.setSoTimeout(10000);
    }

    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)
    {
        int port = Integer.parseInt(args[0]);
        try
        {
            Thread t = new GreetingServer(port);
            t.start();
        }catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

客户端程序

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

public class GreetingClient
{
    public static void main(String [] args)
    {
        String serverName = args[0];
        int port = Integer.parseInt(args[1]);
        try
        {
            System.out.println("Connecting to " + serverName
                    + " on port " + port);
            Socket client = new Socket(serverName, 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)
        {
            e.printStackTrace();
        }
    }
}

如果可能的话,发送和接收任何文件。 我尝试了很多,但我无法弄清楚为什么使用不同的端口和文件操作。

最佳答案

why does this socket program take different ports every time i connect to it

这就是当您连接未绑定(bind)套接字时操作系统所做的事情。它选择一些可用的源端口(称为“临时端口”)并使用它。如果你想确保它选择某个端口,你需要 bind套接字。

关于java - 为什么这个套接字程序每次连接时都会占用不同的端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14796033/

相关文章:

java - 在 Android JUnit 类中测试 EditText 输入的首选方法

c# - 在 C# 中锁定静态方法

node.js - 使用express-session和express-socket.io-session NodeJs时出错

python - 多端口网络应用

java - 在这个简单的例子中替代 if 语句

java - 我在使用 libgdx 首选项时遇到问题。不保存高分?

python - 将所有内容从 argparse 重定向到套接字连接,但通常不重定向到 stdout

使用 WIFI 调试 react-native 应用程序

java - 如何打开Jboss 4447端口?

apache - 更改 wordpress 的端口重定向到旧端口