怎样使用TCP在服务器和客户端传输大量得图片文件?
有谁做过类似得工作,在服务器和客户端建立TCP通道,然后由服务器给客户端传输图片文件,每次传输得大小大约是20M左右,这里是我写得代码,但是传到客户端之后文件里边就为空了,有谁知道为什么嘛?
private void Form1_Load(object sender, System.EventArgs e)
{
Thread tcp=new Thread(new ThreadStart(ThreadedTcpSrvr1));
tcp.IsBackground=true;
tcp.Start();
}
public Socket server;
public string stringdata;
public void ThreadedTcpSrvr1()
{
client=new TcpListener(IPAddress.Parse("192.168.10.54"),5801);
client.Start();
while(true)
{
while(!client.Pending())
{
Thread.Sleep(2000);
}
threadlistener=client;
Thread newthread=new Thread(new ThreadStart(HandleConnection));
newthread.IsBackground=true;
newthread.Start();
}
}
public void HandleConnection()
{
TcpClient tcpclient=threadlistener.AcceptTcpClient();
NetworkStream ns=tcpclient.GetStream();
FileStream fs=new FileStream("d:\\ff.yaan",FileMode.Open,FileAccess.Read);
byte[] send=new byte[2229888];
BinaryReader br=new BinaryReader(fs);
fs.Read(send,0,send.Length-1);
br.Read(send,0,send.Length-1);
ns.Write(send,0,send.Length-1);
ns.Close();
tcpclient.Close();
}
以上是服务器端得发送代码
private bool connect()
{
IPEndPoint ipep=new IPEndPoint(IPAddress.Parse("192.168.10.54"),5801);
client1=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
client1.Connect(ipep);
return true;
}
catch
{
return false;
}
}
if(connect()==true)
{
FileStream fs=new FileStream("d:\\f1.yaan",FileMode.Create);
byte[] receive=new byte[229888];
BinaryWriter bw=new BinaryWriter(fs);
client1.Receive(receive);
fs.Write(receive,0,receive.Length);
bw.Write(receive,0,receive.Length);
fs.Close();
bw.Close();
client1.Close();
以上是客户端得代码
分不够可以再加!!