作者chris70211 (克里斯)
看板C_Sharp
标题[问题] 档案无法传送完全
时间Wed May 25 12:03:53 2011
主要目的是在client端透过webcam截取影像之後灰阶化及取边缘的三个图存下来之後
在藉由tcp传送档案到server端 可是不知道为什麽只能传送前两张图,
第三张图存下来却无法显示 麻烦各位前辈指教一下了 刚接触c#跟.net不久
以下是传档的程式
=======================================================================
client端
TcpClient tc = new TcpClient();
tc.Connect("192.168.0.129", 5000);
fs1 = new FileStream(@"原图.jpg", FileMode.Open);
fs2 = new FileStream(@"灰阶.jpg", FileMode.Open);
fs3 = new FileStream(@"canny.jpg", FileMode.Open);
StreamReader sw = new StreamReader(fs1);
StreamReader sw1 = new StreamReader(fs2);
StreamReader sw2 = new StreamReader(fs3);
NetworkStream ns = tc.GetStream();
byte[] getb = new byte[1024000];
fs1.Read(getb, 0, getb.Length);
ns.Write(getb, 0, getb.Length);
fs2.Read(getb, 0, getb.Length);
ns.Write(getb, 0, getb.Length);
fs3.Read(getb, 0, getb.Length);
ns.Write(getb, 0, getb.Length);
Console.Read();
}
catch (IOException Ex)
{
Console.Write("出代志了" + Ex);
Console.Read();
}
server端
Console.Write("Server 正在起动中!!");
TcpListener serv = new TcpListener(5000);
serv.Start();
TcpClient getClient = serv.AcceptTcpClient();
NetworkStream ns = getClient.GetStream();
int chk;
FileStream fs1 = new FileStream(@"原图.jpg", FileMode.Create);
FileStream fs2 = new FileStream(@"灰阶.jpg", FileMode.Create);
FileStream fs3 = new FileStream(@"canny.jpg", FileMode.Create);
byte[] getb = new byte[1024000];
do
{
chk = ns.Read(getb, 0, getb.Length);
fs1.Write(getb, 0, getb.Length);
Console.WriteLine();
} while (chk == 0);
fs1.Close();
NetworkStream ns1 = getClient.GetStream();
do
{
chk = ns1.Read(getb, 0, getb.Length);
fs2.Write(getb, 0, getb.Length);
Console.WriteLine();
} while (chk == 0);
fs2.Close();
NetworkStream ns2 = getClient.GetStream();
do
{
chk = ns2.Read(getb, 0, getb.Length);
fs3.Write(getb, 0, getb.Length);
Console.WriteLine();
} while (chk == 0);
fs3.Close();
Console.Write("已经收完了");
Console.Read();
}
catch (IOException Ex){
Console.Write("出代志了" + Ex);
Console.Read();
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.124.44.104
1F:→ james732:所有的 stream 使用结束都记得加上 close(); 05/25 12:05
2F:→ james732:你的 ns1, ns2 都没有做,可以先补上 close() 试试 05/25 12:05