• 大小: 83.33 KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-10-17
  • 语言: C#
  • 标签: C#  socket  tcp  文件传输  聊天  

资源简介

在研究C#IM的过程中,循序渐进的学习和研究C#网络编程,写成了本C#异步多线程文件传输和文字聊天系统,对刚刚学习网络编程的人有一定的帮助,因为是练习作品,所以请大家莫拍砖。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace Filetranser
{
    /// 
    /// 发送文件端
    /// 

    class FileClient
    {
        public string FileName;
        public IPAddress myIP;
        public int myPort;
        public FileClient(string _FileNameIPAddress _myIPint _myPort)
        {
            FileName = _FileName;
            myIP = _myIP;
            myPort = _myPort;
        }
        public void Send()
        {
            byte[] myType = new byte[255];
            byte[] FileNameByte = new byte[255];
            byte[] ResultByte = new byte[7];
            byte[] FIleLength = new byte[255];
            IPEndPoint iep = new IPEndPoint(myIP myPort);
            Socket myServer = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
            myServer.Connect(iep);
            for (int i = 0; i < Encoding.UTF8.GetBytes(“FILE“).Length; i++)
            {
                myType[i] = Encoding.UTF8.GetBytes(“FILE“)[i];
            }
            myServer.Send(myType);
            string NewFileName = FileName.Substring(FileName.LastIndexOf(“\\“) + 1 FileName.Length - FileName.LastIndexOf(“\\“) - 1);
            for (int i = 0; i < Encoding.UTF8.GetBytes(NewFileName).Length; i++)
            {
                FileNameByte[i] = Encoding.UTF8.GetBytes(NewFileName)[i];
            }
            myServer.Send(FileNameByte);
            //获得一个回复
            myServer.Receive(ResultByte 0 7 0);
            if (Encoding.UTF8.GetString(ResultByte 0 7) == “ACCEPTS“)
            {
                FileStream fs = new FileStream(FileName FileMode.Open FileAccess.Read);
                byte[] databyte = new byte[(int)fs.Length];
               
                int FsLength = (int)fs.Length;
                Encoding.UTF8.GetBytes(FsLength.ToString()).CopyTo(FIleLength 0);
                myServer.Send(FIleLength);
                fs.Read(databyte 0 FsLength);
                int isSending = 0;
                int isSended = 0;
                int sendSize =1024;
                while (isSended < FsLength)
                {
                    if (isSended + sendSize < FsLength)
                    {
                        isSending = sendSize;
                    }
                    else
                    {
                        isSending = FsLength - isSended;
                    }
                   
                   
                    myServer.Send(databyte isSended isSending 0);
                    isSended += isSending;
                    while (true)
                    {
                        byte[] StatusOK = new byte[2];
                        myServer.Receive(StatusOK 0 2 SocketFlags.None);
                        if (Encoding.UTF8.GetString(StatusOK) == “OK“)
                        {

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      14848  2010-01-18 10:51  Filetranser\Filetranser\bin\Debug\f

     文件      14848  2010-01-18 13:14  Filetranser\Filetranser\bin\Debug\Filetranser.exe

     文件      34304  2010-01-18 13:14  Filetranser\Filetranser\bin\Debug\Filetranser.pdb

     文件      14328  2010-01-18 13:17  Filetranser\Filetranser\bin\Debug\Filetranser.vshost.exe

     文件        490  2007-07-21 01:33  Filetranser\Filetranser\bin\Debug\Filetranser.vshost.exe.manifest

     文件      26707  2010-01-18 13:15  Filetranser\Filetranser\bin\Debug.rar

     文件       3252  2010-01-18 13:14  Filetranser\Filetranser\FileClient.cs

     文件       4015  2010-01-18 10:49  Filetranser\Filetranser\Filetranser.csproj

     文件       5241  2010-01-18 11:46  Filetranser\Filetranser\Form1.cs

     文件       6781  2010-01-15 20:54  Filetranser\Filetranser\Form1.Designer.cs

     文件       6017  2010-01-15 20:54  Filetranser\Filetranser\Form1.resx

     文件        712  2010-01-18 13:17  Filetranser\Filetranser\obj\Debug\Filetranser.csproj.FileListAbsolute.txt

     文件        847  2010-01-15 20:57  Filetranser\Filetranser\obj\Debug\Filetranser.csproj.GenerateResource.Cache

     文件      14848  2010-01-18 13:14  Filetranser\Filetranser\obj\Debug\Filetranser.exe

     文件        180  2010-01-18 10:49  Filetranser\Filetranser\obj\Debug\Filetranser.Form1.resources

     文件      34304  2010-01-18 13:14  Filetranser\Filetranser\obj\Debug\Filetranser.pdb

     文件        180  2010-01-18 10:49  Filetranser\Filetranser\obj\Debug\Filetranser.Properties.Resources.resources

     文件        492  2010-01-14 17:19  Filetranser\Filetranser\Program.cs

     文件       1372  2010-01-14 17:19  Filetranser\Filetranser\Properties\AssemblyInfo.cs

     文件       2872  2010-01-14 17:19  Filetranser\Filetranser\Properties\Resources.Designer.cs

     文件       5612  2010-01-14 17:19  Filetranser\Filetranser\Properties\Resources.resx

     文件       1096  2010-01-14 17:19  Filetranser\Filetranser\Properties\Settings.Designer.cs

     文件        249  2010-01-14 17:19  Filetranser\Filetranser\Properties\Settings.settings

     文件       1183  2010-01-18 11:46  Filetranser\Filetranser\ServerListener.cs

     文件       4855  2010-01-18 13:11  Filetranser\Filetranser\ThreadGetMsg.cs

     文件        923  2010-01-14 17:19  Filetranser\Filetranser.sln

    ..A..H.     17408  2010-01-18 13:17  Filetranser\Filetranser.suo

     目录          0  2010-01-15 22:12  Filetranser\Filetranser\obj\Debug\Refactor

     目录          0  2010-01-14 17:19  Filetranser\Filetranser\obj\Debug\TempPE

     目录          0  2010-01-18 11:43  Filetranser\Filetranser\bin\Debug

............此处省略9个文件信息

评论

共有 条评论