开发者

Set a range of free local ports to application

开发者 https://www.devze.com 2023-04-06 02:00 出处:网络
I have a client application that create a socket with random local port, i cant change the code of this appl开发者_StackOverflow中文版ication and i want to set a range of the ports he can use from my

I have a client application that create a socket with random local port, i cant change the code of this appl开发者_StackOverflow中文版ication and i want to set a range of the ports he can use from my C# application. Is it possible? Thanks!


So the client will try to connect to your application using any one of a range of port numbers. You can't know which socket they will use, but you CAN know they will use one of this range.

If that is the case, you can do one of two things:

  1. If the computer on which your app runs only ever has to interact with this one client, then simply set up one TcpListener (or other socket listener) to listen on your address on all ports. This is risky; if some other computer tries to ask for a web page, or tries to set the network time on this comp, your app may incorrectly intercept these connections.
  2. If you really do need a range of available ports on which the client can connect, then I would use a for loop to set up a collection of TcpListeners, one for each port on which you wish to connect.

Understand this is for TCP. UDP (another common protocol) is stateless, meaning connections are never created between client and server; the client simply shouts something to that server and port and prays the server is listening, receives and understands the message.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号