• 大小: 156KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-27
  • 语言: C#
  • 标签: socket  client  server  file  

资源简介

本代码主要是多个客户端向一个服务器发送文件。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;   

namespace file_client
{
    public partial class FileSendClient : Form
    {

        public static string path;
        public FileSendClient()
        {
            InitializeComponent();
        }   
  

        private void btnBrowse_Click(object sender EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtFilePath.Text = openFileDialog1.FileName;
                path = openFileDialog1.FileName;
            }   
        }

        private void btnSendFile_Click(object sender EventArgs e)
        {
            String ServerIP =“192.168.1.120“;
            string filename = path;  
            try
            {
                //为 Internet 主机地址信息提供容器类。   
                IPAddress ipHost = IPAddress.Parse(ServerIP);   //将主机名或 IP 地址解析为 IPHostEntry 实例。   
                Socket client = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
                IPEndPoint ipEndP = new IPEndPoint(ipHost11000);
                client.Connect(ipEndP);
                byte[] buf = Encoding.UTF8.GetBytes(filename);
                client.Send(Encoding.UTF8.GetBytes(string.Format(“{0:D10}“buf.Length)));
                client.Send(buf);
                FileStream file = File.Open(filename FileMode.Open FileAccess.Read);   //创建文件流             int start = 0;              int end = (int)file.Length;
                int end = (int)file.Length;
                file.Close();
                client.Send(Encoding.UTF8.GetBytes(string.Format(“{0:D10}“ end)));
                client.SendFile(filename);           
                client.Shutdown(SocketShutdown.Both); //to shutdown the Server‘receive and Client‘send 
                client.Close();
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.ToString());

            }   
           
        }

        private void Form1_Load(object sender EventArgs e)
        {

        }

       
       
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-05-16 15:37  server_and_client\
     目录           0  2013-05-16 15:37  server_and_client\file_client1\
     目录           0  2013-05-16 15:37  server_and_client\file_client1\file_client\
     文件         875  2013-05-14 08:23  server_and_client\file_client1\file_client.sln
     文件       26112  2013-05-16 15:36  server_and_client\file_client1\file_client.suo
     目录           0  2013-05-14 08:23  server_and_client\file_client1\file_client\bin\
     目录           0  2013-05-16 15:37  server_and_client\file_client1\file_client\bin\Debug\
     文件        9728  2013-05-15 17:56  server_and_client\file_client1\file_client\bin\Debug\file_client.exe
     文件       24064  2013-05-15 17:56  server_and_client\file_client1\file_client\bin\Debug\file_client.pdb
     文件       11600  2013-05-16 15:19  server_and_client\file_client1\file_client\bin\Debug\file_client.vshost.exe
     文件         490  2010-03-17 22:39  server_and_client\file_client1\file_client\bin\Debug\file_client.vshost.exe.manifest
     文件        3681  2013-05-14 08:40  server_and_client\file_client1\file_client\file_client.csproj
     文件        2405  2013-05-15 17:54  server_and_client\file_client1\file_client\Form1.cs
     文件        3635  2013-05-14 13:13  server_and_client\file_client1\file_client\Form1.Designer.cs
     文件        6020  2013-05-14 13:13  server_and_client\file_client1\file_client\Form1.resx
     目录           0  2013-05-14 08:23  server_and_client\file_client1\file_client\obj\
     目录           0  2013-05-14 08:23  server_and_client\file_client1\file_client\obj\x86\
     目录           0  2013-05-16 15:37  server_and_client\file_client1\file_client\obj\x86\Debug\
     文件        4501  2013-05-14 14:57  server_and_client\file_client1\file_client\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6266  2013-05-16 14:25  server_and_client\file_client1\file_client\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         755  2013-05-16 15:19  server_and_client\file_client1\file_client\obj\x86\Debug\file_client.csproj.FileListAbsolute.txt
     文件        9728  2013-05-15 17:56  server_and_client\file_client1\file_client\obj\x86\Debug\file_client.exe
     文件         180  2013-05-14 13:13  server_and_client\file_client1\file_client\obj\x86\Debug\file_client.FileSendClient.resources
     文件       24064  2013-05-15 17:56  server_and_client\file_client1\file_client\obj\x86\Debug\file_client.pdb
     文件         180  2013-05-14 08:40  server_and_client\file_client1\file_client\obj\x86\Debug\file_client.Properties.Resources.resources
     文件         204  2013-05-14 13:13  server_and_client\file_client1\file_client\obj\x86\Debug\GenerateResource.read.1.tlog
     文件         686  2013-05-14 13:13  server_and_client\file_client1\file_client\obj\x86\Debug\GenerateResource.write.1.tlog
     文件        8031  2013-05-14 15:13  server_and_client\file_client1\file_client\obj\x86\Debug\ResolveAssemblyReference.cache
     目录           0  2013-05-14 08:23  server_and_client\file_client1\file_client\obj\x86\Debug\TempPE\
     文件         501  2013-05-14 08:43  server_and_client\file_client1\file_client\Program.cs
     目录           0  2013-05-16 15:37  server_and_client\file_client1\file_client\Properties\
............此处省略76个文件信息

评论

共有 条评论