2013-09-26
展开全部
先说下思路 两部分一个接收一个发送。要点,难点:接收需要一个专门接收的线程,发送是使用Socket.Sendto()方法。核心代码给你粘了。。 public IPEndPoint Listener()
{
ipend = new IPEndPoint(IPAddress.Any, port);
Udp = new UdpClient(1314);
byte[] getinformation = Udp.Receive(ref ipend); contend = Encoding.GetEncoding("gb2312").GetString(getinformation); Thread.Sleep(30);
Udp.Close();
return ipend;
}
public Socket s1;
public void Sender()
{
IPAddress ip1=IPAddress.Parse(ip);
IPEndPoint ipend1 = new IPEndPoint(ip1, port);
s1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
byte[] b=Encoding.GetEncoding("gb2312").GetBytes(SendContend);
s1.SendTo(b,ipend1);
s1.Close();
}
{
ipend = new IPEndPoint(IPAddress.Any, port);
Udp = new UdpClient(1314);
byte[] getinformation = Udp.Receive(ref ipend); contend = Encoding.GetEncoding("gb2312").GetString(getinformation); Thread.Sleep(30);
Udp.Close();
return ipend;
}
public Socket s1;
public void Sender()
{
IPAddress ip1=IPAddress.Parse(ip);
IPEndPoint ipend1 = new IPEndPoint(ip1, port);
s1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
byte[] b=Encoding.GetEncoding("gb2312").GetBytes(SendContend);
s1.SendTo(b,ipend1);
s1.Close();
}
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询