2011年3月30日 星期三

網路核心程式設計-Week6-雙winsock接收通訊



Dim state, check As Integer

Private Sub Command1_Click()
If Winsock1.state = 0 Then
Winsock1.Bind Winsock1.LocalPort, Winsock1.LocalIP

Label3.Caption = Winsock1.state
End If
End Sub

Private Sub Command2_Click()
If Winsock1.state = 1 Then
Winsock1.Listen
Label3.Caption = Winsock1.state
End If
End Sub

Private Sub Command3_Click()
If Winsock1.state = 2 Then
check = 1
End If
End Sub

Private Sub Command4_Click()
Winsock2.SendData Text2.Text

End Sub

Private Sub Form_Load()
Label1.Caption = Winsock1.LocalIP
Winsock1.LocalPort = 7777
Label2.Caption = Winsock1.LocalPort
Label3.Caption = Winsock1.state

End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
MsgBox "有人來找"
check = 0
Wait 5
If check = 1 Then
Winsock2.Accept requestID
Label4.Caption = Winsock2.state
Winsock2.SendData ("yes")
MsgBox "我會連線"
End If
End Sub
Private Sub Wait(iSec As Long)
Dim iTimer As Long
iTimer = Timer
Do Until Timer - iTimer >= iSec
If check = 1 Then
iSec = 0
End If
DoEvents
Loop
End Sub

Private Sub Winsock2_DataArrival(ByVal bytesTotal As Long)
Dim getdata As String
Winsock2.getdata getdata
List1.AddItem getdata
End Sub

1 則留言: