C#实现一个局域网文件传输工具

C#实现一个局域网文件传输工具工作需要,经常会在工作的台式机和笔记本之间传文件或者需要拷贝文本,两个机器都位于局域网内,传文件或者文本的方式有很多种,之前是通过共享文件夹来进行文件的拷贝,或者通过SVN进行同步。文本传递比较简单,可以通过两台机器上装QQ登两个号码,或者在共享目录下建一个TXT,或者发电子邮件等等。不过上面这些方法总觉得不直接,所以想基于P2P做一个小的局域网文件和文字传输小工具。WinForm的工程,

大家好,又见面了,我是你们的朋友全栈君。

工作需要,经常会在工作的台式机和笔记本之间传文件或者需要拷贝文本,两个机器都位于局域网内,传文件或者文本的方式有很多种,之前是通过共享文件夹来进行文件的拷贝,或者通过SVN进行同步。文本传递比较简单,可以通过两台机器上装QQ登两个号码,或者在共享目录下建一个TXT,或者发电子邮件等等。

不过上面这些方法总觉得不直接,所以想基于P2P做一个小的局域网文件和文字传输小工具

WinForm的工程,界面方面的代码就不贴了,大家自己根据喜好设计就好了,主要把TCP数据传输的代码和逻辑贴出来:

1. 文件和文本传输的通用方法:

private string ReceiveControl(Socket socket)
{
    int bufSize = 1024;
    byte[] buf = new byte[bufSize];
    int len = socket.Receive(buf);
    return len > 0 ? Encoding.UTF8.GetString(buf, 0, len) : String.Empty;
}
private void SendControl(Socket socket, string controlMsg)
{
    byte[] msgBytes = Encoding.UTF8.GetBytes(controlMsg);
    socket.Send(msgBytes);
}
private string ReceiveContent(Socket socket, int contentLen)
{
    int receivedLen = 0;
    int bufSize = 1024;
    byte[] buf = new byte[bufSize];
    StringBuilder sb = new StringBuilder();
    while (receivedLen < contentLen)
    {
        int len = socket.Receive(buf);
        if (len > 0)
        {
            sb.Append(Encoding.UTF8.GetString(buf, 0, len));
            receivedLen += len;
        }
    }
    return sb.ToString();
}
private void SendContent(Socket socket, string content)
{
    byte[] contentBytes = Encoding.UTF8.GetBytes(content);
    SendControl(socket, contentBytes.Length.ToString());
    ReceiveControl(socket);
    socket.Send(contentBytes);
}
private void ReceiveFile(Socket socket, string fileName, int fileLen)
{
    string filePath = Path.Combine(GetCurrentUserDesktopPath(), RenameConflictFileName(fileName));
    using (Stream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
    {
        int bufLen = 1024;
        int receivedLen = 0;
        byte[] buf = new byte[bufLen];
        int len = 0;
        while (receivedLen < fileLen)
        {
            len = socket.Receive(buf);
            fs.Write(buf, 0, len);
            receivedLen += len;
        }
    }
}
private void SendFile(Socket socket, string filePath)
{
    using (Stream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
        SendControl(socket, GetFileNameFromPath(filePath));
        ReceiveControl(socket);
        SendControl(socket, fs.Length.ToString());
        ReceiveControl(socket);
        int bufLen = 1024;
        byte[] buf = new byte[bufLen];
        long readLen = 0;
        long fileLen = fs.Length;
        int len = 0;
        while (readLen < fileLen)
        {
            len = fs.Read(buf, 0, bufLen);
            readLen += len;
            int sentLen = 0;
            int realSent = 0;
            int left = 0;
            while (sentLen < len)
            {
                left = len - realSent;
                realSent = socket.Send(buf, sentLen, left, SocketFlags.None);
                sentLen += realSent;
            }
        }
    }
}

2.连接,发送文字/文件,重命名文件等方法:

private void SendText()
{
    if (connected)
    {
        if (!String.IsNullOrEmpty(this.TextToSend.Text))
        {
            string txt = this.TextToSend.Text;
            SendControl(clientSocket, "Text");
            ReceiveControl(clientSocket);
            SendContent(clientSocket, txt);
            ReceiveControl(clientSocket);
        }
    }
}

private void Connect()
{
    try
    {
        if (!connected)
        {
            passive = false;
            IPAddress serverIPAddress = IPAddress.Parse(this.ServerIPAddress.Text);
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.Connect(serverIPAddress, 60000);
            string msg = ReceiveControl(clientSocket);
            if (msg.Equals("Connected"))
            {
                this.ConnectBtn.Text = "Disconnect";
                connected = true;
            }
        }
        else
        {
            passive = true;
            SendControl(clientSocket, "Disconnect");
            clientSocket.Close();
            this.ConnectBtn.Text = "Connect";
            connected = false;
        }
    }
    catch (Exception err)
    {
        MessageBox.Show(string.Format("Failed to connect to server, error: {0}", err.ToString()));
    }
}

private void ServerThread()
{
    IPAddress local = IPAddress.Parse("0.0.0.0");
    Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    server.Bind(new IPEndPoint(local, 60000));
    server.Listen(1);
    while (true)
    {
        Socket receivedClientSocket = server.Accept();
        IPEndPoint clientEndPoint = (IPEndPoint)receivedClientSocket.RemoteEndPoint;
        SendControl(receivedClientSocket, "Connected");
        if (passive)
        {
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            clientSocket.Connect(clientEndPoint.Address, 60000);
            string msg = ReceiveControl(clientSocket);
            if (msg.Equals("Connected"))
            {
                connected = true;
                this.ConnectBtn.Text = "Disconnect";
                this.ServerIPAddress.Text = clientEndPoint.Address.ToString();
            }
        }
        while (connected)
        {
            string msg = ReceiveControl(receivedClientSocket);
            switch (msg)
            {
                case "Disconnect":
                    receivedClientSocket.Close();
                    clientSocket.Close();
                    this.ConnectBtn.Text = "Connect";
                    passive = true;
                    connected = false;
                    break;
                case "Text":
                    SendControl(receivedClientSocket, "Received");
                    int length = Convert.ToInt32(ReceiveControl(receivedClientSocket));
                    SendControl(receivedClientSocket, "Received");
                    string content = ReceiveContent(receivedClientSocket, length);
                    SendControl(receivedClientSocket, "Received");
                    this.TextToSend.Text = content;
                    break;
                case "File":
                    SendControl(receivedClientSocket, "Received");
                    string fileName = ReceiveControl(receivedClientSocket);
                    SendControl(receivedClientSocket, "Received");
                    int fileLen = Convert.ToInt32(ReceiveControl(receivedClientSocket));
                    SendControl(receivedClientSocket, "Received");
                    ReceiveFile(receivedClientSocket, fileName, fileLen);
                    SendControl(receivedClientSocket, "Received");
                    MessageBox.Show("File Received");
                    break;
            }
        }
    }
}

private string GetFileNameFromPath(string path)
{
    int index = path.LastIndexOf('\\');
    return path.Substring(index + 1);
}

private string RenameConflictFileName(string originalName)
{
    string desktopPath = GetCurrentUserDesktopPath();
    int extensionIndex = originalName.LastIndexOf(".");
    string fileName = originalName.Substring(0, extensionIndex);
    string extensionName = originalName.Substring(extensionIndex + 1);

    int renameIndex = 1;
    string newNameSuffix = String.Format("({0})", renameIndex);
    string finalName = originalName;
    string filePath = Path.Combine(desktopPath, finalName);
    if (File.Exists(filePath))
    {
        finalName = String.Format("{0} {1}.{2}", fileName, newNameSuffix, extensionName);
        filePath = Path.Combine(desktopPath, finalName);
    }
    while (File.Exists(filePath))
    {
        renameIndex += 1;
        string oldNameSuffix = newNameSuffix;
        newNameSuffix = String.Format("({0})", renameIndex);
        finalName = finalName.Replace(oldNameSuffix, newNameSuffix);
        filePath = Path.Combine(desktopPath, finalName);
    }

    return finalName;
}

private string GetCurrentUserDesktopPath()
{
    return Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
}

运行截图:

C#实现一个局域网文件传输工具

完整代码可以到下面的地址下载:

http://download.csdn.net/detail/qwertyupoiuytr/9895436


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136239.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • python监控网站更新_Python 通过网站search功能监控网站内容更新[通俗易懂]

    python监控网站更新_Python 通过网站search功能监控网站内容更新[通俗易懂]接到需求帮朋友监控一个信息网站,当该网站有相关的数据更新的时候发送信息到指定邮箱.下面是相关的python脚本,用到了httplib,time,sys,smtplib模块#!/usr/bin/envpython#coding=utf8importhttplib,timeimportsysimportsmtplibreload(sys)sys.setdefaultencoding(…

  • centos安装git命令_linuxjdk安装

    centos安装git命令_linuxjdk安装一、查看是否安装过git,git–version若出现以上版本号,则代表已经安装了git,不需要再次安装了,否则就安装其实安装的话,分为用yum安装和下载git源码编译安装。但是cetos5以及以下版本中的yum都没有git,无法使用yum安装,而cetos6可以使用yum安装git,但是安装的git是1.7.1版本的,而github需要的git版本最低都不能低于1.7.2。所以如…

  • 反射之invoke方法

    反射之invoke方法当获得类对应的Class对象后,可以通过getMethods()方法得到全部方法——返回Method数组,或者getMethod()方法来获取指定方法——Method对象。可以通过Method对象的invoke()方法来调用它对应的方法。Objectinvoke(Objectobj,Object…args):obj是执行该方法的Class对象,args是执行该方法时传入的实参。

  • 精灵图定位

    精灵图定位.searchem{position:absolute;top:8px;right:90px;width:20px;height:20px;background:url(…/images/sprite-search.png)no-repeat;}.searchem:hover{background-position:-30px0;/*精灵图第一个值是X轴的…

  • 亿能bms上位机_上位机软件 上位机PC软件 bms电池管理系统测试系统软件「建议收藏」

    亿能bms上位机_上位机软件 上位机PC软件 bms电池管理系统测试系统软件「建议收藏」上位机软件上位机PC软件bms电池管理系统测试系统软件上海鸣野软件开发公司从创立伊始就立足于差异化竞争,只做自己有竞争优势的事,13391389262,,在上位机软件,上位机开发,bms电池管理系统测试系统软件,上位机软件外包积累了丰富的开发经验,通过学习和创新,不断地扩大和加强公司在行业中的优势,是企业发展之道。关注别人所不关注的,看到别人所看不到的,做到别人所做不到的,是鸣野软件开发公司*…

  • java运行环境_如何搭建Java运行环境?

    java运行环境_如何搭建Java运行环境?前言对于运行一个Java程序,我们首先需要有一个运行Java程序的平台程序,这个平台程序就是Java虚拟机。本篇文章将介绍如何在Windows系统上搭建一个Java程序的运行环境,主要分为两个步骤:安装JDK,配置环境变量。Windows系统Java运行环境配置下载JDK并安装首先我们需要下载java开发工具包JDK,JDK中包含了JRE(Java运行环境)和一些官方提供给我们的工具,在oracl…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号