- 1
- Thread _timers =
- null;
- 2 private
- Socket _SocketClient =
- null;
- 3 byte
- [] RecvBytes =
- new byte
- [
- 50];
- 4 private int
- Numimgs =
- 0
- ;
- //记录接收成功的张数
- 5 //byte[] ReImgBytes = new byte[8];//接收图片的头字节
- 6 public event ReceivedBitmapHandler ReceivedBitmap;
- 7 private delegate void
- RestartThread();
- //定义委托
- 8
- 9
- 10 public TransferHandler(Socket client)
- 11 {
- 12
- _SocketClient =
- client;
- 13 }
- 14
- 15 #region
- 服务器端
- 16 public void BeginReceive()
- 17 {
- 18 //采用线程直接接收的方式
- 19
- _timers =
- new Thread(StartToReceive);
- 20
- _timers.IsBackground =
- true;
- 21 _timers.Start();
- 22 }
- 23 /// <summary>
- 24 /// 开始接收消息
- 25 /// </summary>
- 26 private void StartToReceive()
- 27 {
- 28 //由于long占8位字节,所以先获取前8位字节数据
- 29
- IAsyncResult iar =
- _SocketClient.BeginReceive(
- 30 RecvBytes,
- 31 0,
- 32 RecvBytes.Length,
- 33 SocketFlags.None,
- 34 null,
- 35 null);
- 36 int
- len =
- _SocketClient.EndReceive(iar);
- 37 string
- ReceivemMsg = Encoding.ASCII.GetString(RecvBytes,
- 0, len);
- 38 if
- (ReceivemMsg.IndexOf(
- "xxx"
- ) >
- 0
- )
- //区分业务消息和心跳检测消息
- 39 {
- 40
- _SocketClient.Send(Encoding.ASCII.GetBytes(
- "xxx"
- ));
- //回应心跳包
- 41
- RecvBytes =
- new byte
- [RecvBytes.Length];
- //清空数据
- 42
- StartToReceive();
- //回应心跳包完成之后继续等待接收
- 43 }
- 44 else if
- (ReceivemMsg ==
- "xxx"
- )
- //如果收到这个请求,告诉客户端可以开始发送第一张图片了
- 45 {
- 46 string
- order =
- "xxx";
- 47 byte
- [] orderdata = Encoding.ASCII.GetBytes(order);
- //把字符串编码为字节
- 48 _SocketClient.Send(orderdata);
- 49
- RecvBytes =
- new byte
- [
- 8
- ];
- //开始接收图片
- 50
- StartToReceive();
- //回应指令后完成之后继续等待接收
- 51 }
- 52 else if
- (ReceivemMsg ==
- "xxx")
- 53 {
- 54 //停止接收图片
- 55 string
- order =
- "xxxx"
- ;
- //通知客户端已经完成接收这次所有图片,结束传输;
- 56 byte
- [] orderdata = Encoding.ASCII.GetBytes(order);
- //把字符串编码为字节
- 57 _SocketClient.Send(orderdata);
- 58 StartToReceive();
- 59 }
- 60 else
- 61 {
- 62 int
- offset =
- 0;
- 63 int
- length = BitConverter.ToInt32(RecvBytes, offset);
- //先获取文件长度
- 64 ReceiveFile(length);
- 65 }
- 66
- 67 }
- 68
- 69
- 70 public void
- ReceiveFile(
- long filelen)
- 71 {
- 72
- MemoryStream ms =
- new MemoryStream();
- 73 int
- bytesRead =
- 0;
- 74 long
- count =
- 0;
- 75 byte
- [] buffer =
- new byte
- [
- 8192];
- 76
- 77 while
- (count !=
- filelen)
- 78 {
- 79 try
- 80 {
- 81
- bytesRead =
- _SocketClient.Receive(buffer);
- 82
- ms.Write(buffer,
- 0, bytesRead);
- 83
- count +=
- bytesRead;
- 84 }
- 85 catch (Exception ex)
- 86 {
- 87
- 88 }
- 89 }
- 90
- ReceivedBitmap(
- new Bitmap(ms));
- 91
- 92
- 93 //接收完成之后清空数据,继续接收
- 94
- buffer =
- new byte
- [buffer.Length];
- //缓存
- 95
- RecvBytes =
- new byte
- [RecvBytes.Length];
- //用于接收
- 96
- 97 string
- order =
- "xxxx"
- ;
- //通知客户端已经收到图片,请继续
- 98 byte
- [] orderdata = Encoding.ASCII.GetBytes(order);
- //把字符串编码为字节
- 99 _SocketClient.Send(orderdata);
- 100
- Numimgs++
- ;
- 101
- 102 if
- (Numimgs >=
- 3)
- 103 {
- 104
- RecvBytes =
- new byte
- [
- 50];
- 105
- Numimgs =
- 0;
- 106 }
- 107
- StartToReceive();
- //接收完成之后继续接收
- 108 }
- 109 #endregion
来源: http://www.cnblogs.com/BarryJames/p/7161791.html