简体   繁体   English

TCP套接字服务器/客户端连接应使用哪个IP /端口?

[英]What ip/port should i use for TCP Socket Server/Client connection?

i made basic TCP Socket server and client console application in c# with listener etc.. it works well with both server and client executed in same machine(127.0.0.1:10048). 我用侦听器等在c#中制作了基本的TCP Socket服务器和客户端控制台应用程序。它与在同一台计算机上执行的服务器和客户端都可以很好地工作(127.0.0.1:10048)。 I want to try it with different machines in same network(both connected to same modem). 我想在同一网络(都连接到同一调制解调器)中的不同机器上尝试。 Which ip port should i use? 我应该使用哪个IP端口? I need help. 我需要帮助。 Thanks 谢谢

You should use a port in the ephemeral port range. 您应该使用临时端口范围内的端口。 The ephemeral port range is the range of port numbers that is being selected from if you active connect to a server. 临时端口范围是从活动连接到服务器时正在选择的端口号范围。 The point is that it is free for use. 关键是它是免费使用的。 Your kernel will skip the port numbers that are already in use so you don't have to worry about that either. 您的内核将跳过已经使用的端口号,因此您不必为此担心。

http://en.wikipedia.org/wiki/Ephemeral_port http://en.wikipedia.org/wiki/Ephemeral_port

And on top of this it is best not to hardcode your port numbers and ip adresses where you bind to connect or send to. 最重要的是,最好不要对绑定或连接到的端口号和IP地址进行硬编码。

Make sure your OS firewall is turned off. 确保您的操作系统防火墙已关闭。 For instance windows firewall can block this type of traffic. 例如,Windows防火墙可以阻止此类流量。

Do not use just any free port that you detect is not in use. 不要仅使用检测到未使用的任何空闲端口。 For instance you may not have an FTP or Telnet server running on your system, but that does not mean that you can just hijack those ports. 例如,您的系统上可能没有运行FTP或Telnet服务器,但这并不意味着您可以劫持这些端口。 From a functional point of view it will work if you do, but then you cannot run those services anymore somewhere in the future where you might need them, or your application will start failing. 从功能的角度来看,如果可以的话,它会起作用,但是将来您将无法再在可能需要它们的地方运行这些服务,否则您的应用程序将开始出现故障。 Which fails depends on which application is first started and starts using the port first. 哪个失败取决于哪个应用程序首先启动并首先开始使用端口。

When you bind an ip@ you should use INADDR_ANY. 绑定ip @时,应使用INADDR_ANY。 Loopback communication will still work if you use this, you probably already did, most examples include it. 如果使用了环回通信(您可能已经做过),它仍然会起作用,大多数示例都包含了它。 Sending or connecting to an IP@ should come from a configuration file (data driven) or commandline parameters. 发送或连接到IP @应该来自配置文件(数据驱动)或命令行参数。 The IP@ depends of course on the machine you want to communicate with. IP @当然取决于您要与之通信的机器。

Open your CMD and type ipconfig . 打开您的CMD并键入ipconfig There you can see your IPv4 address, that you should use to connect. 在这里,您可以看到用于连接的IPv4地址。 The port doesn't really matter. 端口并不重要。 Make sure to turn of your firewall(s) to allow a connection 确保关闭防火墙以允许连接

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

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