CLIENT間透過SERVER互傳 訊息
server端有三個Winsock
一個用來監聽
兩個分別配給client
兩個分別配給client
server端程式碼重點部分
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strData As StringWinsock1(1).GetData strData, vbString
txtReceived.Text = strData
Winsock1(2).GetData strData, vbString
Text1.Text = strData
If Index = 1 Then
Winsock1(2).SendData txtReceived.Text
Else
Winsock1(1).SendData Text1.Text
End If
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim i As Long
For i = 1 To 2
If Winsock1(i).State = sckClosed Then
Winsock1(i).Accept requestID
List1.AddItem "Local Port=" + Str(Winsock1(i).LocalPort) + "RemotePort = " + Str(Winsock1(i).RemotePort)
Exit For
End If
Next
'Winsock1.Close
'Winsock1.Accept requestID '改成接受
cmdSend.Enabled = True
End Sub
Dim i As Long
For i = 1 To 2
If Winsock1(i).State = sckClosed Then
Winsock1(i).Accept requestID
List1.AddItem "Local Port=" + Str(Winsock1(i).LocalPort) + "RemotePort = " + Str(Winsock1(i).RemotePort)
Exit For
End If
Next
'Winsock1.Close
'Winsock1.Accept requestID '改成接受
cmdSend.Enabled = True
End Sub
CLINET端程式碼
Private Sub cmdConnect_Click()Winsock1.LocalPort = 0 '以便自動產生Local Port
Winsock1.Connect "125.230.89.95" '設定改成您 Server 電腦的IP 號碼
End Sub
Private Sub cmdExit_Click()
Winsock1.SendData "close"
DoEvents
Winsock1.Close
Winsock1.LocalPort = 0
End Sub
Private Sub cmdSend_Click()
Winsock1.SendData txtOutput.Text
DoEvents
End Sub
Private Sub Form_Load()
Winsock1.RemotePort = 7777 '設定與Server端做Listen的Port相同
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Winsock1.State <> sckClosed Then
cmdExit_Click
End If
End Sub
Private Sub Winsock1_Connect()
If Winsock1.State = sckConnected Then
lstInput.AddItem "Connected! LocalPort =" & Winsock1.LocalPort _
& " RemptePort = " & Winsock1.RemotePort
End If
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim mydata As String
Winsock1.GetData mydata, vbString
lstInput.AddItem mydata
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
lstInput.AddItem Description
End Sub
如下圖所示,下面框框的內容"Text11"將透過server傳送至另一個client,
上面框框的內容"Text1554"就是從另一個client接收到的訊息
沒有留言:
張貼留言