當前位置:首頁 > 教育 > 正文

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

你可能想看:

有話要說...

取消
掃碼支持 支付碼