thucgia > 12-04-15, 02:06 PM
Option Compare Database
Dim ten As String
Public WithEvents ws_server As MSWinsockLib.Winsock ' Vì không cho lôi thả nên phải sử dụng cái này
Public WithEvents ws_anser As MSWinsockLib.Winsock
Dim remoteHostIP As String
Dim remotePort As String
Private Sub btn_login_Click()
ten = InputBox("Vui long nhap ten", "What you name ?", "noname")
If ten = "" Then
ten = "noname"
End If
DoCmd.OpenForm "frm_client"
Me.btn_login.Enabled = False
End Sub
Private Sub Form_Load()
Set ws_server = New MSWinsockLib.Winsock
ws_server.Protocol = sckUDPProtocol
ws_server.Bind 1001 'server đang lắng nghe ở cổng số 1001, bạn có thể đặt khác tùy bạn
Set ws_anser = New MSWinsockLib.Winsock
ws_anser.Protocol = sckUDPProtocol
End Sub
Private Sub ws_server_DataArrival(ByVal bytesTotal As Long)
Dim msg As String
ws_server.GetData msg
Select Case msg
Case "in":
remoteHostIP = ws_server.remoteHostIP
remotePort = ws_server.remotePort
Me.txt_output_server = "[" & ten & "]" & " login" & vbCrLf & _
Me.txt_output_server
ws_anser.RemoteHost = remoteHostIP 'Chỉnh súng bắn ngược lại client
ws_anser.remotePort = remotePort
ws_anser.SendData "[" & ten & "]: login >"
Case "out":
btn_login.Enabled = True
Me.txt_output_server = "[" & ten & "]" & " logout" & vbCrLf & _
Me.txt_output_server
Case Else:
Me.txt_output_server = "[" & ten & "] :" & msg & vbCrLf & _
Me.txt_output_server
ws_anser.RemoteHost = remoteHostIP
ws_anser.remotePort = remotePort
ws_anser.SendData "[" & ten & "]: " & msg
End Select
End Sub
Option Compare Database
Public WithEvents ws_client As MSWinsockLib.Winsock
Private Sub btn_send_Click()
If Not IsNull(txt_msg) Then
ws_client.RemoteHost = "localhost" 'ở đây để demo nên mình sử dụng localhost
ws_client.remotePort = 1001 ' Xia khẩu súng vào cổng 1001 của server
ws_client.SendData txt_msg ' Bóp cò thôi...
End If
End Sub
Private Sub Form_Load()
Set ws_client = New MSWinsockLib.Winsock
ws_client.Protocol = sckUDPProtocol
ws_client.RemoteHost = "localhost"
ws_client.remotePort = 1001
ws_client.SendData "in"
End Sub
Private Sub Form_Unload(Cancel As Integer)
ws_client.RemoteHost = "localhost"
ws_client.remotePort = 1001
ws_client.SendData "out"
End Sub
Private Sub ws_client_DataArrival(ByVal bytesTotal As Long)
Dim msg As String
ws_client.GetData msg
Me.txt_output = msg & vbCrLf & _
Me.txt_output
End Sub