• 大小: 168KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-07
  • 语言: C#
  • 标签: 网络通信  Tcp/Udp  

资源简介

网络通信 Tcp/Udp 传输字串、文件、对象 Tcp粘包、半包 1.封装了Tcp/Udp传输字串、文件、对象的细节,处理了Tcp粘包问题 2.测试代码设计原始Socket、TcpListener、TcpClient、UdpClient的使用 3.测试代码包括一个可以发送文本消息和发送文件的聊天室 4.设计网络通信、多线程、异步委托、异步套接字等技术

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Net;
using TcpLabCommon;
using System.IO;
using System.Text;

namespace TcpLabSvr1
{
    class Program
    {       
        static void Main()
        {
            Socket serverSocket = new Socket(AddressFamily.InterNetworkSocketType.StreamProtocolType.Tcp);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(“127.0.0.1“)6000);
            serverSocket.Bind(endPoint);
            serverSocket.Listen(3);
            Socket clientSocket = serverSocket.Accept();
            NetworkStream netStream = new NetworkStream(clientSocket);
            NetPacketService packetSvr = new NetPacketTcpService(netStream);
            
            do{
                NetPacket packet = packetSvr.ReceivePacket();
                switch (packet.PacketHead.PType)
                {
                    case PacketType.STRING:
                        Console.WriteLine(Convert.ToString(packet.Data));
                        break;
                    case PacketType.BINARY:
                        NetFile f = packetSvr.ParseFile((Byte[])packet.Data);
                        Console.WriteLine(“成功接收文件:“+f.FileName);
                        FileStream fs = new FileStream(f.FileNameFileMode.Create);
                        fs.Write(f.Content 0 f.Content.Length);
                        fs.Flush();
                        fs.Close();
                        break;
                    case PacketType.COMPLEX:
                        Book book = packet.Data as Book;
                        Console.WriteLine(book.title+“\t“+book.Price);
                        break;
                    default:
                        break;
                }
            }while(true);

        }
    }
}

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

     文件      11244  2012-03-27 10:20  NetLabSolution\NetLabSolution.sln

    ..A..H.    157184  2012-03-28 10:21  NetLabSolution\NetLabSolution.suo

     文件       9216  2012-03-25 21:29  NetLabSolution\TcpLab1\bin\Debug\TcpLab1.exe

     文件      26112  2012-03-25 21:29  NetLabSolution\TcpLab1\bin\Debug\TcpLab1.pdb

     文件      11600  2012-03-27 18:50  NetLabSolution\TcpLab1\bin\Debug\TcpLabSvr1.vshost.exe

     文件        490  2010-03-17 22:39  NetLabSolution\TcpLab1\bin\Debug\TcpLabSvr1.vshost.exe.manifest

     文件       2152  2012-03-25 14:46  NetLabSolution\TcpLab1\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6631  2012-03-28 09:45  NetLabSolution\TcpLab1\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件       1057  2012-03-25 21:30  NetLabSolution\TcpLab1\obj\x86\Debug\TcpLab1.csproj.FileListAbsolute.txt

     文件        180  2012-03-25 21:29  NetLabSolution\TcpLab1\obj\x86\Debug\TcpLab1.Properties.Resources.resources

     文件       4608  2012-03-25 21:46  NetLabSolution\TcpLab1\obj\x86\Debug\TempPE\Properties.Resources.Designer.cs.dll

     文件       1944  2012-03-27 18:47  NetLabSolution\TcpLab1\Program.cs

     文件       1346  2012-03-25 14:46  NetLabSolution\TcpLab1\Properties\AssemblyInfo.cs

     文件       2858  2012-03-25 21:31  NetLabSolution\TcpLab1\Properties\Resources.Designer.cs

     文件       5612  2012-03-25 14:46  NetLabSolution\TcpLab1\Properties\Resources.resx

     文件       1108  2012-03-25 21:31  NetLabSolution\TcpLab1\Properties\Settings.Designer.cs

     文件        249  2012-03-25 14:46  NetLabSolution\TcpLab1\Properties\Settings.settings

     文件       3735  2012-03-25 23:52  NetLabSolution\TcpLab1\TcpLabSvr1.csproj

     文件        227  2012-03-27 18:52  NetLabSolution\TcpLab1\TcpLabSvr1.csproj.user

     文件        461  2012-03-28 09:53  NetLabSolution\TcpLabChat1\App.config

     文件      11600  2012-03-28 16:20  NetLabSolution\TcpLabChat1\bin\Debug\TcpLabChat1.vshost.exe

     文件        461  2012-03-28 09:53  NetLabSolution\TcpLabChat1\bin\Debug\TcpLabChat1.vshost.exe.config

     文件        490  2010-03-17 22:39  NetLabSolution\TcpLabChat1\bin\Debug\TcpLabChat1.vshost.exe.manifest

     文件       7291  2012-03-27 10:19  NetLabSolution\TcpLabChat1\FrmChat.cs

     文件       3986  2012-03-26 22:21  NetLabSolution\TcpLabChat1\FrmChat.Designer.cs

     文件       5817  2012-03-26 22:21  NetLabSolution\TcpLabChat1\FrmChat.resx

     文件       4440  2012-03-27 09:45  NetLabSolution\TcpLabChat1\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6882  2012-03-28 09:45  NetLabSolution\TcpLabChat1\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        494  2012-03-26 21:26  NetLabSolution\TcpLabChat1\Program.cs

     文件       1354  2012-03-26 21:25  NetLabSolution\TcpLabChat1\Properties\AssemblyInfo.cs

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

评论

共有 条评论