using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace Crltest { public class Program { static string msg = null; static void Main(string[] args) { //创建一个新的Socket,这里我们使用最常用的基于TCP的Stream Socket(流式套接字) var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //将该socket绑定到主机上面的某个端口 //方法参考:http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.bind.aspx socket.Bind(new IPEndPoint(IPAddress.Any, 4530)); //启动监听,并且设置一个最大的队列长度 //方法参考:http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.listen(v=VS.100).aspx socket.Listen(4); //开始接受客户端连接请求 //方法参考:http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.beginaccept.aspx socket.BeginAccept(new AsyncCallback((ar) => { //这就是客户端的Socket实例,我们后续可以将其保存起来 var client = socket.EndAccept(ar); //给客户端发送一个欢迎消息 client.Send(Encoding.Unicode.GetBytes("Hi there, I accept you request at " + DateTime.Now.ToString())); //实现每隔两秒钟给服务器发一个消息 //这里我们使用了一个定时器 var timer = new System.Timers.Timer(); timer.Interval = 2000D; timer.Enabled = true; timer.Elapsed += (o, a) => { //检测客户端Socket的状态 if (client.Connected) { try { string strA = Console.ReadLine(); client.Send(Encoding.Unicode.GetBytes(strA)); //client.Send(Encoding.Unicode.GetBytes("Message from server at " + DateTime.Now.ToString())); } catch (SocketException ex) { Console.WriteLine(ex.Message); } } else { timer.Stop(); timer.Enabled = false; Console.WriteLine("Client is disconnected, the timer is stop."); } }; timer.Start(); //接收客户端的消息(这个和在客户端实现的方式是一样的) client.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), client); }), null); Console.WriteLine("Server is ready!"); while (true) { if (msg == "1\0") { Console.WriteLine("111111111111111111111111111111111111"); msg = "0"; } } Console.Read(); } static byte[] buffer = new byte[1024]; public static void ReceiveMessage(IAsyncResult ar) { try { var socket = ar.AsyncState as Socket; //方法参考:http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.endreceive.aspx var length = socket.EndReceive(ar); //读取出来消息内容 msg = Encoding.Unicode.GetString(buffer, 0, length); //显示消息 Console.WriteLine(msg); //接收下一个消息(因为这是一个递归的调用,所以这样就可以一直接收消息了) socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), socket); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
标签:LYT,Console,Socket,C#,System,timer,using,socket From: https://www.cnblogs.com/ViolinHuang/p/17320803.html