//Assign the any IP of the machine and listen on port number 1000
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, localport); serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);其中LOCALPORT是10000
我查过无冲突端口之后
clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), clientSocket);然后接受方接到包 然后查端口 发现每次clientSocket的发送端口都在变 怎么不是一直都是10000?
我的接收方是用MFC写的 GETPEER你懂的,就算我写的有错 我用第三方软件也是显示 clientSocket的发送端口一直变 他不是一开始就是绑定了10000了么,那么怎么做才让clientSocket的发送端口不随机,
因为我下位机接收到端口按原包返回就OK了 这样最简洁最方便
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, localport); serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);其中LOCALPORT是10000
我查过无冲突端口之后
clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), clientSocket);然后接受方接到包 然后查端口 发现每次clientSocket的发送端口都在变 怎么不是一直都是10000?
我的接收方是用MFC写的 GETPEER你懂的,就算我写的有错 我用第三方软件也是显示 clientSocket的发送端口一直变 他不是一开始就是绑定了10000了么,那么怎么做才让clientSocket的发送端口不随机,
因为我下位机接收到端口按原包返回就OK了 这样最简洁最方便
解决方案 »
- 【在线等】【急】求助啊,异步tcp中使用了与请求的协议不兼容的地址。
- 请帮我看看。这段js代码转换成C#
- 如何在C#中使控件随窗口大小变化而改变
- ScrollBar问题 VScrollBar或HScrollBar?欢迎讨论!!!
- 又多了个三角,散分!
- 关于HTMLElement中的setAttribute(string, string)属性值为整数的问题
- openxml如何操作换行?word文档如何操作换行啊?
- C# Activex 如何调用前端js方法
- [网络编程问题] 如何编程设置本机IP
- webform中怎样动态配置与数据库的连接(生成DLL后不能修改连接配置)
- asp.net上传图片按照日期自动生成文件夹保存问题
- DevExpress控件:gridControl中按钮问题
udp 的客户端如果指定本地端口,是不是需要bind下呢, tcp 的应该bind 下的貌似.
//Assign the any IP of the machine and listen on port number 1000
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, localport); serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp); //Bind this address to the server
serverSocket.Bind(ipEndPoint); IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
//The epSender identifies the incoming clients
EndPoint epSender = (EndPoint)ipeSender; serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), null);
客户端没问题 我用第三方测试过 可以原包返回 再有 这个是UDP,不分服务客户端
clientSocketserverSocket没研究过UDP的,但是我看到你的代码里面有2个Socket, 你的serverSocket确实后来说明是bind了, 但是看你的需求你是想"让clientSocket的发送端口不随机", 所以你serverSocket怎么bind也决定不了clientSocket的端口不随机吧.或者 贴更多的代码来看看.
我的client和第三方都能发包给他
我现在问题是client的包发出去后 接收方无论是第三方还是我的程序发现他发的每个包的端口都在变 但是我的client又是bind了的 这种情况在MFC里面就没碰过 另外MFC的UDP是不容许BIND的。是不是c#的socket 发包规则就是这样 bind只是监听那个端口 但是发送端口是随机的?
是你的client没bind,然后你又想client是固定的端口吧.哪里也没看到你的client 的bind代码啊.如果udp不允许bind,那你还干嘛非要客户端是固定的端口呢.
//Bind this address to the server
serverSocket.Bind(ipEndPoint);可是你的clientSocket呢?你的代码里面有个clientSocket, 我指的是那个socket你没绑定的代码啊.
麻烦仔细看看给你的回复好吧.
byte[] byteData = new byte[1024];
public SocketClass() {
} /// <summary>
/// 端口监听
/// </summary>
/// <param name="localport">本机端口</param>
public void Listen(int localport) {
try
{
//Assign the any IP of the machine and listen on port number 1000
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, localport); serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp); //Bind this address to the server
serverSocket.Bind(ipEndPoint); IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
//The epSender identifies the incoming clients
EndPoint epSender = (EndPoint)ipeSender; serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length,
SocketFlags.None, ref epSender, new AsyncCallback(OnReceive), null); }
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
} public delegate void Receive(byte[] byteData, int length);
public event Receive receive;
private void OnReceive(IAsyncResult ar)
{
try
{
IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
EndPoint epSender = (EndPoint)ipeSender;
int length = serverSocket.EndReceiveFrom(ar, ref epSender);
receive(byteData, length); serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
new AsyncCallback(OnReceive), epSender); }
catch (Exception )
{
} } /// <summary>
/// 发送
/// </summary>
/// <param name="clientip">下位机IP</param>
/// <param name="clientport">下位机端口</param>
/// <param name="message"></param>
public void SendTo(string clientip, int clientport, byte[] byteData)
{ try
{
Socket clientSocket; //The main client socket
IPEndPoint epServer; //The EndPoint of the server
IPAddress ipAddress = IPAddress.Parse(clientip); clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp); epServer = new IPEndPoint(ipAddress, clientport); clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), clientSocket);
}
catch (Exception e)
{
MessageBox.Show(e.ToString() );
}
} private void OnSend(IAsyncResult ar)
{ }
在你的SendTo的函数里面.你的clientSocket没bind啊.你给我贴了代码想表达啥意思呢.你自己的客户端与服务器端.你的服务器端确实bind了一个端口了.你现在的需求是想在服务器端能看到客户端的端口是固定的.可是你的客户端没看到有bind.我现在迷惑了.你到底想要什么.为什么你想要,你又不bind. 为什么你想要,你又不bind. 为什么你想要,你又不bind.如果按照你之前的另外一种说法,说客户端不能bind,那么你干嘛还强求.(因为我没搞过udp,没验证你这个说法)反正 我迷糊了.
我是把clientsocket多余的放在里面了 上面的长代码是在一个类里面的 都是上位机的程序···
我把clientsocket改成我已经绑定的那个SOCKET就可以了
不过他的socket也可以这样用 可以随意变端口发送 下次试试用在别的地方 用双socket我后面给了你12分