Java 和 TCP 消息 - 每次在不同端口上发送的消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19201720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java and TCP messages - messages sending on different ports each time
提问by Sop Wyin
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. 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 命令行应用程序(服务器在具有桥接网络适配器的同一台机器上的 VM 中运行,并且主机到来宾 ping 工作,反之亦然),并且它会出现在服务器端,每个它收到的消息来自不同的端口。这是正常行为吗?当机器用完可用的端口时会发生什么?Java 的库在处理完端口后是否会智能地关闭端口?
So basically, is this even a problem? If it is, how do I go about fixing it? Output from the serverand then code for the clientlisted 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;
}
}
采纳答案by neeagl
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 个端口由系统保留。