使用 NetworkStream 传输文件
我在将文件从服务器程序传输到客户端时遇到问题。我想解决几个问题。首先,我将字节数组设置为 6000 字节大,并且始终是这个大小。有没有办法保持正确的文件大小?而且按照现在的代码方式,程序会挂起。当我将它从客户端的 while 循环中取出时它就起作用了。帮助!!
客户端:
private void button1_Click(object sender, EventArgs e)
{
BinaryWriter binWriter;
int i = -1;
Byte[] bytes = new Byte[6000];
NetworkStream clientStream = connTemp.GetStream();
byte[] outstream = Encoding.ASCII.GetBytes(txtMessage.Text);
clientStream.Write(outstream, 0, outstream.Length);
while (i != 0)
{
try
{
if (clientStream.CanRead)
{
i = clientStream.Read(bytes, 0, bytes.Length);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
break;
}
}
binWriter = new BinaryWriter(File.Open("C:\\SeanLaunch\\log.rxlog",FileMode.Create));
binWriter.Write(bytes);
binWriter.Close();
}
}
服务器:
Byte[] fileToSendAsByteArray = new Byte[6000];
fileToSendAsByteArray = File.ReadAllBytes("C:\\Launch\\Test.rxlog");
stream.Write(fileToSendAsByteArray, 0, fileToSendAsByteArray.Length);
编辑!!!:我修复了循环问题。
I am having trouble transferring a file to a client from a server program. A few problems I would like to address. First is that I make the byte array 6000 bytes big and its always that size. Is there a way to maintain the correct file size? Also with the way the code is now, the program hangs. It works when I take it out of the while loop on the client side. Help!!
Client:
private void button1_Click(object sender, EventArgs e)
{
BinaryWriter binWriter;
int i = -1;
Byte[] bytes = new Byte[6000];
NetworkStream clientStream = connTemp.GetStream();
byte[] outstream = Encoding.ASCII.GetBytes(txtMessage.Text);
clientStream.Write(outstream, 0, outstream.Length);
while (i != 0)
{
try
{
if (clientStream.CanRead)
{
i = clientStream.Read(bytes, 0, bytes.Length);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
break;
}
}
binWriter = new BinaryWriter(File.Open("C:\\SeanLaunch\\log.rxlog",FileMode.Create));
binWriter.Write(bytes);
binWriter.Close();
}
}
Server:
Byte[] fileToSendAsByteArray = new Byte[6000];
fileToSendAsByteArray = File.ReadAllBytes("C:\\Launch\\Test.rxlog");
stream.Write(fileToSendAsByteArray, 0, fileToSendAsByteArray.Length);
EDIT!!!: I fixed the looping issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个问题是,即使您只从文件中读取一个字节,您也会将整个 6000 字节写入流中。
使用 FileStream 访问文件并复制内容到 NetworkStream。 Framework 4.0对此有一个很好的功能,
您可以在客户端采取类似的方法,只是相反,从NetworkStream复制到目标Stream。
在 Framework 4.0 之前,您可以实现自己的 CopyTo 函数。像这样的东西
One problem is that you are writing the entire 6000 bytes to the stream even if you only read one byte from the file.
Use a FileStream to access the file and copy the content to the NetworkStream. Framework 4.0 has a nice function for this
You can take a similar approach for the client side, just in reverse, copy from the NetworkStream to the target Stream.
Prior to Framework 4.0 you can implement you own CopyTo function. Something like this
如果 i 不为零时 CanRead 保持为 false,则程序将永远循环。或者,它可能会在 Read 调用中被阻止。
调试您的接收端以了解发生了什么情况。它真的挂了,还是只是循环?
添加代码以转储每一端发送和接收的数据,以查看在问题出现之前您读取到的程度正常。
The program will loop forever if CanRead remains false while i is non-zero. Or, it may be blocked on the Read call.
Debug your receive side to find out what's going on. Is it really hung, or just looping?
Add code to dump the data sent and received on each end to see how far you read OK before the problems arise.