Exemplo n.º 1
0
 private void frmClient_FormClosing(object sender, FormClosingEventArgs e)
 {
     if (net.CanWrite == true)
     {
         NetMsg msg = new NetMsg();
         msg.msg = "QUIT";
         byte[] tmp = XuLie.ObjToByte(msg);
         try
         {
             net.Write(tmp, 0, tmp.Length);
         }
         catch (IOException)
         {
             textBox1.Text += "已经从服务器断开连接\r\n";
             Client.Close();
             Client = null;
             return;
         }
     }
     Client = null;
     GC.Collect();
     Application.ExitThread();
 }
Exemplo n.º 2
0
 private void ThSub()
 {
     try
     {
         while (Client != null)
         {
             NetworkStream Net = Client.GetStream();
             if (Net.DataAvailable == true) //有数据。
             {
                 byte[] tmp = new byte[1024];
                 if (Net.CanRead == true)
                 {
                     MemoryStream memory = new MemoryStream();
                     memory.Position = 0;
                     int len = 1;
                     while (len != 0)
                     {
                         if (Net.DataAvailable == false)
                         {
                             break;
                         }
                         len = Net.Read(tmp, 0, tmp.Length);
                         memory.Write(tmp, 0, len);
                     }
                     log.LogWriter("接收完毕");
                     NetMsg msg = (NetMsg)XuLie.ByteToObj(memory.ToArray());
                     log.LogWriter("序列化完毕");
                     TcpClient tcpclient = new TcpClient();
                     log.LogWriter("建立TCP对象");
                     if (msg.Fip != null) //非心跳包。
                     {
                         try
                         {
                             tcpclient.Connect(msg.JieIP, msg.port);
                             NetworkStream SubNet = tcpclient.GetStream();
                             byte[]        Tmp    = XuLie.ObjToByte(msg);
                             SubNet.Write(Tmp, 0, Tmp.Length);
                         }
                         catch (SocketException)
                         {
                             msg.msg = "对方不在线";
                             byte[] Tmp = XuLie.ObjToByte(msg);
                             Net.Write(Tmp, 0, Tmp.Length);
                         }
                     }
                     else
                     {
                         if (msg.msg == "QUIT")
                         {
                             Arr.Remove(Client);
                             return;
                         }
                     }
                     tcpclient.Close();
                     GC.Collect();
                 }
             }
             else //没有数据。
             {
             }
             Thread.Sleep(1000);
         }
     }
     catch
     {
         Arr.Remove(Client);
         th.Abort();
     }
 }