发送端代码: String TargetIP = file.Target; //接收方IP
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(TargetIP), 19812);
Socket sndSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
FileStream fs = new FileStream(file.FilePath + "\\" + file.filename, FileMode.Open);
byte[] SendBytes = new byte[MAX_TRANSFER_BYTES]; //MAX_TRANSFER_BYTES=8192
Int64 offset = 0;
while (offset < fs.Length)
{
int readcount = 0;
if (fs.Length - offset >= SendBytes.Length)
{
readcount = fs.Read(SendBytes, 0, SendBytes.Length);
}
else
{
readcount = fs.Read(SendBytes, 0, (int)(fs.Length - offset));
}
int send = sndSock.SendTo(SendBytes, ip);
Thread.Sleep(2);
offset += readcount;
}
fs.Close();
fs.Dispose();
sndSock.Close();
接收端主要代码 while (totalCount < file.filesize)
{
Int32 readCount = RevSocket.ReceiveFrom(Receivebyte, ref SndEndPoint);
fs.Seek(0, SeekOrigin.End);
fs.Write(Receivebyte, 0, readCount);
totalCount += readCount;
}
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(TargetIP), 19812);
Socket sndSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
FileStream fs = new FileStream(file.FilePath + "\\" + file.filename, FileMode.Open);
byte[] SendBytes = new byte[MAX_TRANSFER_BYTES]; //MAX_TRANSFER_BYTES=8192
Int64 offset = 0;
while (offset < fs.Length)
{
int readcount = 0;
if (fs.Length - offset >= SendBytes.Length)
{
readcount = fs.Read(SendBytes, 0, SendBytes.Length);
}
else
{
readcount = fs.Read(SendBytes, 0, (int)(fs.Length - offset));
}
int send = sndSock.SendTo(SendBytes, ip);
Thread.Sleep(2);
offset += readcount;
}
fs.Close();
fs.Dispose();
sndSock.Close();
接收端主要代码 while (totalCount < file.filesize)
{
Int32 readCount = RevSocket.ReceiveFrom(Receivebyte, ref SndEndPoint);
fs.Seek(0, SeekOrigin.End);
fs.Write(Receivebyte, 0, readCount);
totalCount += readCount;
}
解决方案 »
- Visio二次开发,怎样获得形状内的拖动对象
- TreeView如何控制节点是否显示
- 如何在richTextBox 插入图片
- menuStrip控件怎样绑定数据 急
- 怎么在c#里获得一个应用程序的窗口句柄呢?
- Microsoft .NET Framework Version 2.0 Redistributable Package (x86) - 简体中文官方发布
- 在Windows中如何清空DataGrid中的数据???
- dataset.merge()操作的相关数据集结构可不同吗?merge()是不是相当于集合的并操作呢?
- 需要做个上传下载功能的小程序,敬请达人们答疑解惑
- C++翻译成C#
- C#window编程示例.
- 关于接口的小问题
加入固定的包头,比如包头先发送一个总共多少个包,当前第几个包这样两个数据服务器如果发现缺少或者不连续,可以主动发指令要求客户端重发指定编号的数据包或者每个收到的都返回RESPOSE消息
你可以用TCP来进行有连接的传输或是在发送后加上fs.Flush()方法将缓冲区的数据强制发送出去