今天开始学习UDP协议,因为UDP协议是无连接的协议。因此,为了使服务器应用能够接收到UDP数据包,必须注意两件事:
1、创建一个Socket对象
2、使创建的套接字和本地的一个端口绑定
完成这两件工作后服务器端就可以从指定的端口接收UDP数据包,或者收将数据包发送到指定的设备上。
UDP中不使用标准的Send()和Receive()方法,而是使用新的两个方法SendTo()和ReceiveFrom()
由于服务器跟客户端是不建立连接的,所以服务器接收到数据后可以用 ReceiveFrom(data, ref remote);把远程发送数据端的IP和端口放到remote中,然后就可以原路返回
服务器端程序
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Text;
namespace UdpServ
{
class Program
{
static void Main(string[] args)
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 8899);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.Bind(ipEnd);
Console.Write("waiting for a client");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint remote = (EndPoint)(sender);
recv = sock.ReceiveFrom(data, ref remote);
Console.WriteLine("Message receive from {0}:", remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
string welcome = "welcome to my server";
data = Encoding.ASCII.GetBytes(welcome);
sock.SendTo(data, data.Length, SocketFlags.None, remote);
while (true)
{
data = new byte[1024];
recv = sock.ReceiveFrom(data, ref remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
sock.SendTo(data, recv, SocketFlags.None, remote);
}
}
}
}
//客户端程序:
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Text;
namespace UdpClient
{
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];
string input, stringData;
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("192.168.123.108"), 8899);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "hello";
data = Encoding.ASCII.GetBytes(welcome);
socket.SendTo(data, data.Length, SocketFlags.None, ipEnd);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint remote = (EndPoint)sender;
data = new byte[1024];
int recv = socket.ReceiveFrom(data, ref remote);
Console.WriteLine("Message receive from {0}:", remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
while (true)
{
input = Console.ReadLine();
if (input == "exit")
break;
socket.SendTo(Encoding.ASCII.GetBytes(input), remote);
data = new byte[1024];
recv = socket.ReceiveFrom(data, ref remote);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
Console.WriteLine("stop client");
socket.Close();
}
}
}
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Text;
namespace UdpServ
{
class Program
{
static void Main(string[] args)
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 8899);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.Bind(ipEnd);
Console.Write("waiting for a client");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint remote = (EndPoint)(sender);
recv = sock.ReceiveFrom(data, ref remote);
Console.WriteLine("Message receive from {0}:", remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
string welcome = "welcome to my server";
data = Encoding.ASCII.GetBytes(welcome);
sock.SendTo(data, data.Length, SocketFlags.None, remote);
while (true)
{
data = new byte[1024];
recv = sock.ReceiveFrom(data, ref remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
sock.SendTo(data, recv, SocketFlags.None, remote);
}
}
}
}
//客户端程序:
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Text;
namespace UdpClient
{
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];
string input, stringData;
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse("192.168.123.108"), 8899);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "hello";
data = Encoding.ASCII.GetBytes(welcome);
socket.SendTo(data, data.Length, SocketFlags.None, ipEnd);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint remote = (EndPoint)sender;
data = new byte[1024];
int recv = socket.ReceiveFrom(data, ref remote);
Console.WriteLine("Message receive from {0}:", remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
while (true)
{
input = Console.ReadLine();
if (input == "exit")
break;
socket.SendTo(Encoding.ASCII.GetBytes(input), remote);
data = new byte[1024];
recv = socket.ReceiveFrom(data, ref remote);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
Console.WriteLine("stop client");
socket.Close();
}
}
}