【求助】C# Socket 编程 求救 为什么此程序 文件传输时会有大小限制?
2009-12-25 13:54
309 查看
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.IO; namespace File { public partial class FileTrans : Form { public FileTrans() { InitializeComponent(); } private void SelectBtn_Click(object sender, EventArgs e) { Ofd.ShowDialog(); SelectBox.Text = Ofd.FileName; } private void SendBtn_Click(object sender, EventArgs e) { //tcp.Start(); String path = SelectBox.Text; TcpClient client = new TcpClient(); try { client.Connect(IPAddress.Parse(IPBox.Text), 5000); MsgBox.AppendText("连接成功!"); } catch (Exception E) { MsgBox.AppendText(E.Message); } FileStream file = new FileStream(path,FileMode.Open,FileAccess.Read); NetworkStream stream = client.GetStream(); byte[] by = new byte [2048]; int data; while ((data = file.Read(by, 0, 2048)) != 0) { stream.Write(by,0,by.Length);//发送文件到目标机器 stream.Flush(); by = new byte[2048]; } client.Client.Shutdown(SocketShutdown.Both); stream.Close(); file.Close(); MsgBox.AppendText("/n发送文件" + Ofd.FileName); } private void Receive_Click(object sender, EventArgs e) { Sfd.ShowDialog(); String path = Sfd.FileName; TcpClient client = tcp.AcceptTcpClient(); FileStream fs = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write); NetworkStream stream = client.GetStream(); int count; byte[] by = new byte[2048]; while ((count = stream.Read(by,0,by.Length)) != 0) { fs.Write(by, 0, count); stream.Flush(); by = new byte[2048]; } stream.Close(); fs.Close(); //sock.Close(); MsgBox.AppendText("/n接受成功"); } TcpListener tcp = null; private void FileTrans_Load(object sender, EventArgs e) { tcp = new TcpListener(5000); tcp.Start(); } } }
为什么此段代码 会有文件大小限制 ?
while ((data = file.Read(by, 0, 2048)) != 0) { stream.Write(by,0,by.Length);//发送文件到目标机器 stream.Flush(); by = new byte[2048]; }
不是应该循环读文件流么? 求大侠指教。。。
相关文章推荐
- c# WINFORM SOCKET编程-简单聊天程序(客户端)
- c#如何限制程序内存使用的大小
- c# WINFORM SOCKET编程-简单聊天程序(服务端)
- C#如何限制程序内存使用的大小
- c#基于socket编程实现简单多人聊天程序
- C#网络编程(四、Socket中英文字符大小和文本边界问题)
- c# WINFORM SOCKET编程-简单聊天程序(服务端)(转载)
- C#:程序关闭了,为什么在后台还有进程存在?(转载)
- 最基本的Socket编程(C#版)
- Socket编程指南及示例程序
- C#.Net上传文件大小限制设置
- C# socket编程中即可以处理Win32Exception也可以处理Exception
- C# 串口操作系列(2) -- 入门篇,为什么我的串口程序在关闭串口时候会死锁 ?
- 请教如何改善C#中socket通信机客户端程序的健壮性_C#教程
- C# 串口操作系列(2) -- 入门篇,为什么我的串口程序在关闭串口时候会死锁 ?
- linux网络编程之socket(九):使用select函数改进客户端/服务器端程序
- [python&php 网络编程]socket缓冲区大小设置
- C#的Socket程序(TCP)
- C/C++ socket编程教程之二:一个简单的Linux下的socket程序
- 对于C#中的FileUpload解决文件上传大小限制的问题设置