Article ID: 163999
Article Last Modified on 1/19/2007
Form: TCPForm
---------------------------------
Caption: TCP Form
Command button:
Name: cmdListen
Caption: Listen
Command button:
Name: cmdConnect
Caption: Establish Connection
Command button:
Name: cmdSend
Caption: Send Data
Command button:
Name: cmdRespond
Caption: Respond
Command button:
Name: cmdClose
Caption: Close Connection
Text box:
Name: Text1
Label Caption: Data Received:
Winsock control:
Name: axWinsockListen
Winsock control:
Name: axWinsockClient
Winsock control:
Name: axWinsockServer
Dim wsListen, wsClient, wsServer As Winsock
Private Sub Form_Load()
' Set one server Winsock control and the client Winsock control
' when the form loads.
Set wsListen = Me!axWinsockListen.Object
Set wsClient = Me!axWinsockClient.Object
' Set the protocol for each control.
wsListen.Protocol = sckTCPProtocol
wsClient.Protocol = sckTCPProtocol
' Set the remote host on the client Winsock control. Because
' client and server are the same computer in this example, set
' RemoteHost equal to LocalIP.
wsClient.RemoteHost = wsListen.LocalIP
' Set a local and a remote port for the client.
wsClient.RemotePort = 100
wsClient.LocalPort = 99
' Set a local and a remote port for the server. Note that the
' server RemotePort is the client LocalPort and vice versa.
wsListen.LocalPort = 100
wsListen.RemotePort = 99
End Sub
Private Sub cmdListen_Click()
' Start the server listening for a connection request.
wsListen.Listen
Msgbox "Server is waiting for a connection request."
End Sub
Private Sub cmdConnect_Click()
' The client requests a connection with the server.
wsClient.Connect
Msgbox "Client requested connection with server."
End Sub
Private Sub axWinsockListen_ConnectionRequest(ByVal requestID As _
Long)
' When the server receives a connection request, set the second
' Winsock on the server to accept the request.
Set wsServer = Me!axWinsockServer.Object
wsServer.Protocol = sckTCPProtocol
' Accept the connection request.
wsServer.Accept requestID
Msgbox "Server accepted client connection request."
End Sub
Private Sub axWinsockClient_Connect()
' When the server accepts the connection request, the Connect
' event fires on the client. Display a message indicating success.
MsgBox "Connection Successful!"
End Sub
Private Sub cmdSend_Click()
' After a connection is established, use a command button to send
' data from client to server.
wsClient.SendData "Hello"
End Sub
Private Sub axWinsockServer_DataArrival(ByVal bytesTotal As Long)
Dim strClientMsg As String
' The DataArrival event fires on the server when the client sends
' information. Get the data and display it in a text box.
wsServer.GetData strClientMsg, vbString
Me!Text1.Value = strClientMsg
End Sub
Private Sub cmdRespond_Click()
' Send a message from the server to the client.
wsServer.SendData "Thanks for the message!"
End Sub
Private Sub axWinsockClient_DataArrival(ByVal bytesTotal As Long)
Dim strServerMsg As String
' The DataArrival event fires on the client when the server sends
' information. Get the data and display it in a text box.
wsClient.GetData strServerMsg
Me!Text1.Value = strServerMsg
End Sub
Private Sub cmdClose_Click()
' Close the server connections
wsServer.Close
wsListen.Close
Msgbox "Server connections closed."
End Sub
Private Sub axWinsockClient_Close()
' Close event on client fires after server closes connection.
' Close the client connection and display a message box.
wsClient.Close
MsgBox "Client connections closed. Good-Bye!"
End Sub
Form: UDPForm
--------------------------------
Caption: UDP Form
Command button:
Name: cmdSend
Caption: Send Data
Text box:
Name: Text1
Label Caption: Data Received:
Winsock control:
Name: axWinsockClient
Winsock control:
Name: axWinsockServer
Dim wsClient, wsServer As Winsock
Private Sub Form_Load()
' Set the control objects when the form loads.
Set wsClient = Me!axWinsockClient.Object
Set wsServer = Me!axWinsockServer.Object
' Set the protocol for client and server.
wsClient.Protocol = sckUDPProtocol
wsServer.Protocol = sckUDPProtocol
' Set the host and ports for client and server. Because client
' and server are the same computer in this example, set RemoteHost
' equal to LocalIP.
wsServer.RemoteHost = wsClient.LocalIP
wsServer.RemotePort = 1007
wsClient.Bind 1007
End Sub
Private Sub CmdSend_Click()
' Send a broadcast message from the server.
wsServer.SendData "Hello"
End Sub
Private Sub axWinsockClient_DataArrival(ByVal bytesTotal As Long)
Dim strServerMsg As String
' When a message arrives from the server, display it in a text
' box.
wsClient.GetData strServerMsg, vbString
Me!Text1.Value = strServerMsg
End Sub
Additional query words: ODE
Keywords: kbhowto kbprogramming kbusage KB163999