您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > VB.NETTCP多客户端编程
'使用到TcpListen类ImportsSystem.Net.Sockets'使用到StreamWriter类ImportsSystem.IO'使用IPAddress类、IPHostEntry类等ImportsSystem.NetPublicClassMyClientImplementsIDisposable#Region自定义变量'远程服务器IP地址PrivateServerIPAsIPAddress'远程服务器端口号码PrivatePortNumAsInteger=0'创建发送数据的网络基础数据流PrivateSocketStreamAsNetworkStream'用以向网络基础数据流传送数据PrivateWriterAsBinaryWriter'用以向网络基础数据流获得数据PrivateReaderAsBinaryReader'定义一个客户端对象PrivateClientAsTcpClient'客户名称PrivateclientNameAsString'执行状态消息以显示于窗体PublicEventOnText(ByValmsgAsString)'与远程服务器是否连接PublicIsConnectedAsBoolean#EndRegion#Region初始化远程服务器端口'''summary'''初始化远程服务器端口定义'''/summarySubNew(ByValSerIPAsIPAddress,ByValPortAsInteger,ByValTagAsString)ServerIP=SerIPPortNum=PortclientName=TagEndSub'''summary'''初始化远程服务器端口定义'''/summarySubNew(ByValSerIPAsString,ByValPortAsInteger,ByValTagAsString)ServerIP=IPAddress.Parse(SerIP)PortNum=PortclientName=TagEndSub#EndRegion#Region自定义过程'''summary'''与远程服务器建立一个连接'''/summaryPublicSubConnet()'初始化一个客户端对象,通过它实现向远程主机提出TCP连接申请Client=NewTcpClientTry'对远程主机的5000端口提出TCP连接申请Client.Connect(ServerIP,5000)'通过申请,并获取传送数据的网络基础数据流SocketStream=Client.GetStream'使用获取的网络基础数据流来初始化StreamReader实例Reader=NewBinaryReader(SocketStream)'使用获取的网络基础数据流来初始化StreamWriter实例Writer=NewBinaryWriter(SocketStream)'提醒服务器用户连接已经成功。Writer.Write(clientName&-连接成功&vbCrLf)IsConnected=TrueRaiseEventOnText(服务器连接成功)CatchexAsExceptionRaiseEventOnText(服务器连接失败,请检查)EndTryEndSub'''summary'''与远程服务器断开连接'''/summaryPublicSubDisConnect()IsConnected=FalseIfClientIsNotNothingThenClient.Close()IfWriterIsNotNothingThenWriter.Close()IfSocketStreamIsNotNothingThenSocketStream.Close()EndSub'''summary'''获取服务器发送来的消息'''/summaryPublicFunctionGetMessage()AsStringDimstrMsgAsString=IfSocketStreamIsNothingThenIsConnected=FalseRaiseEventOnText(与服务器的连接已断开)Return与服务器的连接已断开EndIfReader=NewBinaryReader(SocketStream)TrystrMsg=Reader.ReadStringRaiseEventOnText(strMsg)ReturnstrMsgCatchexAsIOExceptionSocketStream=NothingThreading.Thread.Sleep(50)IsConnected=FalseRaiseEventOnText(ex.Message)FinallyReader=NothingEndTryReturnstrMsgEndFunction'''summary'''向服务器发送消息'''/summaryPublicSubSetMessage(ByValmsgAsString)IfNotSocketStreamIsNothingThen'Writer=NewBinaryWriter(SocketStream)Writer.Write(clientName&&msg&vbCrLf)'刷新当前数据流中的数据Writer.Flush()ElseIsConnected=FalseEndIfEndSub#EndRegion#RegionIDisposableSupportPrivatedisposedValueAsBoolean'检测冗余的调用'IDisposableProtectedOverridableSubDispose(ByValdisposingAsBoolean)IfNotMe.disposedValueThenIfdisposingThen'TODO:释放托管状态(托管对象)。EndIfIfClientIsNotNothingThenClient.Close()IfWriterIsNotNothingThenWriter.Close()IfSocketStreamIsNotNothingThenSocketStream.Close()Client=NothingWriter=NothingReader=NothingSocketStream=Nothing'TODO:释放非托管资源(非托管对象)并重写下面的Finalize()。'TODO:将大型字段设置为null。EndIfMe.disposedValue=TrueEndSub'TODO:仅当上面的Dispose(ByValdisposingAsBoolean)具有释放非托管资源的代码时重写Finalize()。'ProtectedOverridesSubFinalize()''不要更改此代码。请将清理代码放入上面的Dispose(ByValdisposingAsBoolean)中。'Dispose(False)'MyBase.Finalize()'EndSub'VisualBasic添加此代码是为了正确实现可处置模式。PublicSubDispose()ImplementsIDisposable.Dispose'不要更改此代码。请将清理代码放入上面的Dispose(ByValdisposingAsBoolean)中。Dispose(True)GC.SuppressFinalize(Me)EndSub#EndRegionEndClassImportsSystem.NetImportsSystem.Net.SocketsImportsSystemImportsSystem.ThreadingImportsSystem.IOPublicClassChatServerImplementsIDisposable#Region自定义变量PrivateReadOnlyServerIPAsIPAddressPrivateReadOnlyPortNumAsInteger=5000PrivateReadOnlyServerTagAsStringPrivateSocketListAsNewArrayListPrivateMySocketAsSocket=NothingPrivatelistenerAsTcpListenerPrivateSocketStreamAsNetworkStreamPrivateWriterAsBinaryWriterPrivateReaderAsBinaryReaderPrivateNewThreadAsThread=NothingPrivateIsStartedAsBoolean=False'服务器状态为未启动PrivateClientCountAsInteger=0ConstCMAXCOUNT=30'容许连接的最大客户端数目PublicEventOnText(ByValNAsInteger,ByValmsgAsString)PrivateIndexAsIntegerPublicClientRemoteEndPointAsString#EndRegion#Region初始化服务器端口参数'''summary'''初始化服务器端口参数'''/summarySubNew(ByValSerIPAsIPAddress,ByValPortAsInteger,ByValSerTagAsString)ServerIP=SerIPPortNum=PortServerTag=SerTagEndSub'''summary'''初始化服务器端口参数'''/summarySubNew(ByValSerIPAsString,ByValPortAsInteger,ByValSerTagAsString)ServerIP=IPAddress.Parse(SerIP)PortNum=PortServerTag=SerTagEndSub#EndRegion#Region属性区域'''summary'''服务器连接状态'''/summaryPublicReadOnlyPropertyIsConnectedAsBooleanGetReturnIsStartedEndGetEndProperty#EndRegion#Region自定义过程'''summary'''创建服务器'''/summaryPrivateSubStart()Try'创建一个侦听对象listener=NewTcpListener(ServerIP,PortNum)'启动服务器侦听端口listener.Start()IsStarted=TrueRaiseEventOnText(3,服务器已启动)WhileTrue'连接客户端,返回一个套接字IfClientCount=CMAXCOUNTThenMySocket=listener.AcceptSocket()'添加到连接列表中SocketList.Add(MySocket)ClientRemoteEndPoint=MySocket.RemoteEndPoint.ToString()RaiseEventOnText(1,ClientRemoteEndPoint)SocketStream=NewNetworkStream(MySocket)Writer=NewBinaryWriter(SocketStream)'通知客户端,连接成功Writer.Write(与服务器成功连接&vbCrLf)'应该在什么时候断开该连接Writer.Flush()ClientCount+=1ElseMySocket=listener.AcceptSocket()SocketStream=NewNetworkStream(MySocket)Writer=NewBinaryWriter(SocketStream)'通知客户端,连接成功Writer.Write(Server服务器的连接数已满,连接已
本文标题:VB.NETTCP多客户端编程
链接地址:https://www.777doc.com/doc-4006427 .html