1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 using System.Net.Sockets; 7 using System.Net; 8 9 namespace SocketServerAcceptMultipleClient 10 { 11 public class SocketServer 12 { 13 // 创建一个和客户端通信的套接字 14 static Socket socketwatch = null; 15 //定义一个集合,存储客户端信息 16 static Dictionary<string, Socket> clientConnectionItems = new Dictionary<string, Socket> { }; 17 18 public static void Main(string[] args) 19 { 20 //定义一个套接字用于监听客户端发来的消息,包含三个参数(IP4寻址协议,流式连接,Tcp协议) 21 socketwatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 22 //服务端发送信息需要一个IP地址和端口号 23 IPAddress address = IPAddress.Parse("127.0.0.1"); 24 //将IP地址和端口号绑定到网络节点point上 25 IPEndPoint point = new IPEndPoint(address, 8098); 26 //此端口专门用来监听的 27 28 //监听绑定的网络节点 29 socketwatch.Bind(point); 30 31 //将套接字的监听队列长度限制为20 32 socketwatch.Listen(20); 33 34 //负责监听客户端的线程:创建一个监听线程 35 Thread threadwatch = new Thread(watchconnecting); 36 37 //将窗体线程设置为与后台同步,随着主线程结束而结束 38 threadwatch.IsBackground = true; 39 40 //启动线程 41 threadwatch.Start(); 42 43 Console.WriteLine("开启监听。。。"); 44 Console.WriteLine("点击输入任意数据回车退出程序。。。"); 45 Console.ReadKey(); 46 Console.WriteLine("退出监听,并关闭程序。"); 47 } 48 49 //监听客户端发来的请求 50 static void watchconnecting() 51 { 52 Socket connection = null; 53 54 //持续不断监听客户端发来的请求 55 while (true) 56 { 57 try 58 { 59 connection = socketwatch.Accept(); 60 } 61 catch (Exception ex) 62 { 63 //提示套接字监听异常 64 Console.WriteLine(ex.Message); 65 break; 66 } 67 68 //获取客户端的IP和端口号 69 IPAddress clientIP = (connection.RemoteEndPoint as IPEndPoint).Address; 70 int clientPort = (connection.RemoteEndPoint as IPEndPoint).Port; 71 72 //让客户显示"连接成功的"的信息 73 string sendmsg = "连接服务端成功!\r\n" + "本地IP:" + clientIP + ",本地端口" + clientPort.ToString(); 74 byte[] arrSendMsg = Encoding.UTF8.GetBytes(sendmsg); 75 connection.Send(arrSendMsg); 76 77 //客户端网络结点号 78 string remoteEndPoint = connection.RemoteEndPoint.ToString(); 79 //显示与客户端连接情况 80 Console.WriteLine("成功与" + remoteEndPoint + "客户端建立连接!\t\n"); 81 //添加客户端信息 82 clientConnectionItems.Add(remoteEndPoint, connection); 83 84 //IPEndPoint netpoint = new IPEndPoint(clientIP,clientPort); 85 IPEndPoint netpoint = connection.RemoteEndPoint as IPEndPoint; 86 87 //创建一个通信线程 88 ParameterizedThreadStart pts = new ParameterizedThreadStart(recv); 89 Thread thread = new Thread(pts); 90 //设置为后台线程,随着主线程退出而退出 91 thread.IsBackground = true; 92 //启动线程 93 thread.Start(connection); 94 } 95 } 96 97 /// <summary> 98 /// 接收客户端发来的信息,客户端套接字对象 99 /// </summary> 100 /// <param name="socketclientpara"></param> 101 static void recv(object socketclientpara) 102 { 103 Socket socketServer = socketclientpara as Socket; 104 105 while (true) 106 { 107 //创建一个内存缓冲区,其大小为1024*1024字节 即1M 108 byte[] arrServerRecMsg = new byte[1024 * 1024]; 109 //将接收到的信息存入到内存缓冲区,并返回其字节数组的长度 110 try 111 { 112 int length = socketServer.Receive(arrServerRecMsg); 113 114 //将机器接受到的字节数组转换为人可以读懂的字符串 115 string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length); 116 117 //将发送的字符串信息附加到文本框txtMsg上 118 Console.WriteLine("客户端:" + socketServer.RemoteEndPoint + ",time:" + GetCurrentTime() + "\r\n" + strSRecMsg + "\r\n\n"); 119 120 socketServer.Send(Encoding.UTF8.GetBytes("测试server 是否可以发送数据给client ")); 121 } 122 catch (Exception ex) 123 { 124 clientConnectionItems.Remove(socketServer.RemoteEndPoint.ToString()); 125 126 Console.WriteLine("Client Count:" + clientConnectionItems.Count); 127 128 //提示套接字监听异常 129 Console.WriteLine("客户端" + socketServer.RemoteEndPoint + "已经中断连接" + "\r\n" + ex.Message + "\r\n" + ex.StackTrace + "\r\n"); 130 //关闭之前accept出来的和客户端进行通信的套接字 131 socketServer.Close(); 132 break; 133 } 134 } 135 } 136 137 /// 138 /// 获取当前系统时间的方法 139 /// 当前时间 140 static DateTime GetCurrentTime() 141 { 142 DateTime currentTime = new DateTime(); 143 currentTime = DateTime.Now; 144 return currentTime; 145 } 146 } 147 }
原文链接:https://www.cnblogs.com/yy3b2007com/p/7476458.html
原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/7045