Welcome to the world of Winsock ;) This project will not teach you how to make a chat program; rather, it will teach you the basics behind the mswinsock.ocx in hopes that you will learn as much as you can from the definitions and the events themselves. The project that goes with this is simple and small. It merely teaches you how to get the IP address of your computer with one small line of code, but the definitions and explanations of the control itself should help you get started in the world of internet programming.
Winsock's Properties
The first thing you should understand are the properties of the control. Understanding what each little part does will help you to move on to much greater things in programming.
LocalPort: This is the port which your computer uses. You should set this port to a four digit number (IE: 1234). This is where your computer accesses information that it receives through the winsock.
RemotePort: This is the port from which your computer receives the data. In other words, the other computer you "talk" to sends the data from this port. The two ports transfer data between each other.
Protocol: There are two types of protocols which you may choose from. UDP protocol is slower and tends to lose "packets" of data transmitted, but easier to program in. TCP is much faster and doesn't lose as much of the packets as UDP does, but the downside of this protocol is that it is very difficult to program in unless you know what you're doing, or have a firm grasp on the operation of internet utilities.
RemoteHost: The remote host is the IP address of the computer which you are connecting to. An IP address is a special number which identifies your computer when it logs onto the internet. It is a series of four sets of numbers that can be between 1 and 255 and they are divided by periods. An example would be: 111.222.121.212
LocalIP: This is your IP address.
Events
DataArrival: This is where you put the source code which you want to occur when data arrives through your local port.
Connect: This is what occurs once the user connects to the other computer.
SendProgress: This associates mostly with file transfers. You indicate here what you want the control to do while it is in the process of sending the information/data.
SendComplete: Here you would indicate what you wanted to happen after the sending of data was completed. A good example is how some programs give you a message box or play a .wav file when a file finishes transferring.
Close: When you close the connection, you place the source code here telling what you want to be done at the termination of a connection. The best way of using this is to give a message box that says the connection is terminated.
SendData: This tells the winsock control to send the data. An example of this would be:
Winsock1.SendData "Blah and more Blah!"
GetData: This tells the winsock control to get data that is being sent to it through the remoteport.
Making the IP Grabber
Now that we have the properties and events definitions out of the way, why don't we try to learn how to grab the IP address of your computer while using the mswinsock.ocx control.
The first thing you need to do is to open up a new project and name it IP.vbp. Go to the components section and select: Microsoft Winsock Common Control 5.0 and add it to your project. Drag out a copy of the control onto your form and make sure the protocol is set for TCP (actually either way it doesn't matter, but TCP is less buggy and doesn't lose as many packets as mentioned earlier).
Next make a textbox and a command button. Make sure that in the properties for the textbox that you lock the box (this means that the user cannot alter any information in the box manually). Next, go to your command button and put in the following source code:
Text1.Text = Winsock1.LocalIP
What this does is it "grabs" the localIP of your computer (so long as it is logged onto the internet) and it places it into the textbox indicated. Make sure you are logged onto the internet when you run this program. Otherwise it will cause VB to crash ;)
If you had any problems programming this application or understanding anything within the tutorial, please feel free to download the project file from the Tutorial Source page!