using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace UDP
{
class Program
{
private static UdpClient udpSender;
private static UdpClient udpRecver;
static void Main(string[] args)
{
//BeginListen();
IPEndPoint localIpEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345); //本机ip,发送的ip地址
udpSender = new UdpClient(localIpEP);
Thread thrSend = new Thread(SendMessage); 发送的线程
thrSend.Start("hello,this is server");
BeginListen(); 开启接收线程
Console.WriteLine("server started");
Console.Read();
}
private static void BeginListen()
{
IPEndPoint localIpep = new IPEndPoint(
IPAddress.Parse("127.0.0.1"), 8849); // 本机IP和监听端口号 ********************这个监听端口是必须的(个人的理解)
udpRecver = new UdpClient(localIpep);
Thread thrReceive = new Thread(ReceiveMessage);
thrReceive.Start();
Console.WriteLine("client started");
}
private static void ReceiveMessage()
{
// throw new NotImplementedException();
IPEndPoint remoteIpEP = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
// 从远处接收到的数据
byte[] bytReceive = udpRecver.Receive(ref remoteIpEP);
string msg = Encoding.Unicode.GetString(bytReceive, 0, bytReceive.Length);
Console.WriteLine("received from server");
Console.WriteLine(msg);
}
}
private static void SendMessage(object obj)
{
//throw new NotImplementedException();
// string msg = (string)obj;
string msg ="this is send data";
byte[] sendbytes = Encoding.Unicode.GetBytes(msg);
//发送到的ip地址
IPEndPoint remoteIpEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8849); 发送到的ip地址
udpSender.Send(sendbytes, sendbytes.Length, remoteIpEP);
udpSender.Close();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace UDP
{
class Program
{
private static UdpClient udpSender;
private static UdpClient udpRecver;
static void Main(string[] args)
{
//BeginListen();
IPEndPoint localIpEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345); //本机ip,发送的ip地址
udpSender = new UdpClient(localIpEP);
Thread thrSend = new Thread(SendMessage); 发送的线程
thrSend.Start("hello,this is server");
BeginListen(); 开启接收线程
Console.WriteLine("server started");
Console.Read();
}
private static void BeginListen()
{
IPEndPoint localIpep = new IPEndPoint(
IPAddress.Parse("127.0.0.1"), 8849); // 本机IP和监听端口号 ********************这个监听端口是必须的(个人的理解)
udpRecver = new UdpClient(localIpep);
Thread thrReceive = new Thread(ReceiveMessage);
thrReceive.Start();
Console.WriteLine("client started");
}
private static void ReceiveMessage()
{
// throw new NotImplementedException();
IPEndPoint remoteIpEP = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
// 从远处接收到的数据
byte[] bytReceive = udpRecver.Receive(ref remoteIpEP);
string msg = Encoding.Unicode.GetString(bytReceive, 0, bytReceive.Length);
Console.WriteLine("received from server");
Console.WriteLine(msg);
}
}
private static void SendMessage(object obj)
{
//throw new NotImplementedException();
// string msg = (string)obj;
string msg ="this is send data";
byte[] sendbytes = Encoding.Unicode.GetBytes(msg);
//发送到的ip地址
IPEndPoint remoteIpEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8849); 发送到的ip地址
udpSender.Send(sendbytes, sendbytes.Length, remoteIpEP);
udpSender.Close();
}
}
}
本文介绍了一个使用C#实现的简单UDP客户端和服务端通信的例子。通过创建两个线程分别用于发送和接收数据,演示了如何在本地计算机上进行UDP数据包的收发。此例涉及线程启动、数据编码及端口监听等关键技术点。

被折叠的 条评论
为什么被折叠?



