public class XmlSocket { //非同步socket診聽 // Incoming data from client.從用戶端傳來的資料 public static string data = null; // Thread signal.線程 用一個指示是否將初始狀態設定為終止的布爾值初始化 ManualResetEvent 類的新執行個體。 public static ManualResetEvent allDone = new ManualResetEvent(false); //static void Main(string[] args) //{ // StartListening(); //} public static void StartListening() { // Data buffer for incoming data. 傳入資料緩衝 byte[] bytes = new Byte[1024]; // Establish the local endpoint for the socket. 建立本地連接埠 // The DNS name of the computer // running the listener is "host.contoso.com". IPAddress ipAddress; String ipString = ConfigurationManager.AppSettings.Get("SocketIP"); if (ipString==null || ipString ==String.Empty) { IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); ipAddress = ipHostInfo.AddressList[0]; } else { ipAddress = IPAddress.Parse(ipString); } int port; String portString = ConfigurationManager.AppSettings.Get("SocketPort"); if (portString==null || portString==String.Empty) { port = 11001; } else { port = int.Parse(portString); } IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and listen for incoming connections.綁定連接埠和資料 try { listener.Bind(localEndPoint); listener.Listen(100); while (true) { // Set the event to nonsignaled state.設定無訊號狀態的事件 allDone.Reset(); // Start an asynchronous socket to listen for connections.重新 啟動非同步串連 listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); // Wait until a connection is made before continuing.等待串連建立後繼續 allDone.WaitOne(); } } catch (Exception e) { // } } public static void AcceptCallback(IAsyncResult ar) { try { // Signal the main thread to continue.接受回調方法 該方法的此節向主應用程式線程發出訊號, //讓它繼續處理並建立與用戶端的串連 allDone.Set(); // Get the socket that handles the client request.擷取用戶端請求控制代碼 Socket listener = (Socket)ar.AsyncState; Socket handler = listener.EndAccept(ar); // Create the state object. StateObject state = new StateObject(); state.workSocket = handler; handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } catch (Exception e) { // } } /// <summary> /// 與接受回調方法一樣,讀取回調方法也是一個 AsyncCallback 委託。 /// 該方法將來自用戶端通訊端的一個或多個位元組讀入資料緩衝區,然後再次調用 BeginReceive 方法,直到用戶端發送的資料完成為止。 /// 從用戶端讀取整個訊息後,在控制台上顯示字串,並關閉處理與用戶端的已連線的服務器通訊端。 /// </summary> /// <param name="ar">IAsyncResult 委託</param> public static void ReadCallback(IAsyncResult ar) { try { String content = String.Empty; // Retrieve the state object and the handler socket建立自訂的狀態物件 from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket;//處理的控制代碼 // Read data from the client socket. 讀出 int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { //業務代碼 string result = DoSomeThing(...); String len = Encoding.UTF8.GetBytes(result).Length.ToString().PadLeft(8, '0'); log.writeLine(len); Send(len + result, handler); } } catch (Exception e) { // } } private static void Send(String data, Socket handler) { try { // Convert the string data to byte data using UTF8 encoding. byte[] byteData = Encoding.UTF8.GetBytes(data); // Begin sending the data to the remote device. handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler); } catch (Exception e) { // } } /// <summary> /// 發送 /// </summary> /// <param name="ar"></param> private static void SendCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket handler = (Socket)ar.AsyncState; // Complete sending the data to the remote device.向遠端發送資料 int bytesSent = handler.EndSend(ar); StateObject state = new StateObject(); state.workSocket = handler; handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,new AsyncCallback(ReadCallback), state); handler.Shutdown(SocketShutdown.Both); handler.Close(); } catch (Exception e) {// } } public static void StopListening() { allDone.Close(); log.close(); } /// <summary> /// 具體處理業務的方法 /// </summary> /// <returns></returns> private static string DoSomething(int i) { //具體業務代碼,返回需要返回的字串資訊 } /// <summary> /// 寫日誌方法 /// </summary> /// <param name="strLog">寫入內容</param> public static void WriteLog(string strLog) { //寫入日誌代碼 } }
/// 線程執行體,轉寄訊息 /// </summary> /// <param name="obj">傳遞給線程執行體的使用者名稱,用以與使用者通訊 </param> private void ThreadFunc(object obj) { //通過轉寄表得到目前使用者通訊端 Socket clientSkt = _transmit_tb[obj] as Socket; //主迴圈 while (true) { try { //接受第一個資料包。 //由於程式邏輯結構簡單,所以在這裡對客戶機發送的第一個包內容作逐一判斷, //這裡的實現不夠優雅,但不失為此簡單模型的一個解決之道。 byte[] packetBuff = new byte[_maxPacket]; clientSkt.Receive(packetBuff); string _str = Encoding.Unicode.GetString(packetBuff).TrimEnd('\0'); //如果是發給不線上好友的資訊 if (_str.StartsWith("cmd::FriendMessage")) { string UserName = _str.Substring("cmd::FriendMessage".Length, 20).Trim(); string MessageS = _str.Substring("cmd::FriendMessage".Length + 20, _str.Length - "cmd::FriendMessage".Length - 20); SaveMessage(obj as string, UserName, MessageS); continue; } //如果是離線請求 if (_str.StartsWith("cmd::RequestLogout")) { _transmit_tb.Remove(obj); UpdateFriendList((string)obj, false, ""); // string svrlog = string.Format("[系統訊息]使用者 {0} 在 {1} 已斷開... 當前線上人數: {2}\r\n\r\n", obj, DateTime.Now, _transmit_tb.Count); // Console.WriteLine(svrlog); //向所有客戶機發送系統訊息 //foreach (DictionaryEntry de in _transmit_tb) //{ // string _clientName = de.Key as string; // Socket _clientSkt = de.Value as Socket; // _clientSkt.Send(Encoding.Unicode.GetBytes(svrlog)); //} Thread.CurrentThread.Abort(); } //如果是請求好友名單 if (_str.StartsWith("cmd::RequestFriendList")) { SerializeFriendList(obj, clientSkt); // 將該使用者不線上時的資訊發送給使用者 DataTable TabMessage = ReadMessage(obj as string); if (TabMessage != null) { foreach (DataRow myrow in TabMessage.Rows) { if (myrow["SendUserName"].ToString() == "System::Message") { clientSkt.Send(Encoding.Unicode.GetBytes(myrow["Message"].ToString())); } else { clientSkt.Send(Encoding.Unicode.GetBytes("cmd::FriendMessage" + myrow["SendUserName"].ToString().PadRight(20, ' ') + myrow["Message"].ToString())); } } } //這裡不需要再繼續接受後繼資料包了,跳出當前迴圈體。 continue; } ////如果是請求好友名單 //if (_str.StartsWith("cmd::RequestOnLineList")) //{ // byte[] onlineBuff = SerializeOnlineList(); // //先發送響應訊號,使用者客戶機的判斷 // clientSkt.Send(Encoding.Unicode.GetBytes("cmd::RequestOnLineList")); // clientSkt.Send(onlineBuff); // //這裡不需要再繼續接受後繼資料包了,跳出當前迴圈體。 // continue; //} //尋找使用者 if (_str.StartsWith("Find::FindFriend")) { DataTable TabFind = TabUser.Clone(); DataRow [] FindRow =null ; string UserName = _str.Substring("Find::FindFriend".Length, _str.Length - "Find::FindFriend".Length); if (UserName.Equals("Find::WhoOnLine")) { //看誰線上 FindRow = TabUser.Select(" ZX = 1"); } else//精確尋找 { FindRow = TabUser.Select("UserName = '" + UserName + "'"); } foreach (DataRow myrow in FindRow) { TabFind.ImportRow(myrow); } clientSkt.Send(Encoding.Unicode.GetBytes("Find::FindFriend")); IFormatter format = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); format.Serialize(stream, TabFind); stream.Position = 0; byte[] ret = new byte[_maxPacket]; int count = 0; count = stream.Read(ret, 0, _maxPacket); while (count >0) { clientSkt.Send(ret); count = stream.Read(ret, 0, _maxPacket); } clientSkt.Send(Encoding.Unicode.GetBytes("Find::FindFriendEnd")); stream.Close(); TabFind = null; FindRow = null; //這裡不需要再繼續接受後繼資料包了,跳出當前迴圈體。 continue; } //請求添加好友 if (_str.StartsWith("Find::AddFriendAsk")) { string UserName = _str.Substring("Find::AddFriendAsk".Length, _str.Length - "Find::AddFriendAsk".Length); //通過轉寄表尋找接收方的通訊端 if (_transmit_tb.Count != 0 && _transmit_tb.ContainsKey(UserName)) { Socket receiverSkt = _transmit_tb[UserName] as Socket; receiverSkt.Send(Encoding.Unicode.GetBytes("Find::AddFriendAsk" + obj as string)); } //這裡不需要再繼續接受後繼資料包了,跳出當前迴圈體。 continue; } //回複答應添加好友 if (_str.StartsWith("Find::AddFriendYes")) { string UserName = _str.Substring("Find::AddFriendYes".Length, _str.Length - "Find::AddFriendYes".Length); //// 儲存資料 DataTable TabmyFriend = new DataTable() ; //儲存該使用者 TabmyFriend.ReadXml(MyPath + "\\UserFriend\\" + obj as string + ".xml"); DataRow newRow = TabmyFriend.NewRow(); newRow["UserName"] = UserName; TabmyFriend.Rows.Add(newRow); TabmyFriend.WriteXml(MyPath + "\\UserFriend\\" + obj as string + ".xml", XmlWriteMode.WriteSchema, false); //儲存其好友 TabmyFriend = new DataTable(); TabmyFriend.ReadXml(MyPath + "\\UserFriend\\" + UserName + ".xml"); DataRow newRow1 = TabmyFriend.NewRow(); newRow1["UserName"] = obj as string; TabmyFriend.Rows.Add(newRow1); TabmyFriend.WriteXml(MyPath + "\\UserFriend\\" + UserName + ".xml", XmlWriteMode.WriteSchema, false); TabmyFriend = null; SerializeFriendList(obj, clientSkt);