UDP协议。
看代码:
服务端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDPServer
{
class Program
{
static void Main( string[] args)
{
int recv;
byte[] data = new byte[1024];
//构建TCP 服务器
//得到本机IP,设置TCP端口号
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse( "192.168.1.125"), 8001);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram , ProtocolType.Udp);
//绑定网络地址
newsock.Bind(ipep);
Console.WriteLine( "这里是服务器, 主机名是 {0}",Dns.GetHostName());
//等待客户机连接
Console.WriteLine( "等待客户端连接");
//得到客户机IP
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote); //会阻塞
Console .WriteLine ( "消息来自 {0}: ", Remote.ToString ());
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
//客户机连接成功后,发送欢迎信息
string welcome = "欢迎 ! ";
//字符串与字节数组相互转换
//data = Encoding .ASCII .GetBytes(welcome);
data = Encoding.UTF8.GetBytes( "服务端>>>"+welcome);
//发送信息
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
while ( true)
{
data = new byte [1024];
//发送接受信息
recv =newsock.ReceiveFrom(data , ref Remote);
Console.Write( "客户端>>>");
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
//向客户端回复信息
newsock.SendTo(data,recv,SocketFlags.None, Remote);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UDPServer
{
class Program
{
static void Main( string[] args)
{
int recv;
byte[] data = new byte[1024];
//构建TCP 服务器
//得到本机IP,设置TCP端口号
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse( "192.168.1.125"), 8001);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram , ProtocolType.Udp);
//绑定网络地址
newsock.Bind(ipep);
Console.WriteLine( "这里是服务器, 主机名是 {0}",Dns.GetHostName());
//等待客户机连接
Console.WriteLine( "等待客户端连接");
//得到客户机IP
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote); //会阻塞
Console .WriteLine ( "消息来自 {0}: ", Remote.ToString ());
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
//客户机连接成功后,发送欢迎信息
string welcome = "欢迎 ! ";
//字符串与字节数组相互转换
//data = Encoding .ASCII .GetBytes(welcome);
data = Encoding.UTF8.GetBytes( "服务端>>>"+welcome);
//发送信息
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
while ( true)
{
data = new byte [1024];
//发送接受信息
recv =newsock.ReceiveFrom(data , ref Remote);
Console.Write( "客户端>>>");
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
//向客户端回复信息
newsock.SendTo(data,recv,SocketFlags.None, Remote);
}
}
}
}
客户端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UdpClient
{
class Program
{
static void Main( string[] args)
{
byte[] data = new byte[1024];
string input, stringData;
Console.WriteLine( "这里是客户端, 主机名是 {0}", Dns.GetHostName());
//设置服务端IP,设置TCP端口号
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse( "192.168.1.125"), 8001);
//定义网络类型,数据连接类型和网络协议UDP
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "你好! ";
data = Encoding.UTF8.GetBytes( "客户端>>>" + welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep); //向服务端发送数据
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;
data = new byte[1024];
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
//server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
int recv = server.ReceiveFrom(data, ref Remote); //将数据接收放入缓冲区
Console.WriteLine( "消息来自服务端 {0}: ", Remote.ToString());
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
while ( true)
{
input = Console.ReadLine();
//如果输入exit则退出
if (input == "exit" || input== "quit")
break;
//input = "客户端>>>" + input;
server.SendTo(Encoding.UTF8.GetBytes(input), Remote);
data = new byte[1024];
recv = server.ReceiveFrom(data, ref Remote);
stringData = Encoding.UTF8.GetString(data, 0, recv);
Console.Write( "服务端回复>>>");
Console.WriteLine(stringData);
}
Console.WriteLine( "客户端关闭.");
server.Close();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace UdpClient
{
class Program
{
static void Main( string[] args)
{
byte[] data = new byte[1024];
string input, stringData;
Console.WriteLine( "这里是客户端, 主机名是 {0}", Dns.GetHostName());
//设置服务端IP,设置TCP端口号
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse( "192.168.1.125"), 8001);
//定义网络类型,数据连接类型和网络协议UDP
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "你好! ";
data = Encoding.UTF8.GetBytes( "客户端>>>" + welcome);
server.SendTo(data, data.Length, SocketFlags.None, ipep); //向服务端发送数据
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;
data = new byte[1024];
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
//server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
int recv = server.ReceiveFrom(data, ref Remote); //将数据接收放入缓冲区
Console.WriteLine( "消息来自服务端 {0}: ", Remote.ToString());
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
while ( true)
{
input = Console.ReadLine();
//如果输入exit则退出
if (input == "exit" || input== "quit")
break;
//input = "客户端>>>" + input;
server.SendTo(Encoding.UTF8.GetBytes(input), Remote);
data = new byte[1024];
recv = server.ReceiveFrom(data, ref Remote);
stringData = Encoding.UTF8.GetString(data, 0, recv);
Console.Write( "服务端回复>>>");
Console.WriteLine(stringData);
}
Console.WriteLine( "客户端关闭.");
server.Close();
}
}
}
研究一个教程,只做了些改动。
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/318765
,如需转载请自行联系原作者