使用套接字客户端发送具有特定端口范围的数据 [英] Using socket client to send data with specific ports range
问题描述
首先,请看下面的源代码:
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse(" 192.168.0.2"),90);
Socket socketSend = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
socketSend.Connect(endpoint);
if(socketSend.Connected)
socketSend.Send(data.ToArray());
这些代码用于将数据发送到端口的ip 192.168.0.2:90。来自收到的在ip 192.168.0.2和端口90的一侧,我看到我的ip:192.168.0.3的端口总是被改变(随机)。是否可以使用静态端口设置socketSend,以便当数据通过ip 192.168.0.2和端口90到达时,端口始终是静态的?或者是否可以为socketSend设置一系列可能的端口?
提前致谢。
您可以使用Socket.Bind()将本地套接字绑定到特定端口。然后,从该套接字发送的所有数据报都将源端口设置为您指定的数据报。
http://msdn.microsoft.com/en-us/library/system.net.sockets。 socket.bind.aspx
Hi,
First, please take a look at my source code following:
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("192.168.0.2"), 90);
Socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socketSend.Connect(endpoint);
if (socketSend.Connected)
socketSend.Send(data.ToArray());
That pieces of code is used to send data to the ip 192.168.0.2 at the port: 90. From the received side of the ip 192.168.0.2 and port 90, I see that the port from my ip: 192.168.0.3 is always changed (randomly). Is it possible to set the socketSend with a static port so that when the data is arrived ath the ip 192.168.0.2 and the port 90, the port is always static? or is it possible to set a range of possible ports for the socketSend?
Thanks in advance.
You can use Socket.Bind() to bind the local socket to a particular port. Then, all datagrams sent from that socket will have the source port set to the one you specified.
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.bind.aspx
这篇关于使用套接字客户端发送具有特定端口范围的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!