Changing local port of a socket client

2019-09-11 17:18发布


I have a simple TCP socket client and server application. They are communicating using IP = localhost and port = 33367.

I'm using SocketSniff to examine my packets going through localhost. While sniffing the client app, I noticed that every time I'm sending a packet to the server in the same process, the "local port" is changing, while remote port is always 33367.

So, is it possible for the client apps to send their data through a fixed port (if so, how in C#?) or do they have to get assigned a different port each time?


You can bind the socket before calling connect.