Server端代碼:
黃石港網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),黃石港網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為黃石港上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的黃石港做網(wǎng)站的公司定做!class Program { static SocketListener listener; public static void Main(string[] args) { //實(shí)例化Timer類,設(shè)置間隔時間為5000毫秒; System.Timers.Timer t = new System.Timers.Timer(5000); t.Elapsed += new System.Timers.ElapsedEventHandler(CheckListen); //到達(dá)時間的時候執(zhí)行事件; t.AutoReset = true; t.Start(); listener = new SocketListener(); listener.ReceiveTextEvent += new SocketListener.ReceiveTextHandler(ShowText); listener.StartListen(); Console.ReadKey(); } private static void ShowText(string text) { Console.WriteLine(text); } private static void CheckListen(object sender, System.Timers.ElapsedEventArgs e) { if (listener != null && listener.Connection != null) { Console.WriteLine("連接數(shù):" + listener.Connection.Count.ToString()); } } } public class Connection { Socket _connection; public Connection(Socket socket) { _connection = socket; } public void WaitForSendData(object connection) { try { while (true) { byte[] bytes = new byte[1024]; string data = ""; //等待接收消息 int bytesRec = this._connection.Receive(bytes); if (bytesRec == 0) { // ReceiveText("客戶端[" + _connection.RemoteEndPoint.ToString() + "]連接關(guān)閉..."); break; } data += Encoding.UTF8.GetString(bytes, 0, bytesRec); ReceiveText("收到消息:" + data); string sendStr = "服務(wù)端已經(jīng)收到信息!"; byte[] bs = Encoding.UTF8.GetBytes(sendStr); _connection.Send(bs, bs.Length, 0); } } catch (Exception) { ReceiveText("客戶端[" + _connection.RemoteEndPoint.ToString() + "]連接已斷開..."); Hashtable hConnection = connection as Hashtable; if (hConnection.Contains(_connection.RemoteEndPoint.ToString())) { hConnection.Remove(_connection.RemoteEndPoint.ToString()); } } } public delegate void ReceiveTextHandler(string text); public event ReceiveTextHandler ReceiveTextEvent; private void ReceiveText(string text) { if (ReceiveTextEvent != null) { ReceiveTextEvent(text); } } } public class SocketListener { public Hashtable Connection = new Hashtable(); public void StartListen() { Agine: try { //端口號、IP地址 //int port = 8889; //string host = "127.0.0.1"; //IPAddress ip = IPAddress.Parse(host); //IPEndPoint ipe = new IPEndPoint(ip, port); string ip = string.Empty; System.Net.IPHostEntry IpEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()); for (int i = 0; i != IpEntry.AddressList.Length; i++) { if (!IpEntry.AddressList[i].IsIPv6LinkLocal) { ip = IpEntry.AddressList[i].ToString(); } } IPEndPoint ipend = new IPEndPoint(IPAddress.Parse(ip), 6000); //創(chuàng)建一個Socket類 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Bind(ipend);//綁定2000端口 s.Listen(0);//開始監(jiān)聽 ReceiveText("啟動Socket監(jiān)聽..."); while (true) { Socket connectionSocket = s.Accept();//為新建連接創(chuàng)建新的Socket ReceiveText("客戶端[" + connectionSocket.RemoteEndPoint.ToString() + "]連接已建立..."); Connection gpsCn = new Connection(connectionSocket); gpsCn.ReceiveTextEvent += new Connection.ReceiveTextHandler(ReceiveText); Connection.Add(connectionSocket.RemoteEndPoint.ToString(), gpsCn); //在新線程中啟動新的socket連接,每個socket等待,并保持連接 Thread thread = new Thread(gpsCn.WaitForSendData); thread.Name = connectionSocket.RemoteEndPoint.ToString(); thread.Start(Connection); } } catch (ArgumentNullException ex1) { ReceiveText("ArgumentNullException:" + ex1); } catch (SocketException ex2) { ReceiveText("SocketException:" + ex2); } goto Agine; } public delegate void ReceiveTextHandler(string text); public event ReceiveTextHandler ReceiveTextEvent; private void ReceiveText(string text) { if (ReceiveTextEvent != null) { ReceiveTextEvent(text); } } }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。