VB:用Winsock控件数组作服务的实现过程范例

前端之家收集整理的这篇文章主要介绍了VB:用Winsock控件数组作服务的实现过程范例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Visual Basic Code
PrivateTypeSockServerInfo
EnabledAsBoolean
EndType
DimServer ( ) AsSockServerInfo

PrivateSubForm_Load ( )
CreateServer
EndSub


PrivateSubWinsock1_ConnectionRequest ( _
IndexAsInteger,_
ByValrequestIDAsLong)
IfWinsock1 ( Index ) .State <>0ThenWinsock1 ( Index ) .Close
Winsock1 ( Index ) .Accept requestID
CreateServer
EndSub

PrivateSubWinsock1_DataArrival ( IndexAsInteger,ByValbytesTotalAsLong)
DimNetWorkStringAsString
Winsock1 ( Index ) .GetData NetWorkString,vbString,bytesTotal
Print"Index "& Index &" 收到数据:"& NetWorkString
Winsock1 ( Index ) .SendData"转发回去的数据"
EndSub

PrivateSubWinsock1_Error ( IndexAsInteger,_
ByValNumberAsInteger,_
DescriptionAsString,_
ByValScodeAsLong,_
ByValSourceAsString,_
ByValHelpFileAsString,_
ByValHelpContextAsLong,_
CancelDisplayAsBoolean)
CancelDisplay = True
Winsock1_Close Index
EndSub

PrivateSubWinsock1_Close ( IndexAsInteger)
StopServer Index
EndSub

PrivateSubCreateServer ( )
DimServerMaxCountAsLong
DimXAsLong
DimIsHaveFalseAsLong
OnErrorResumeNext
ServerMaxCount = UBound( Server )
IfErr.Number <>0Then
ServerMaxCount = 0
ReDimServer ( ServerMaxCount )
Else
IsHaveFalse = 0
ForX = 0ToServerMaxCount
IfServer ( X ) .Enabled = FalseThen
ServerMaxCount = X
IsHaveFalse = 1
ExitFor
EndIf
NextX
IfIsHaveFalse = 0Then
ServerMaxCount = ServerMaxCount +1
ReDimPreserve Server ( ServerMaxCount )
EndIf
Load Winsock1 ( ServerMaxCount )
EndIf
Winsock1 ( ServerMaxCount ) .LocalPort = 60000
Winsock1 ( ServerMaxCount ) .Listen
Server ( ServerMaxCount ) .Enabled = True
EndSub

PrivateSubStopServer ( IndexAsInteger)
DimServerMaxCountAsLong
IfWinsock1 ( Index ) .State <>0ThenWinsock1 ( Index ) .Close
OnErrorResumeNext
ServerMaxCount = UBound( Server )
IfIndex = ServerMaxCountThen
IfIndex = 0Then
EraseServer
Else
Unload Winsock1 ( Index )
ReDimPreserve Server ( ServerMaxCount -1)
EndIf
Else
Unload Winsock1 ( Index )
Server ( Index ) .Enabled = False
EndIf
EndSub

其实概念很简单,主要是处理好控件数组的创建和释放就可以了

猜你在找的VB相关文章