• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#Socket发送文件多线程

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

控制台界面,分服务器端与客户端,经测试,多个客户端能成功同时发送大文件给服务器端

服务器端:

    class Program
{
const int BufferSize = 1024;
static string path = @"E:\";
static void Main(string[] args)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Bind(ip);
sock.Listen(1);
Console.WriteLine("Begin listen...");

while (true)
{
Socket client = sock.Accept();
if (client.Connected)
{
Thread cThread = new Thread(new ParameterizedThreadStart(myClient));
cThread.IsBackground = true;
cThread.Start(client);
}
}
}

static void myClient(object oSocket)
{
Socket clientSocket = (Socket)oSocket;
string clientName = clientSocket.RemoteEndPoint.ToString();
Console.WriteLine("新来一个客户:" + clientName);
try
{
while (true)
{
byte[] buffer = new byte[BufferSize];
int count = clientSocket.Receive(buffer);
Console.WriteLine("收到" + clientName + ":" + Encoding.Default.GetString(buffer, 0, count));
string[] command = Encoding.Default.GetString(buffer, 0, count).Split(',');
string fileName;
long length;
if (command[0] == "namelength")
{
fileName = command[1];
length = Convert.ToInt64(command[2]);
clientSocket.Send(Encoding.Default.GetBytes("OK"));
long receive = 0L;
Console.WriteLine("Receiveing file:" + fileName + ".Plz wait...");
using (FileStream writer = new FileStream(Path.Combine(path, fileName), FileMode.Create, FileAccess.Write, FileShare.None))
{
int received;
while (receive < length)
{
received = clientSocket.Receive(buffer);
writer.Write(buffer, 0, received);
writer.Flush();
receive += (long)received;
}
}
Console.WriteLine("Receive finish.\n");
}
}
}
catch
{
Console.WriteLine("客户:" + clientName + "退出");
}

}
}


客户端:

    class Program
{
static Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static void Main(string[] args)
{
sock.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080));
Console.WriteLine("Connect successfully");
while (true)
{
Console.WriteLine("please input the path of the file which you want to send:");
string path = Console.ReadLine();
try
{
using (FileStream reader = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None))
{
long send = 0L, length = reader.Length;
string sendStr = "namelength," + Path.GetFileName(path) + "," + length.ToString();

string fileName = Path.GetFileName(path);
sock.Send(Encoding.Default.GetBytes(sendStr));

int BufferSize = 1024;
byte[] buffer = new byte[32];
sock.Receive(buffer);
string mes = Encoding.Default.GetString(buffer);

if (mes.Contains("OK"))
{
Console.WriteLine("Sending file:" + fileName + ".Plz wait...");
byte[] fileBuffer = new byte[BufferSize];
int read, sent;
while ((read = reader.Read(fileBuffer, 0, BufferSize)) != 0)
{
sent = 0;
while ((sent += sock.Send(fileBuffer, sent, read, SocketFlags.None)) < read)
{
send += (long)sent;
}
}
Console.WriteLine("Send finish.\n");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Http请求报头设置(C#)发布时间:2022-07-13
下一篇:
C#复制文件夹内所有内容发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap