private void btnGetDBF_Click(object sender, EventArgs e) { this.rtbMsg.AppendText("正在获取DBF文件…" + "/r"); try { netStream = tcpClient.GetStream(); if (netStream.CanWrite) { string control = "cmdDBF"; byte[] by = System.Text.Encoding.ASCII.GetBytes(control.ToCharArray()); netStream.Write(by, 0, by.Length); netStream.Flush(); //thread = new Thread(new ThreadStart(receive)); //thread.Start(); string path = "E://ColorCatcher.exe"; //要传入文件的路径 FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); //注意这个的属性和send端有所不同 BinaryWriter binarywrite = new BinaryWriter(fs); int count; byte[] b = new byte[60000000]; if ((count = tcpClient.Client.Receive(b, Convert.ToInt32(b.Length), SocketFlags.None)) != 0) //这个是接受文件流 { binarywrite.Write(b, 0, count); //将接收的流用写成文件 this.rtbMsg.AppendText("DBF文件下载完毕"+"/r"); } binarywrite.Close(); //thread.Abort(); } } catch (Exception ee) { this.rtbMsg.AppendText(ee.Message + "/r"); MessageBox.Show(ee.Message); } } 服务器: //控制码为cmdDBF,控制传输dbf文件 if (ss == "cmdDBF") { try { //string str = "dbf"; //byte[] newbyte = System.Text.Encoding.ASCII.GetBytes(str.ToCharArray()); //socket.Send(newbyte, newbyte.Length, 0); try { string path = "//Program Files//ColorCatcher.exe"; //要传输的文件 FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read); //注意与receive的filestream的区别 BinaryReader binaryreader = new BinaryReader(file); byte[] b = new byte[file.Length]; int data; while ((data = binaryreader.Read(b, 0,Convert.ToInt32(b.Length))) != 0) //这个注意是将文件写成流的形式 { socket.Send(b, data, SocketFlags.None); //发送文件流到目标机器 } binaryreader.Close(); file.Close(); } catch { //str = "000"; MessageBox.Show("Reading DBF file failure"); } } catch { MessageBox.Show("socket exception"); } }