发送端
FileStream fs = new FileStream(SenFilePath, FileMode.Open); for ( int i=0 ; i < Axlst_ServerPath.Items.Count ; i++ )
{ Socket sen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string[] IPPort = Axlst_ServerPath.Items[i].ToString().Split(Convert.ToChar(":")); IPAddress myIP = IPAddress.Parse(IPPort[0]); IPEndPoint ipe = new IPEndPoint(myIP, Convert.ToInt32(IPPort[1])); sen.Connect(ipe);
//byte[] message = UTF8Encoding.UTF8.GetBytes("Statr:" + Axlab_SelectFileName.Text.ToString() + ":" + fs.Length + ":\r\n");
//sen.Send(message); ishead = true; while (sendCount < fs.Length)
{ int count = fs.Read(by, 0, 1024); sen.Send(by); sendCount = sendCount + count; System.Threading.Thread.Sleep(20);
} sen.Close();
fs.Close();接收 while (true)
{
Socket newSock = s.Accept(); IPEndPoint clientep = (IPEndPoint)newSock.RemoteEndPoint ; FileStream fs = new FileStream(Axtxt_FilePath.Text + "1.jpg", FileMode.Create, FileAccess.Write); newSock.Receive(bytebuffer); fs.Write(bytebuffer,0, bytebuffer.Length);
}
为什么没全部发送过来,只发了第一次的1024字节?要怎么修改?做个小软件到这步难倒了。
FileStream fs = new FileStream(SenFilePath, FileMode.Open); for ( int i=0 ; i < Axlst_ServerPath.Items.Count ; i++ )
{ Socket sen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); string[] IPPort = Axlst_ServerPath.Items[i].ToString().Split(Convert.ToChar(":")); IPAddress myIP = IPAddress.Parse(IPPort[0]); IPEndPoint ipe = new IPEndPoint(myIP, Convert.ToInt32(IPPort[1])); sen.Connect(ipe);
//byte[] message = UTF8Encoding.UTF8.GetBytes("Statr:" + Axlab_SelectFileName.Text.ToString() + ":" + fs.Length + ":\r\n");
//sen.Send(message); ishead = true; while (sendCount < fs.Length)
{ int count = fs.Read(by, 0, 1024); sen.Send(by); sendCount = sendCount + count; System.Threading.Thread.Sleep(20);
} sen.Close();
fs.Close();接收 while (true)
{
Socket newSock = s.Accept(); IPEndPoint clientep = (IPEndPoint)newSock.RemoteEndPoint ; FileStream fs = new FileStream(Axtxt_FilePath.Text + "1.jpg", FileMode.Create, FileAccess.Write); newSock.Receive(bytebuffer); fs.Write(bytebuffer,0, bytebuffer.Length);
}
为什么没全部发送过来,只发了第一次的1024字节?要怎么修改?做个小软件到这步难倒了。
解决方案 »
- 【菜鸟请问】关于变量使用结束后调用Dispose方法的疑问
- 关于线程的问题
- 做小程序,该如何开始?
- 关闭窗体,确认问题
- DateTimePicker控件在选择日期后,为什么时间不改变?请教如何让时间也为当前时间?
- 请问在DataGrid里怎样在多个TableStyles之间切换是当前显示状元??
- c/s模式下怎么来限制登录次数或只能使用一段时间???
- 监听某个文件是否打开,如果打开则关闭!听说用HOOK可以实现!!谢谢
- 各位大哥,帮帮小弟,给点设计思路。万分感谢!!!!
- 输出Excel文件时报错:QueryInterface for Interface Excel._Application failed
- 关于数据绑定出现System.Data.DataViewManagerListItemTypeDescriptor这个信息是为什呢?
- 用正则表达式提取html中的一个指定div对!
这句我是在网上,COPY的不没注意弄掉。
while (sendCount < fs.Length)
{ int count = fs.Read(by, 0, 1024);
sen.Send(by); sendCount = sendCount + count; System.Threading.Thread.Sleep(20);
}
{ int count = fs.Read(by, 0, 1024);
sen.Send(by); sendCount = sendCount + count; System.Threading.Thread.Sleep(20);
}
我想知道为什么他只发了一次信息出去就不发了
才是我接收有问题。
还有挂起的连接我怎么知道发送这边是不是断了?它能自己判断吗?
我试着传了个图片只收到 了一个角,就1KB的文件.
如果可以加Q方便些
谢谢了。
{ int count = fs.Read(by, 0, by.Length); sen.Send(by); sendCount = sendCount + count; System.Threading.Thread.Sleep(20);
}
while (true)
{ if(newSock.Receive(bytebuffer)==0)
{
fs.close();
break;
}
fs.Write(bytebuffer,0, bytebuffer.Length);
}