110,561
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName());
//设置服务端终结点
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("我的互联网ip"), 1688);
//创建与服务端连接的套接字,指定网络类型,数据连接类型和网络协议
Socket ConnSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "Client Message:Hello!!!";
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(welcome);
//给服务端发送测试消息
ConnSocket.SendTo(data, data.Length, SocketFlags.None, ipe);
IPEndPoint server = new IPEndPoint(IPAddress.Any, 0);
//服务端终结点
EndPoint Remote = (EndPoint)server;
data = new byte[1024];
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
//server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
int recv = ConnSocket.ReceiveFrom(data, ref Remote);
//打印从服务端发回的信息
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
while (true) //可以实时给服务端发送消息
{
string input = Console.ReadLine();
if (input == "exit") //中断连接
{
ConnSocket.SendTo(Encoding.ASCII.GetBytes(input), Remote);
data = new byte[1024];
recv = ConnSocket.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
break;
}
else
{
ConnSocket.SendTo(Encoding.ASCII.GetBytes("Client Message:" + input), Remote);
data = new byte[1024];
recv = ConnSocket.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
}
}
Console.WriteLine("Stopping Client.");
ConnSocket.Close();
}
static void Main(string[] args)
{
//设置“终结点”
IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 1899);
//创建与客户机连接的套接字(基于UDP协议无需侦听Socket)
Socket ConnSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//绑定网络地址
ConnSocket.Bind(ipe);
Console.WriteLine("This is a Server, host name is {0}", Dns.GetHostName());
//等待客户机连接
Console.WriteLine("Waiting for a client");
IPEndPoint client = new IPEndPoint(IPAddress.Any, 0);
//客户机终结点
EndPoint Remote = (EndPoint)(client);
byte[] data = new byte[1024];
//从客户机接收信息,并将信息保存到数据缓冲区data
int recv = ConnSocket.ReceiveFrom(data, ref Remote);
//将接收的信息(客户机ip及端口号和文本信息)打印出来
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
//客户机连接成功后,发送欢迎信息
string welcome = "Server Message:Welcome!!!";
//字符串与字节数组相互转换
data = Encoding.ASCII.GetBytes(welcome);
//给客户机发送信息
ConnSocket.SendTo(data, data.Length, SocketFlags.None, Remote);
while (true) //时刻准备接收从客户机发回的信息
{
data = new byte[1024];
recv = ConnSocket.ReceiveFrom(data, ref Remote);
if (Encoding.ASCII.GetString(data, 0, recv) == "exit")
{
Console.WriteLine("Client has been exit from the connection.");
//给客户机发送一条"exit"消息表示欲关闭连接
welcome = "Server exit!!!";
data = Encoding.ASCII.GetBytes(welcome);
ConnSocket.SendTo(data, data.Length, SocketFlags.None, Remote);
ConnSocket.Close();
break;
}
else
{
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
//告知客户机当前信息已成功接收
welcome = "Server has been received successfully!!!";
data = Encoding.ASCII.GetBytes(welcome);
ConnSocket.SendTo(data, data.Length, SocketFlags.None, Remote);
}
}
}
System.Net.Sockets.UdpClient uc = new System.Net.Sockets.UdpClient(5678);
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 0);
byte[]data = uc.Receive(ref ip);