当前位置:首页 > 教育 > 正文

VB中一个服务器和多个客户端的程序

具体的理论只是,参考 的上一部分

服务器端


PrivateSubCommand1_Click()
DimstrSendAsString
strSend=Text2.Text
Sock(1).SendDatastrSend
EndSub

PrivateSubCommand2_Click()
Command2.Caption=Sock(0).RemoteHostIP
EndSub

PrivateSubForm_Load()
'LoadSock(0)
Listener.LocalPort=8000'端口号
Listener.Listen'开始侦听
EndSub

PrivateSubListener_ConnectionRequest(ByValrequestIDAsLong)
DimSockIndexAsInteger
SockIndex=200
DimiAsInteger?
'遍历控件
Fori=0ToSock.UBound
IfSock(i).State=0ThenSockIndex=i
Next
IfSockIndex=200Then
LoadSock(Sock.UBound+1)
SockIndex=Sock.UBound
EndIf
'接受请求
Sock(SockIndex).Accept(requestID)
EndSub

PrivateSubSock_Close(IndexAsInteger)
IfSock(Index).State<>sckClosedThen
Sock(Index).Close
EndIf
EndSub

PrivateSubSock_DataArrival(IndexAsInteger,ByValbytesTotalAsLong)
DimstrGetAsString
'接收字符串并写入text中
Sock(Index).GetDatastrGet
Text1.Text=strGet
EndSub
------------------------------------------------------------------

客户端


PrivateSubCommand1_Click()
DimstrSetAsString
strSet=Text2.Text
Winsock1.SendDatastrSet
EndSub

PrivateSubCommand2_Click()
Me.Winsock1.RemoteHost="10.1.51.150"
Me.Winsock1.RemotePort=8000
Me.Winsock1.Connect
EndSub

PrivateSubWinsock1_DataArrival(ByValbytesTotalAsLong)
DimstrGetAsString
'接收字符串并写入Text1控件中
Winsock1.GetDatastrGet
Text1.Text=strGet
EndSub

PrivateSubForm_Load()

EndSub

你可能想看:

有话要说...

取消
扫码支持 支付码