資源簡介
網絡通信 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個文件信息
評論
共有 條評論