socket连续两次调用sendTo
客户端:
string str = "Hello ";
string str1 = "World";
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
IPEndPoint localPoint1 = new IPEndPoint(IPAddress.Parse("10.46.255.100"),60000);
EndPoint tempPoint1 = (EndPoint)localPoint1;
socket .SendTo(System.Text.Encoding.Default.GetBytes(str),tempPoint1);
//Thread.Sleep(1000);
socket .SendTo(System.Text.Encoding.Default.GetBytes(str1),tempPoint1);
服务器端:
while(true)
{
Length = socket.ReceiveFrom(recvBuffer,ref tempPoint);
Thread thread = new Thread(new ThreadStart(DistributeMsg));
thread.Start();
}
private void DistributeMsg()
{
byte[] buffer = new byte[Length];
Array.Copy(recvBuffer,0,buffer,0,Length);
Console.WriteLine(System.Text.Encoding.Default.GetString(buffer));
}
以上客户端和服务器都是本机。
输出结果:
World
World
如果加上Sleep(1000),输出正常,为什么?
string str = "Hello ";
string str1 = "World";
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
IPEndPoint localPoint1 = new IPEndPoint(IPAddress.Parse("10.46.255.100"),60000);
EndPoint tempPoint1 = (EndPoint)localPoint1;
socket .SendTo(System.Text.Encoding.Default.GetBytes(str),tempPoint1);
//Thread.Sleep(1000);
socket .SendTo(System.Text.Encoding.Default.GetBytes(str1),tempPoint1);
服务器端:
while(true)
{
Length = socket.ReceiveFrom(recvBuffer,ref tempPoint);
Thread thread = new Thread(new ThreadStart(DistributeMsg));
thread.Start();
}
private void DistributeMsg()
{
byte[] buffer = new byte[Length];
Array.Copy(recvBuffer,0,buffer,0,Length);
Console.WriteLine(System.Text.Encoding.Default.GetString(buffer));
}
以上客户端和服务器都是本机。
输出结果:
World
World
如果加上Sleep(1000),输出正常,为什么?
...全文
相关推荐
C语言中send()函数和sendto()函数的使用方法
C语言send()函数:经socket传送数据 头文件: #include #include 定义函数: int send(int s, const void * msg, int len, unsigned int falgs); 函数说明:send()用来将数据由指定的socket 传给对方主机. 参数s 为已建立好连接的socket. 参数msg 指向欲连线的数据内容, 参数len 则为数据长度. 参数flags 一般设0, 其他数值定义如下: MSG_OOB 传送的数据以out-of-band 送出. MS
recvfrom()和sendto()函数
recvfrom()和sendto()函数
了解与unix下socket编程相关的各种系统调用,加深对tcp/udp协议的理解
1.掌握linux下socket编程相关的各种系统调用 socket bind connect listen accept read, recvfrom write, sendto close 2.理解tcp与udp通讯机制的区别
C#使用UDP协议socket编程收发数据
本源码采用第三方类库,详细演示如何实现Udp协议的数据收发。调用接口简单,关键是底层采用了IOCP模型,效率高,比自己实现要稳定,且高效。
sendto.rar_网络编程_Visual_C++_
windows网络编程sendto程序实例


