Пример #1
0
 // 使用套接字客户端发送消息
 static void SendMessage(SocketClient client, string data)
 {
     try
     {
         client.SendAsync(data);
     }
     catch (Exception ex)
     {
         Console.WriteLine("当发送消息时,发生异常:\n" + ex.Message);
         client.Close();
         Console.WriteLine("客户端断开连接。");
     }
 }
Пример #2
0
 // 连接的客户端
 static void socketp2_SocketConnected(Socket sock)
 {
     // 创建新的线程来处理客户会话
     new Thread(
         new ThreadStart(delegate
     {
         // 初始化套接字客户端
         var client = new SocketClient(sock);
         try
         {
             client.MessageReceived += new EventHandler<SocketMessageEventArgs>(client_MessageReceived);
             client.MessageSended += new EventHandler<SocketMessageEventArgs>(client_MessageSended);
             // 准备接收
             client.StartReceiving();
             Console.WriteLine("客户端已连接!");
         }
         catch (Exception ex)
         {
             Console.WriteLine("当开始接收消息时发生异常:\n" + ex.Message);
             client.Close();
         }
     })).Start();
 }