好友
阅读权限25
听众
最后登录1970-1-1
|
这个是找了好久唯一一个不报错的C#传文件源码,但是注释较少,
还是遇到些问题,再此求助大佬
源码:[C#] 纯文本查看 复制代码 //文件发送[/b][/size][size=6][b] public void Send(IPEndPoint ip, string path)
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ip);
using (FileStream reader = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None))
{
long send = 0L, length = reader.Length;
sock.Send(BitConverter.GetBytes(length));
string fileName = Path.GetFileName(path);
sock.Send(Encoding.Default.GetBytes(fileName));
Console.WriteLine("Sending file:" + fileName + ".Plz wait...");
byte[] buffer = new byte[BufferSize];
int read, sent;
//断点发送 在这里判断设置reader.Position即可
while ((read = reader.Read(buffer, 0, BufferSize)) != 0)
{
sent = 0;
while ((sent += sock.Send(buffer, sent, read, SocketFlags.None)) < read)
{
send += (long)sent;
//Console.WriteLine("Sent " + send + "/" + length + ".");//进度
}
}
Console.WriteLine("Send finish.");
}
}
调用此接受方法时报以下错误:
请问大佬应如何修改 |
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|