Java和TCP消息-每次在不同端口上发送消息

Java和TCP消息-每次在不同端口上发送消息,java,networking,tcp,ip,port,Java,Networking,Tcp,Ip,Port,我对网络和使用网络通过编程发送消息非常陌生。无论如何,我有一个客户机和服务器java命令行应用程序(服务器在同一台机器上运行,使用桥接网络适配器,主机到客户机ping工作,反之亦然),它在服务器端显示,它接收到的每条消息都来自不同的端口。这是正常的行为吗?当机器的端口用完时会发生什么情况?Java的库在处理完端口后是否会智能地关闭端口 那么基本上,这是一个问题吗?如果是,我该如何着手修复它从服务器输出,然后为下面列出的客户机编码 发送某些消息后的服务器输出: Received (/192.168

我对网络和使用网络通过编程发送消息非常陌生。无论如何,我有一个客户机和服务器java命令行应用程序(服务器在同一台机器上运行,使用桥接网络适配器,主机到客户机ping工作,反之亦然),它在服务器端显示,它接收到的每条消息都来自不同的端口。这是正常的行为吗?当机器的端口用完时会发生什么情况?Java的库在处理完端口后是否会智能地关闭端口

那么基本上,这是一个问题吗?如果是,我该如何着手修复它从服务器输出,然后为下面列出的客户机编码

发送某些消息后的服务器输出:

Received (/192.168.1.122:59628): shsfh

Received (/192.168.1.122:59629): dfsh

Received (/192.168.1.122:59631): dfh

Received (/192.168.1.122:59632): fdshdf

Received (/192.168.1.122:59633): shf

Received (/192.168.1.122:59637): fgfggsdfhsfdh

Received (/192.168.1.122:59638): fdshf

Received (/192.168.1.122:59639): hs

Received (/192.168.1.122:59640): hfh
import java.io.*;
import java.util.*;
import java.net.*;
class TCPClient
{
  public static void main(String argv[]) throws Exception
  {   Scanner scan = new Scanner(System.in);
       while (true)
       {
          String msgcont = scan.nextLine();
          System.out.println(tcpSend("192.168.1.153", 6789, 5000, msgcont));
       }
   }

public static String tcpSend(String ip, int port, int timeout, String content)
{
     String ipaddress = ip;
     int portnumber = port;
     String sentence;
     String modifiedSentence;
     Socket clientSocket;
     try
     {
         clientSocket = new Socket(ipaddress, portnumber);
         DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
         BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
         outToServer.writeBytes(content + '\n');
         clientSocket.setSoTimeout(timeout);
         modifiedSentence = inFromServer.readLine();
         clientSocket.close();
             outToServer.close();
         inFromServer.close();
     }
     catch (Exception exc)
     {
          modifiedSentence = "";
     }
          return modifiedSentence;
}
}
发送这些消息的客户端的代码:

Received (/192.168.1.122:59628): shsfh

Received (/192.168.1.122:59629): dfsh

Received (/192.168.1.122:59631): dfh

Received (/192.168.1.122:59632): fdshdf

Received (/192.168.1.122:59633): shf

Received (/192.168.1.122:59637): fgfggsdfhsfdh

Received (/192.168.1.122:59638): fdshf

Received (/192.168.1.122:59639): hs

Received (/192.168.1.122:59640): hfh
import java.io.*;
import java.util.*;
import java.net.*;
class TCPClient
{
  public static void main(String argv[]) throws Exception
  {   Scanner scan = new Scanner(System.in);
       while (true)
       {
          String msgcont = scan.nextLine();
          System.out.println(tcpSend("192.168.1.153", 6789, 5000, msgcont));
       }
   }

public static String tcpSend(String ip, int port, int timeout, String content)
{
     String ipaddress = ip;
     int portnumber = port;
     String sentence;
     String modifiedSentence;
     Socket clientSocket;
     try
     {
         clientSocket = new Socket(ipaddress, portnumber);
         DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
         BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
         outToServer.writeBytes(content + '\n');
         clientSocket.setSoTimeout(timeout);
         modifiedSentence = inFromServer.readLine();
         clientSocket.close();
             outToServer.close();
         inFromServer.close();
     }
     catch (Exception exc)
     {
          modifiedSentence = "";
     }
          return modifiedSentence;
}
}

是的,每次打开到其他主机的套接字时,都可以从计算机上的任何剩余端口启动连接。操作系统选择下一个可用端口并建立连接


有65536个打开的端口可用,其中前1-1024个端口由系统保留。

是的,每次打开到其他主机的套接字时,都可以从计算机上的任何剩余端口启动连接。操作系统选择下一个可用端口并建立连接


有65536个开放端口可用,系统将从中保留前1-1024个端口。

您正在为每个发送的注释创建一个新连接,因此它将位于不同的端口上。如果每次使用相同的连接,效率将提高100倍,每次使用相同的端口。感谢Peter和neeagl提供的快速而有用的响应。它们都很有用。你正在为每个发送的评论创建一个新的连接,因此它将位于不同的端口。如果每次使用相同的连接,效率将提高100倍,每次使用相同的端口。感谢Peter和neeagl提供的快速而有用的响应。它们都很有用。+1从技术上讲,第一个~48K是为不同的目的保留的,“49152–65535”对于临时端口是免费的,但Linux不会使用前1024个端口,除非您是root用户。窗户没有那么严格。如果您不是root用户,则可以使用1024,但不能使用1023。+1从技术上讲,第一个~48K是为不同的目的保留的,“49152–65535”对于临时端口是免费的,但Linux不会使用第一个1024,除非您是root用户。窗户没有那么严格。如果不是root用户,可以使用1024,但不能使用1023。