This tutorial shows newbies to VB, basically everything they need to know about Winsock. It shows how to open and close a Winsock connection and also how to send and receive data via a Winsock connection. Very easy to understand and use. Includes sample code that can be used in your applications! A few bugs have been fixed in this update so try it out!
Submitted On | 2000-11-26 04:39:52 |
By | Mahangu |
Level | Beginner |
User Rating | 4.2 (38 globes from 9 users) |
Compatibility | VB 5.0, VB 6.0 |
Category | Internet/ HTML |
World | Visual Basic |
Archive File | CODE_UPLOAD1206111252000.zip |
Winsock for Beginners
Introduction
This tutorial will show newcomers to Visual Basic how to use the Winsock ActiveX Control to transfer data across the internet. This tutorial show beginners how to start a Winsock connection, how to send data across a Winsock connection, how to receive data using a Winsock Connection and how to close a Winsock connection.
Why I wrote this tutorial
I got asked a few questions on Winsock so I decided to write a tutorial that would describe the very basics of using Winsock. Also I thought that it would help new coders who were trying to send data over the net.
Getting Started
1)Start VB and choose 'Standard EXE'
2)Now Using the Add Components (Right Click on Toolbar) add the Microsoft Winsock Control
3)Double Click the New Icon that Appeared on the Toolbar
Now you will see the control on the form. You can rename the control but in the code I will call it Winsock1.
Opening a Winsock Connection
To Open a Winsock Connection all you need to do is to type Winsock1.Connect . But there are two values you have to give for the code to work. Remote Host and Remote Port.
Paste this Into the Form_Load() , Command1_Click() or any other Sub
'<---- The Code Starts Here ---->
Winsock1.Connect , RemHost, RemotePort,
<---- The Code Ends Here ---->
RemHost stands for the Remote Host you want to connect to. The RemotePort stands for the Remote Port you want to connect to.
Example
Winsock1.Connect , "127.0.0.1" , "100" 'This code example will connect you to your own computer on Port 100
Sending Data Using Winsock
Sending data using Winsock is also relatively simple. Just use Winsock1.SendData . But this too requires a value to be given. In plain English - It has to to know what data to send.
<---- The Code Starts Here ---->
Winsock1.SendData(Data)
<---- The Code Ends Here ---->
Data stands for the data you want to send.
Example
Winsock1.SendData("Test") 'This code will send the data string "Test"
Receiving Data Using Winsock
Receiving data using Winsock is relatively more complex than the methods mentioned above. It requires code in three places. It requires code in the Form_Load (or any other section), code in the Winsock1_DataArrival Section , and code in the Winsock_ConnectionRequest event.
Step1 (Placing the code in Form_Load event)
Placing this code depends on when you want to start accepting data. The best place to put this code is usually in the Form_Load event.
<---- The Code Starts Here ---->
Winsock1.LocalPort = PortNumber
Winsock.Listen
<---- The Code Ends Here ---->
Data stands for the data you want to send.
Example
Winsock1.LocalPort = 1000 'This will set the port number to 1000
Winsock.Listen 'This will tell Winsock to start listening
Step 2 (Placing the code in Winsock1_DataArrival Section)
You will need to place some code in the Winsock1_DataArrival event to tell Winsock what to do once it receives data.
<---- The Code Starts Here ---->
Winsock1.GetData (data)
MsgBox (data) 'This will show the data in a Message Box
<---- The Code Ends Here ---->
Example
Dim StrData 'This declares the data string (can be place in general declarations too)
Winsock1.GetData StrData 'Tells Winsock to get the data from the Port and put it in the data string
MsgBox SrtData 'Displays the data in a Message Box
Step 3 (Placing the code in Winsock1_Connection Request Section)
You will need to place some code in the Winsock1_ConnectionRequest event to tell Winsock what do when it receives a connection request.
<---- The Code Starts Here ---->
Dim RequestID 'Declare the RequestID String
If socket.State <> sckClosed Then
socket.Close
socket.Accept requestID
End If
<---- The Code Ends Here ---->
Example
Dim RequestID Declare the RequestID String
If socket.State <> sckClosed Then 'If
Winsock is not closed
socket.Close 'Then Close the Connetion
socket.Accept requestID Reuquest the ID
End If
Closing a Winsock Connection
This is relatively simple. All you have to do is to type one line of code. This can be place in almost any event on the form including Form_Unload , Comman1_Click and so on.
<---- The Code Starts Here ---->
Winsock1.Close 'Closes the Winsock Connection
<---- The Code Ends Here ---->
The End
Please tell me how I can improve this tutorial. If you have any questions or comments please post them here and I will reply to them as soon as I can.