win10 udp无法接收,求解

mynicobaby 2017-09-13 10:55:07
C#实现的是,本地通过3848端口向1.1.1.8的3850端口发包,会返回一个包,以下是接收返回包的代码,不知道是我写错了还是怎么着,win7很正常的收发,但是到了win10不但收不了还线程阻塞,于是我给它新开了线程,虽然不阻塞了,但是依旧没东西可收,我关了防火墙也关了杀软之类的都不行,求各位大佬支招。


IPEndPoint localPoint = new IPEndPoint(IPAddress.Parse(localip), 3848);
UdpClient client = new UdpClient(localPoint);
IPEndPoint serverPoint = new IPEndPoint(IPAddress.Parse("1.1.1.8"), 3850);

//发送数据包
client.Send(sendBytes, sendBytes.Length, serverPoint);

//接收数据包
byte[] receBytes = client.Receive(ref serverPoint);
client.Close();
...全文
1988 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mynicobaby 2017-09-14
  • 打赏
  • 举报
回复
引用 4 楼 ilikeff8 的回复:
看你的描述,是装双系统,还是说在2台机器上测试的,如果同样的程序在win7机器上OK,win10的机器上就收不到服务器的反馈,是不是路由做过什么端口映射,只回到win7这台的3848端口
两台机器测试的,没有路由器。
mynicobaby 2017-09-14
  • 打赏
  • 举报
回复
引用 1 楼 sp1234 的回复:
先把另一个服务端进程跟这个进程部署在同一台机器(本机)测试一下。 贴出服务端的那4、5行代码来看看你的服务端如何接收和(使用相同的channel)返回信息。
那是学校的服务器,我是模拟拨号客户端,发包后服务器会返回一些数据的,win7正常收发,就是win10有问题
mynicobaby 2017-09-14
  • 打赏
  • 举报
回复
引用 5 楼 xian_wwq 的回复:
使用抓包工具,看是否有包发出或接收到 可以帮助定位问题
win7是正常收发的,就是win10有问题
xian_wwq 2017-09-14
  • 打赏
  • 举报
回复
使用抓包工具,看是否有包发出或接收到 可以帮助定位问题
ilikeff8 2017-09-14
  • 打赏
  • 举报
回复
看你的描述,是装双系统,还是说在2台机器上测试的,如果同样的程序在win7机器上OK,win10的机器上就收不到服务器的反馈,是不是路由做过什么端口映射,只回到win7这台的3848端口
lindexi_gd 2017-09-14
  • 打赏
  • 举报
回复
UWP 无法获得本地的访问,如果需要需要在调试开启,UDP传输在 UWP 中我没用过
  • 打赏
  • 举报
回复
另外,客户端根本无需注册什么端口!你的所谓“3848端口”完全是多余的。这部分代码应该删除掉。
  • 打赏
  • 举报
回复
先把另一个服务端进程跟这个进程部署在同一台机器(本机)测试一下。 贴出服务端的那4、5行代码来看看你的服务端如何接收和(使用相同的channel)返回信息。
mynicobaby 2017-09-14
  • 打赏
  • 举报
回复
引用 9 楼 sp1234 的回复:
[quote=引用 7 楼 sanshuifeibing 的回复:] 那是学校的服务器,我是模拟拨号客户端,发包后服务器会返回一些数据的,win7正常收发,就是win10有问题
贴出你的服务端的那4、5行主要代码,其实是有目的的。 为什么我让你删除 3848 端口的相关语句?因为正常的 udp 服务端通讯,当收到某个客户端channel 来的请求(也是一个 udpclient 对向),直接往这个对象写返回结果。通讯的编程模式,是向 udpclient 写结果,并不是指定某个端口去发送返回值! 当然你收不到数据这可以跟 udp 自身的不可靠通讯机制有关。在有些路由、联网方式上 udp 不通,一点都不稀奇。所以 qq 会在 udp 不通时自动改为 tcp 通讯,tcp不通时自动改为http 通讯。[/quote] 没有写服务端,我写的只是一个拨号客户端,向学校的服务器发送包,用于拨号之类的
Jackey_shao 2017-09-14
  • 打赏
  • 举报
回复
/ Implements the connection logic for the socket server. // After accepting a connection, all data read from the client // is sent back to the client. The read and echo back to the client pattern // is continued until the client disconnects. class TcpSocketServer { private int m_numConnections; // the maximum number of connections the sample is designed to handle simultaneously private int m_receiveBufferSize;// buffer size to use for each socket I/O operation BufferManager m_bufferManager; // represents a large reusable set of buffers for all socket operations const int opsToPreAlloc = 2; // read, write (don't alloc buffer space for accepts) Socket listenSocket; // the socket used to listen for incoming connection requests // pool of reusable SocketAsyncEventArgs objects for write, read and accept socket operations public static SocketAsyncEventArgsPool m_readWritePool; int m_totalBytesRead; // counter of the total # bytes received by the server int m_numConnectedSockets; // the total number of clients connected to the server Semaphore m_maxNumberAcceptedClients; public delegate void OnReceiveData(AsyncUserToken token, byte[] buff); /// <summary> /// 接收到客户端的数据事件 /// </summary> public event OnReceiveData ReceiveClientData; public TcpSocketServer(int numConnections, int receiveBufferSize) { m_totalBytesRead = 0; m_numConnectedSockets = 0; m_numConnections = numConnections; m_receiveBufferSize = receiveBufferSize; m_bufferManager = new BufferManager(receiveBufferSize * numConnections * opsToPreAlloc, receiveBufferSize); m_readWritePool = new SocketAsyncEventArgsPool(numConnections); m_maxNumberAcceptedClients = new Semaphore(numConnections, numConnections); } public void Init() { m_bufferManager.InitBuffer(); SocketAsyncEventArgs readWriteEventArg; for (int i = 0; i < m_numConnections; i++) { readWriteEventArg = new SocketAsyncEventArgs(); readWriteEventArg.Completed += IO_Completed;// new EventHandler(IO_Completed); readWriteEventArg.UserToken = new AsyncUserToken(); m_bufferManager.SetBuffer(readWriteEventArg); m_readWritePool.Push(readWriteEventArg); } } public void Start(IPEndPoint localEndPoint) { listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listenSocket.Bind(localEndPoint); listenSocket.Listen(100); StartAccept(null); } public void StartAccept(SocketAsyncEventArgs acceptEventArg) { if (acceptEventArg == null) { acceptEventArg = new SocketAsyncEventArgs(); acceptEventArg.Completed += AcceptEventArg_Completed; //new EventHandler(AcceptEventArg_Completed); } else { acceptEventArg.AcceptSocket = null; } m_maxNumberAcceptedClients.WaitOne(); bool willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg); if (!willRaiseEvent) { ProcessAccept(acceptEventArg); } } void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e) { ProcessAccept(e); } private void ProcessAccept(SocketAsyncEventArgs e) { Interlocked.Increment(ref m_numConnectedSockets); Log.SaveMsg("TCPSocketInfo", string.Format("Client connection accepted. There are {0} clients connected to the server", m_numConnectedSockets)); //ReadEventArg object user token SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop(); ((AsyncUserToken)readEventArgs.UserToken).Socket = e.AcceptSocket; bool willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArgs); if (!willRaiseEvent) { ProcessReceive(readEventArgs); } // Accept the next connection request StartAccept(e); } void IO_Completed(object sender, SocketAsyncEventArgs e) { switch (e.LastOperation) { case SocketAsyncOperation.Receive: ProcessReceive(e); break; case SocketAsyncOperation.Send: ProcessSend(e); break; default: throw new ArgumentException("The last operation completed on the socket was not a receive or send"); } } private void ProcessReceive(SocketAsyncEventArgs e) { AsyncUserToken token = (AsyncUserToken)e.UserToken; if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) { Interlocked.Add(ref m_totalBytesRead, e.BytesTransferred); Log.SaveMsg("TCPSocketInfo", string.Format("The server has read a total of {0} bytes", m_totalBytesRead)); Int32 BytesToProcess = e.BytesTransferred; Byte[] bt = new Byte[BytesToProcess]; Buffer.BlockCopy(e.Buffer, e.Offset, bt, 0, BytesToProcess); string strReceive = Encoding.Default.GetString(bt); Log.SaveMsg("TCPSocketInfo", "The server has read string: " + strReceive); if (ReceiveClientData != null) ReceiveClientData(token, bt); Thread.Sleep(200); bool willRaiseEvent = token.Socket.SendAsync(e); if (!willRaiseEvent) { ProcessSend(e); } } else { CloseClientSocket(e); } } public static bool Send(Socket socket, byte[] buffer, int offset, int size, int timeout) { socket.SendTimeout = 0; int startTickCount = Environment.TickCount; int sent = 0; // how many bytes is already sent do { if (Environment.TickCount > startTickCount + timeout) { Log.SaveMsg("SendMsgInfo","TimeOut..."); } try { string str = System.Text.Encoding.Default.GetString(buffer); sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None); Log.SaveMsg("SendMsgInfo", "Send Info==="+str); } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.WouldBlock || ex.SocketErrorCode == SocketError.IOPending || ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable) { // socket buffer is probably full, wait and try again Thread.Sleep(30); } else { Log.SaveMsg("SendMsgInfoError", "Send Error Info===" + ex); return false; } } } while (sent < size); return true; } private void ProcessSend(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { //e.SetBuffer(e.Offset, 10); // done echoing data back to the client AsyncUserToken token = (AsyncUserToken)e.UserToken; // read the next block of data send from the client bool willRaiseEvent = token.Socket.ReceiveAsync(e); if (!willRaiseEvent) { ProcessReceive(e); } } else { CloseClientSocket(e); } } private void CloseClientSocket(SocketAsyncEventArgs e) { AsyncUserToken token = e.UserToken as AsyncUserToken; // close the socket associated with the client try { token.Socket.Shutdown(SocketShutdown.Send); } // throws if client process has already closed catch (Exception) { } token.Socket.Close(); // decrement the counter keeping track of the total number of clients connected to the server Interlocked.Decrement(ref m_numConnectedSockets); m_maxNumberAcceptedClients.Release(); Console.WriteLine("A client has been disconnected from the server. There are {0} clients connected to the server", m_numConnectedSockets); // Free the SocketAsyncEventArg so they can be reused by another client m_readWritePool.Push(e); } 上面是通过tcp方式实现的 udp也都类似,你参考下。调用方式如下: IPEndPoint iep = new IPEndPoint(IPAddress.Parse(GlobalConfig.HttpServerIp), iport); TcpSocketServer objServer = new TcpSocketServer(500, 1024); objServer.Init(); objServer.Start(iep); objServer.ReceiveClientData += ObjServer_ReceiveClientData;
  • 打赏
  • 举报
回复
你可以在 win10 上,单机,并且不绑定任何本地端口(不绑定3848,而是由系统随机分配),先测试一下。排除你认为的 window 10 系统设置有问题。然后再在小的局域网里测试,就是一个路由器直接连两个电脑的方式。最后再测试“拨号网络”。
  • 打赏
  • 举报
回复
通常我们首先要求开发 tcp 通讯程序。udp 只是补充,并且通常只是用于单机(跟自己的其它进程)或者很小的局域网内的一个网段内部(不跨网段),并不对 udp 这种不可靠通讯报很大的希望。
  • 打赏
  • 举报
回复
引用 7 楼 sanshuifeibing 的回复:
那是学校的服务器,我是模拟拨号客户端,发包后服务器会返回一些数据的,win7正常收发,就是win10有问题
贴出你的服务端的那4、5行主要代码,其实是有目的的。 为什么我让你删除 3848 端口的相关语句?因为正常的 udp 服务端通讯,当收到某个客户端channel 来的请求(也是一个 udpclient 对向),直接往这个对象写返回结果。通讯的编程模式,是向 udpclient 写结果,并不是指定某个端口去发送返回值! 当然你收不到数据这可以跟 udp 自身的不可靠通讯机制有关。在有些路由、联网方式上 udp 不通,一点都不稀奇。所以 qq 会在 udp 不通时自动改为 tcp 通讯,tcp不通时自动改为http 通讯。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧