使用SOCKET实现TCP-IP协议的通讯最好的DEMO源码

上传者: pendy2008 | 上传时间: 2020-01-03 11:28:09 | 文件大小: 330KB | 文件类型: zip
首先要理解基本的原理,2台电脑间实现TCP通讯,首先要建立起连接,在这里要提到服务器端与客户端,两个的区别通俗讲就是主动与被动的关系,两个人对话,肯定是先有人先发起会话,要不然谁都不讲,谈什么话题,呵呵!一样,TCPIP下建立连接首先要有一个服务器,它是被动的,它只能等待别人跟它建立连接,自己不会去主动连接,那客户端如何去连接它呢,这里提到2个东西,IP地址和端口号,通俗来讲就是你去拜访某人,知道了他的地址是一号大街2号楼,这个是IP地址,那么1号楼这么多门牌号怎么区分,嗯!门牌号就是端口(这里提到一点,我们访问网页的时候也是IP地址和端口号,IE默认的端口号是80),一个服务器可以接受多个客户端的连接,但是一个客户端只能连接一台服务器,在连接后,服务器自动划分内存区域以分配各个客户端的通讯,那么,那么多的客户端服务器如何区分,你可能会说,根据IP么,不是很完整,很简单的例子,你一台计算机开3个QQ,服务器怎么区分?所以准确的说是IP和端口号,但是客户端的端口号不是由你自己定的,是由计算机自动分配的,要不然就出现端口冲突了,说的这么多,看下面的这张图就简单明了了。 在上面这张图中,你可以理解为程序A和程序B是2个SOCKET程序,服务器端程序A设置端口为81,已接受到3个客户端的连接,计算机C开了2个程序,分别连接到E和D,而他的端口是计算机自动分配的,连接到E的端口为789,连接到D的为790。 了解了TCPIP通讯的基本结构后,接下来讲解建立的流程,首先声明一下我用的开发环境是Visual Studio2008版的,语言C#,组件System.Net.Sockets,流程的建立包括服务器端的建立和客户端的建立,如图所示: 二、实现: 1.客户端: 第一步,要创建一个客户端对象TcpClient(命名空间在System.Net.Sockets),接着,调用对象下的方法BeginConnect进行尝试连接,入口参数有4个,address(目标IP地址),port(目标端口号),requestCallback(连接成功后的返调函数),state(传递参数,是一个对象,随便什么都行,我建议是将TcpClient自己传递过去),调用完毕这个函数,系统将进行尝试连接服务器。 第二步,在第一步讲过一个入口参数requestCallback(连接成功后的返调函数),比如我们定义一个函数void Connected(IAsyncResult result),在连接服务器成功后,系统会调用此函数,在函数里,我们要获取到系统分配的数据流传输对象(NetworkStream),这个对象是用来处理客户端与服务器端数据传输的,此对象由TcpClient获得,在第一步讲过入口参数state,如果我们传递了TcpClient进去,那么,在函数里我们可以根据入口参数state获得,将其进行强制转换TcpClient tcpclt = (TcpClient)result.AsyncState,接着获取数据流传输对象NetworkStream ns = tcpclt.GetStream(),此对象我建议弄成全局变量,以便于其他函数调用,接着我们将挂起数据接收等待,调用ns下的方法BeginRead,入口参数有5个,buff(数据缓冲),offset(缓冲起始序号),size(缓冲长度),callback(接收到数据后的返调函数),state(传递参数,一样,随便什么都可以,建议将buff传递过去),调用完毕函数后,就可以进行数据接收等待了,在这里因为已经创建了NetworkStream对象,所以也可以进行向服务器发送数据的操作了,调用ns下的方法Write就可以向服务器发送数据了,入口参数3个,buff(数据缓冲),offset(缓冲起始序号),size(缓冲长度)。 第三步,在第二步讲过调用了BeginRead函数时的一个入口参数callback(接收到数据后的返调函数),比如我们定义了一个函数void DataRec(IAsyncResult result),在服务器向客户端发送数据后,系统会调用此函数,在函数里我们要获得数据流(byte数组),在上一步讲解BeginRead函数的时候还有一个入口参数state,如果我们传递了buff进去,那么,在这里我们要强制转换成byte[]类型byte[] data= (byte[])result.AsyncState,转换完毕后,我们还要获取缓冲区的大小int length = ns.EndRead(result),ns为上一步创建的NetworkStream全局对象,接着我们就可以对数据进行处理了,如果获取的length为0表示客户

文件下载

资源详情

[{"title":"( 77 个子文件 330KB ) 使用SOCKET实现TCP-IP协议的通讯最好的DEMO源码","children":[{"title":"TcpIpDemo","children":[{"title":".vs","children":[{"title":"TcpIpDemo","children":[{"title":"v15","children":[{"title":".suo <span style='color:#111;'> 90.50KB </span>","children":null,"spread":false},{"title":"Server","children":[{"title":"sqlite3","children":[{"title":"storage.ide <span style='color:#111;'> 568.00KB </span>","children":null,"spread":false},{"title":"db.lock <span style='color:#111;'> 0B </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true}],"spread":true},{"title":"TcpIpDemo.sln <span style='color:#111;'> 1.43KB </span>","children":null,"spread":false},{"title":"TcpIpDemo.v12.suo <span style='color:#111;'> 71.00KB </span>","children":null,"spread":false},{"title":"TcpIpDemo","children":[{"title":"bin","children":[{"title":"Debug","children":[{"title":"TcpIpDemo.vshost.exe.manifest <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"TcpIpDemo.vshost.exe.config <span style='color:#111;'> 189B </span>","children":null,"spread":false},{"title":"TcpIpDemo.exe <span style='color:#111;'> 11.50KB </span>","children":null,"spread":false},{"title":"TcpIpDemo.exe.config <span style='color:#111;'> 189B </span>","children":null,"spread":false},{"title":"TcpIpDemo.vshost.exe <span style='color:#111;'> 22.63KB </span>","children":null,"spread":false},{"title":"TcpIpDemo.pdb <span style='color:#111;'> 27.50KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Form1.Designer.cs <span style='color:#111;'> 5.14KB </span>","children":null,"spread":false},{"title":"Program.cs <span style='color:#111;'> 521B </span>","children":null,"spread":false},{"title":"obj","children":[{"title":"Debug","children":[{"title":"TcpIpDemo.exe <span style='color:#111;'> 11.50KB </span>","children":null,"spread":false},{"title":"TcpIpDemo.Properties.Resources.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"TcpIpDemo.Form1.resources <span style='color:#111;'> 180B </span>","children":null,"spread":false},{"title":"TcpIpDemo.csproj.FileListAbsolute.txt <span style='color:#111;'> 1.67KB </span>","children":null,"spread":false},{"title":"TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"TcpIpDemo.csprojResolveAssemblyReference.cache <span style='color:#111;'> 12.84KB </span>","children":null,"spread":false},{"title":"TcpIpDemo.csproj.GenerateResource.cache <span style='color:#111;'> 1012B </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferencesInput.cache <span style='color:#111;'> 6.86KB </span>","children":null,"spread":false},{"title":"TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferences.cache <span style='color:#111;'> 868B </span>","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false},{"title":"TcpIpDemo.pdb <span style='color:#111;'> 27.50KB </span>","children":null,"spread":false},{"title":"TcpIpDemo.csproj.CoreCompileInputs.cache <span style='color:#111;'> 42B </span>","children":null,"spread":false},{"title":"TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs <span style='color:#111;'> 0B </span>","children":null,"spread":false}],"spread":false}],"spread":true},{"title":"ClientServer.cs <span style='color:#111;'> 3.06KB </span>","children":null,"spread":false},{"title":"Test.cs <span style='color:#111;'> 2.99KB </span>","children":null,"spread":false},{"title":"Form1.cs <span style='color:#111;'> 808B </span>","children":null,"spread":false},{"title":"TcpIpDemo.csproj <span style='color:#111;'> 3.86KB </span>","children":null,"spread":false},{"title":"Form1.resx <span style='color:#111;'> 5.68KB </span>","children":null,"spread":false},{"title":"App.config <span style='color:#111;'> 189B </span>","children":null,"spread":false},{"title":"Properties","children":[{"title":"Resources.resx <span style='color:#111;'> 5.48KB </span>","children":null,"spread":false},{"title":"Settings.settings <span style='color:#111;'> 249B </span>","children":null,"spread":false},{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.31KB </span>","children":null,"spread":false},{"title":"Settings.Designer.cs <span style='color:#111;'> 1.07KB </span>","children":null,"spread":false},{"title":"Resources.Designer.cs <span style='color:#111;'> 2.80KB </span>","children":null,"spread":false}],"spread":false}],"spread":false},{"title":"Server","children":[{"title":"bin","children":[{"title":"Debug","children":[{"title":"Server.vshost.exe.config <span style='color:#111;'> 189B </span>","children":null,"spread":false},{"title":"Server.vshost.exe.manifest <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"Server.pdb <span style='color:#111;'> 15.50KB </span>","children":null,"spread":false},{"title":"Server.vshost.exe <span style='color:#111;'> 22.63KB </span>","children":null,"spread":false},{"title":"Server.exe <span style='color:#111;'> 7.00KB </span>","children":null,"spread":false},{"title":"Server.exe.config <span style='color:#111;'> 189B </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"TServer.cs <span style='color:#111;'> 3.65KB </span>","children":null,"spread":false},{"title":"Program.cs <span style='color:#111;'> 326B </span>","children":null,"spread":false},{"title":"obj","children":[{"title":"Debug","children":[{"title":"Server.csprojResolveAssemblyReference.cache <span style='color:#111;'> 8.92KB </span>","children":null,"spread":false},{"title":"TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"Server.pdb <span style='color:#111;'> 15.50KB </span>","children":null,"spread":false},{"title":"Server.csproj.CoreCompileInputs.cache <span style='color:#111;'> 42B </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferencesInput.cache <span style='color:#111;'> 6.53KB </span>","children":null,"spread":false},{"title":"TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"Server.exe <span style='color:#111;'> 7.00KB </span>","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false},{"title":"TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"Server.csproj.FileListAbsolute.txt <span style='color:#111;'> 905B </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Server.csproj <span style='color:#111;'> 2.67KB </span>","children":null,"spread":false},{"title":"使用SOCKET实现TCP-IP协议的通讯.docx <span style='color:#111;'> 42.89KB </span>","children":null,"spread":false},{"title":"App.config <span style='color:#111;'> 189B </span>","children":null,"spread":false},{"title":"Properties","children":[{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.30KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"TServer","children":[{"title":"bin","children":[{"title":"Debug","children":[{"title":"TServer.exe.config <span style='color:#111;'> 189B </span>","children":null,"spread":false},{"title":"TServer.exe <span style='color:#111;'> 6.50KB </span>","children":null,"spread":false},{"title":"TServer.vshost.exe.config <span style='color:#111;'> 189B </span>","children":null,"spread":false},{"title":"TServer.vshost.exe <span style='color:#111;'> 22.63KB </span>","children":null,"spread":false},{"title":"TServer.vshost.exe.manifest <span style='color:#111;'> 490B </span>","children":null,"spread":false},{"title":"TServer.pdb <span style='color:#111;'> 15.50KB </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Program.cs <span style='color:#111;'> 246B </span>","children":null,"spread":false},{"title":"obj","children":[{"title":"Debug","children":[{"title":"Server.csprojResolveAssemblyReference.cache <span style='color:#111;'> 1.73KB </span>","children":null,"spread":false},{"title":"TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"TServer.exe <span style='color:#111;'> 6.50KB </span>","children":null,"spread":false},{"title":"DesignTimeResolveAssemblyReferencesInput.cache <span style='color:#111;'> 6.53KB </span>","children":null,"spread":false},{"title":"TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"TempPE","children":null,"spread":false},{"title":"TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs <span style='color:#111;'> 0B </span>","children":null,"spread":false},{"title":"TServer.pdb <span style='color:#111;'> 15.50KB </span>","children":null,"spread":false},{"title":"Server.csproj.FileListAbsolute.txt <span style='color:#111;'> 603B </span>","children":null,"spread":false}],"spread":true}],"spread":true},{"title":"Server.cs <span style='color:#111;'> 3.06KB </span>","children":null,"spread":false},{"title":"Server.csproj <span style='color:#111;'> 2.60KB </span>","children":null,"spread":false},{"title":"App.config <span style='color:#111;'> 189B </span>","children":null,"spread":false},{"title":"Properties","children":[{"title":"AssemblyInfo.cs <span style='color:#111;'> 1.31KB </span>","children":null,"spread":false}],"spread":true}],"spread":true}],"spread":true}],"spread":true}]

评论信息

  • sanqima :
    代码写的很基础,值得一看。
    2018-11-28
  • YJRong :
    Socket报文和端口测试工具的开发
    2018-11-26

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明