Make your own free website on Tripod.com
         

     This Month's Hot Topic!!!

Winsock Control - This is another great control which makes programming so much fun and easy with Visual Basic. It can be used from enabling simple communications between applications over the LAN to replacing IIS on Your machine! 

 

Microsoft® Winsock Control can be used to enable applications to communicate over the network. It makes it possible using either TCP or UDP protocol, and sending and receiving data over a specific communication port. Depending on the protocol it is set to use, there are two types of applications you can build with this control: "server-client" (TCP) and "client-client" (UDP) applications. If You use TCP protocol, then one application with Winsock control has to act as a server and listen to a specific port (which You set by Yourself), and another application has to act as a client, requesting for a connection on that port. Server side intercepts the request by a client and accepts it and then the data between two machines can be sent and received. So to use Winsock with TCP:

  • Server listens to the port (winsockServer.Listen)

  • Client connects to the server (winsockClient.Connect)

  • Server closes the connection (winsockServer.Close) and accepts the connection from client (winsockServer.Accept (requestID))

  • Then, both, server and client can send and receive information (winsock.SendData, winsock.GetData)

UDP is very similar, except now instead of listening to one port, each machine has its own port and it binds to other machine's port (winsock.Bind). You might be confused on how exactly to do all this, so let's look at the example.

We will build simple chat program using TCP. First, open two instances of Visual Basic. In the first instance, which we will call "server" side, place Winsock control (in this example its name will be wsServer), set Winsock Protocol property to TCP, add two textboxes (txtSend and txtGet) and command button (cmdSend). In Form_Load() procedure write this:

       wsServer.LocalPort=1001 ' you choose the number you want to use
       wsServer.Listen               ' listen to the port 1001 for connection request

These two lines make Winsock control listen to the port You specify. Now imagine client requested the connection (we will see how client does that). To intercept the connection, you need to code wsServer_ConnectionRequest procedure:

       Private Sub wsServer_ConnectionRequest(ByVal requestID As Long)
               If wsServer.State<>sckClosed Then wsServer.Close
               ' connection has to be closed to intercept the request, so it will be closed if open

               wsServer.Accept RequestID ' server accepts the request of the client, the connection is                                                           established!

       End Sub

Now, the server knows where the client is, and the client knows where the server is, they are both connected and can send and receive information forth and back! Server receives information in the  wsServer_DataArrival procedure:

        Private Sub ws_DataArrival(ByVal bytesTotal As Long)

           Dim dat As String

           wsServer.GetData dat, vbString ' gets the data and treats it as it would be string. (Other                                                          data types are also available)
           txtGet.Text=dat' shows the data in the text box

       End Sub

Data can also be sent to the client, in this program, when user clicks on cmdSend button. To send data use wsServer.SendData txtSend.Text method.

Now switch to "client" instance of Visual Basic. Place the same controls you did when you made "server" appliaction, except now the name of winsock control is "wsClient" and additional command button cmdConnect is needed to establish the connection. Add this code to the Form_Load procedure of the client:

         wsClient.RemotePort=1001 ' the number of server's local port
         wsClient.RemoteHost=name ' the name of the server's computer

To connect to the server, add wsClient.Connect to the cmdConnect_Click() event. So the moment the user will click on cmdConnect, the request will be sent to the remote computer (in this case "server" instance) through the socket 1001 to the socket 1001 asking for the connection. Connection will be granted, (if not, enter the code to display any errors in wsClient_Error()) procedure. To send and receive information on the "client", add the same code You did when You added code to send and receive information on the "server", to your "client" code. After you inserted the code for sending information and receiving on "client" side, run both instances of Visual Basic. Press connect in "client" to connect, and then type any message and send it to "server" instance. This message should appear on "server" instance in txtGet textbox. For the application to work properly, the "client" has to connect first, and only after that the communications between client and server can be started.

This is the basics of Winsock control. What was written here, is enough to create simple chat program over Your local area network. After You gain more experience with Winsock and how it works, you can even start building Your own internet server! The topic on this is not included yet, but it should come out soon! Good luck with VB! 

Topics - Home

Made By Laimonas Simutis. 2001. laijerrad@yahoo.com