简体   繁体   English

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

[英]Java and TCP messages - messages sending on different ports each time

I've very new to networking and using networks to send messages through programming. 我对联网和使用网络通过编程发送消息非常陌生。 Anyways, I have a client and server java command line application (server is running in a VM on the same machine with a bridged network adapter, and host to guest pinging works and vice versa), and it would appear on the server side that each message it receives is coming from a different port. 无论如何,我有一个客户端和服务器java命令行应用程序(服务器在具有桥接网络适配器的同一台计算机上的VM中运行,并且主机到来宾ping通,反之亦然),并且它将分别出现在服务器端它收到的消息来自其他端口。 Is this normal behavior? 这是正常行为吗? What happens when the machine runs out of ports to use? 当计算机用尽端口时会发生什么? Does Java's libraries intelligently close the ports after it's done with them? Java的库处理完端口后,是否会智能地关闭端口?

So basically, is this even a problem? 所以基本上,这甚至是一个问题吗? If it is, how do I go about fixing it? 如果是,该如何解决? Output from the server and then code for the client listed below. 从服务器输出 ,然后下面列出的客户端编码

SERVER OUTPUT AFTER SENDING SOME MESSAGES: 发送某些消息后的服务器输出:

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

CODE FOR THE CLIENT THAT SENT THOSE MESSAGES: 发送这些消息的客户的代码:

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

Yes, everytime you open a socket to other host, the connection can be initiated from any of the remaining port on your machine. 是的,每次打开与其他主机的套接字时,都可以从计算机上任何剩余的端口启动连接。 The OS chooses the next available port and makes the connection. 操作系统选择下一个可用端口并建立连接。

There are 65536 open ports available from which first 1-1024 ports are reserved by the system. 有65536个开放端口,系统会从中保留第一个1-1024端口。

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

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