使用 NetworkStream 传输文件

发布于 2024-09-18 03:20:36 字数 1307 浏览 0 评论 0原文

我在将文件从服务器程序传输到客户端时遇到问题。我想解决几个问题。首先,我将字节数组设置为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃气十足 2024-09-25 03:20:36

一个问题是,即使您只从文件中读取一个字节,您也会将整个 6000 字节写入流中。

使用 FileStream 访问文件并复制内容到 NetworkStream。 Framework 4.0对此有一个很好的功能,

FileStream fs = new FileStream(...);
fs.CopyTo(stream);

您可以在客户端采取类似的方法,只是相反,从NetworkStream复制到目标Stream。

在 Framework 4.0 之前,您可以实现自己的 CopyTo 函数。像这样的东西

public static long CopyStream(Stream source, Stream target)
{
  const int bufSize = 0x1000;
  byte[] buf = new byte[bufSize];

  long totalBytes = 0;
  int bytesRead = 0;

  while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
  {
    target.Write(buf, 0, bytesRead);
    totalBytes += bytesRead;
  }
  return totalBytes;
}

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

FileStream fs = new FileStream(...);
fs.CopyTo(stream);

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

public static long CopyStream(Stream source, Stream target)
{
  const int bufSize = 0x1000;
  byte[] buf = new byte[bufSize];

  long totalBytes = 0;
  int bytesRead = 0;

  while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
  {
    target.Write(buf, 0, bytesRead);
    totalBytes += bytesRead;
  }
  return totalBytes;
}
菩提树下叶撕阳。 2024-09-25 03:20:36

如果 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文