技术开发 频道

遇到了在c#中socket如何实现大文件传输

 发送端

 private void btSend_Click(object sender, System.EventArgs e)

 {

 if (this._isConnect)

 {

 _ns = _tc.GetStream();

 string path = this.txtPath.Text.Trim();

 FileStream fs = new FileStream(path, FileMode.Open);

 int sendCount = 0;

 byte[] countbuffer = null;

 byte[] clientbuffer = new byte[1004];

 while (sendCount < fs.Length && _ns.CanWrite)

 {

 int count = fs.Read(_sendBuf, 0, _sendBuf.Length);  //读出要发送的数据

 countbuffer = BitConverter.GetBytes(count);

 countbuffer.CopyTo(clientbuffer,0);

 _sendBuf.CopyTo(clientbuffer, 4);

 this._ns.Write(clientbuffer, 0, 4 + count);          //写入网络流

 sendCount += count;

 }

 countbuffer = BitConverter.GetBytes(0);                //发送完文件后 发送count = 0

 this._ns.Write(countbuffer, 0, countbuffer.Length);    //使接收端停止

 _ns.Close();

 fs.Close();

 }

 }
 

0
相关文章