📄 filesend.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace mychat1
{
public class filesend
{
private string herip;
private int herport;
private string thefilepath;
private string hername;
public filesend(string herip,int herport,string hername,string thefilepath)
{
this.herip = herip;
this.herport = herport + 100;//防止绑定一个端口
this.thefilepath = thefilepath;
this.hername = hername;
}
private string receiverfromacomputer(string mycomputerip, int mycomputerport)
{
string stringdata = null;
int recv = 0;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(mycomputerip), mycomputerport);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;
recv = newsock.ReceiveFrom(data, ref Remote);
newsock.Close();
stringdata = Encoding.Unicode.GetString(data, 0, recv);
return stringdata;
}
public void sendfile()
{
string tipmessage = hername + ":hello " + "我要给你传输文件!";
commonmethod.sendtosevermessage(tipmessage);
string returnmessage = receiverfromacomputer(commonmethod .myip ,9060);
IPEndPoint Conncet = new IPEndPoint(IPAddress .Parse (herip), herport); //构造结点
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //初始化socket
try
{
s.Connect(Conncet); //连接远程服务器
if (s.Connected) //如果连接成功 s.Connected 则为true 否则为 false
{
byte[] b = new byte[10000000]; //创建文件缓冲区,这里可以认为文件的最大值
FileStream file = File.Open(thefilepath, FileMode.Open, FileAccess.Read); //创建文件流
int start = 0;
int end = (int)file.Length; //获取文件长度 文件传送如果有需要超过int的范围估计就要改写FileStream类了
commonmethod.sendtoacomputer(herip,9060, end.ToString());
try
{
while (end != 0)
{
int count = file.Read(b, start, end); //把数据写进流
start += count;
end -= count;
}
while (start != 0)
{
int n = s.Send(b, end, start, SocketFlags.None); //用Socket的Send方法发送流
end += n;
start -= n;
}
file.Close(); //关闭文件流
s.Close(); //关闭Socket
}
catch
{
throw new Exception ("文件发送失败!");
}
}
}
catch
{
throw new Exception ("对方没有接受你的请求!");
}
}
public void get() //文件接收
{
FileStream file = new FileStream(thefilepath , FileMode.OpenOrCreate, FileAccess.Write); //写入文件流
TcpListener listen = new TcpListener(IPAddress .Parse (commonmethod .myip),(commonmethod .myport + 100)); //监听端口
Socket s1;
commonmethod.sendtoacomputer(herip, 9060, "我准备好了!");
try
{
listen.Start(); //开始监听
s1 = listen.AcceptSocket();
string filelength = receiverfromacomputer(commonmethod.myip, 9060);
int all = Convert.ToInt32(filelength);
byte[] data = new byte[10000000]; //定义缓冲区
int longer = data.Length;
int start = 0;
int mid = 0;
if (s1.Connected) //确定连接
{
int count = s1.Receive(data, start, longer, SocketFlags.None); //把接收到的byte存入缓冲区
mid += count;
longer -= mid;
while (count != 0)
{
count = s1.Receive(data, mid, longer, SocketFlags.None);
mid += count;
longer -= mid;
}
file.Write(data, 0, all); //写入文件,1214134为文件大小,可以用socket发送获得,代码前面已经说明。
s1.Close();
file.Close();
}
}
catch
{
throw new Exception("接受传送文件失败!");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -