Beispiel #1
0
 /// <summary>
 /// send默认为文字
 /// </summary>
 public void send()
 {
     //TODO:这个函数需要更改,不再是传string了,而是传C2PMSG类型
     if (!holdToSend.Equals(string.Empty))
     {
         C2PMsg m = new C2PMsg();
         m.type     = (sbyte)(MSGTYPE._STRING_);
         m.strValue = Encoding.UTF8.GetBytes(holdToSend);
         client.Send(SerializeObj(m));
         holdToSend = string.Empty;
     }
 }
Beispiel #2
0
 /// <summary>
 /// upload命令传输文件
 /// </summary>
 /// <param name="cmd"></param>
 /// <param name="fileURL"></param>
 public void send(string cmd, string fileURL)
 {
     //TODO:需要用新线程执行这个函数
     if (fileURL != null)
     {
         C2PMsg c2PMsg = new C2PMsg();
         c2PMsg.type     = (sbyte)MSGTYPE._FILE_;
         c2PMsg.strValue = Encoding.UTF8.GetBytes(cmd);
         c2PMsg.imgValue = GetFileData(fileURL);
         client.Send(SerializeObj(c2PMsg));
         DateTime now = DateTime.Now;
         Console.WriteLine("[{0}/{1}/{2} {3}:{4}:{5}] " + "长度为{6}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, c2PMsg.imgValue.Length);
     }
     else
     {
         C2PMsg c2PMsg = new C2PMsg();
         c2PMsg.type     = (sbyte)MSGTYPE._FILE_;
         c2PMsg.strValue = Encoding.UTF8.GetBytes(cmd);
         client.Send(SerializeObj(c2PMsg));
     }
 }
Beispiel #3
0
 public void loopRecive()
 {
     try
     {
         while (true)
         {
             byte[] bytes = new byte[12 * 1024 * 1024];
             int    len   = client.Receive(bytes);
             Object msg1  = new Object();
             DeserializeToObj(bytes, out msg1);
             C2PMsg msg = (C2PMsg)msg1;
             if (msg.type == (sbyte)MSGTYPE._STRING_)
             {
                 //文本转换
                 string n = Encoding.UTF8.GetString(msg.strValue);
                 //输出信息
                 DateTime now = DateTime.Now;
                 Console.WriteLine("[{0}/{1}/{2} {3}:{4}:{5}] " + "Client" + name + " >", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
                 Console.WriteLine("[{0}/{1}/{2} {3}:{4}:{5}] " + n, now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
                 //信息发给各客户端
                 foreach (KeyValuePair <string, DialogToPhoneClient> kv in ProgramPhone.phoneClients)
                 {
                     if (kv.Value.listenIndex == name)
                     {
                         kv.Value.sendStr(n);
                     }
                 }
             }
             else if (msg.type == (sbyte)MSGTYPE._IMAGE_)
             {
                 string       time = DateTime.Now.ToString("yyyyMMddhhmmss");
                 MemoryStream ms   = new MemoryStream(msg.imgValue);
                 Bitmap       bm   = (Bitmap)Image.FromStream(ms);
                 //Don't save, asshole
                 //bm.Save(name+"_"+time + ".jpg");
                 DateTime now = DateTime.Now;
                 Console.WriteLine("[{0}/{1}/{2} {3}:{4}:{5}] " + "Client" + name + "----接收到图像----图像已经发给各移动端", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
                 //图片发给各客户端
                 foreach (KeyValuePair <string, DialogToPhoneClient> kv in ProgramPhone.phoneClients)
                 {
                     if (kv.Value.listenIndex == name)
                     {
                         kv.Value.sendBitMap(bm);
                         kv.Value.sendStr("接收到图像:" + time + "长度为:" + ms.ToArray().Length);
                     }
                 }
                 ms.Close();
             }
             else if (msg.type == (sbyte)MSGTYPE._FILE_)
             {
                 //TODO:接收客户端传回来的文件类型
                 WriteByteToFile(msg.imgValue, Encoding.UTF8.GetString(msg.strValue).Split('?')[2]);
                 DateTime now = DateTime.Now;
                 Console.WriteLine("[{0}/{1}/{2} {3}:{4}:{5}] " + "从Client {6}接收到文件,并保存到了{7}", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second, name, Encoding.UTF8.GetString(msg.strValue).Split('?')[2]);
             }
         }
     }catch (Exception ex)
     {
         string errorMessage = ex.Message;
         if (errorMessage.Equals("由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。"))
         {
             DateTime now = DateTime.Now;
             Console.WriteLine("[{0}/{1}/{2} {3}:{4}:{5}] " + errorMessage, now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
         }
         else if (errorMessage.Equals("远程主机强迫关闭了一个现有的连接。"))
         {
             DateTime now = DateTime.Now;
             Console.WriteLine("[{0}/{1}/{2} {3}:{4}:{5}] " + ex.Message, now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
             Console.WriteLine("[{0}/{1}/{2} {3}:{4}:{5}] " + "----Client" + name.ToString() + "断开连接----", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
             Program.DisconnectClient(this.name);
         }
         else
         {
             DateTime now = DateTime.Now;
             Console.WriteLine("[{0}/{1}/{2} {3}:{4}:{5}] " + ex.Message, now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
         }
     }
 }