How To Use Winsock for Beginners (Update)
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!
Original Author: Mahangu
Code
Winsock for Introduction This tutorial will show Why I wrote this tutorial I got asked a few questions Getting Started 1)Start VB and choose 2)Now Using the Add 3)Double Click the New Icon Now you will see the control Opening a To Open a Winsock Connection all you need to Paste this Into the Form_Load() '<---- The Code Starts Winsock1.Connect , RemHost, <---- The Code Ends Here RemHost stands for the Remote Example Winsock1.Connect , "127.0.0.1" , Sending Data Using Sending data using Winsock is <---- The Code Starts Here Winsock1.SendData(Data) <---- The Code Ends Here Data stands for the data you Example Winsock1.SendData("Test") Receiving Data Receiving data using Winsock Step1 (Placing Placing this code depends on when you want to start <---- The Code Starts Here Winsock1.LocalPort = Winsock.Listen <---- The Code Ends Here Data stands for the data you Example Winsock1.LocalPort = 1000 'This Winsock.Listen 'This Step 2 (Placing You will need to <---- The Code Starts Here Winsock1.GetData (data) MsgBox (data) 'This <---- The Code Ends Here Example Dim StrData 'This Winsock1.GetData StrData 'Tells MsgBox SrtData Step 3 (Placing You will need to <---- The Code Starts Here Dim RequestID 'Declare If socket.State <> sckClosed Then <---- The Code Ends Here Example Dim RequestID Declare If socket.State <> sckClosed Then 'If Closing a Winsock This is relatively simple. All you have to <---- The Code Starts Here Winsock1.Close 'Closes <---- The Code Ends Here The End Please tell me how I can
Beginners
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.
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.
'Standard EXE'
Components (Right Click on Toolbar) add the Microsoft Winsock Control
that Appeared on the Toolbar
on the form. You can rename the control but in the code I will call it Winsock1.
Winsock Connection
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.
, Command1_Click() or any other Sub
Here ---->
RemotePort,
---->
Host you want to connect to. The RemotePort stands for the Remote Port you want
to connect to.
"100" 'This code example will
connect you to your own computer on Port 100
Winsock
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.
---->
---->
want to send.
'This code will send the data string
"Test"
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.
the code in Form_Load event)
accepting data. The best place to put this code is usually in the Form_Load
event.
---->
PortNumber
---->
want to send.
will set the port number to 1000
will tell Winsock to start listening
the code in Winsock1_DataArrival Section)
place some code in the Winsock1_DataArrival event to tell Winsock what to do
once it receives data.
---->
will show the data in a Message Box
---->
declares the data string (can be place in general declarations too)
Winsock to get the data from the Port and put it in the data string
'Displays the data in a Message Box
the code in Winsock1_Connection Request Section)
place some code in the Winsock1_ConnectionRequest event to tell Winsock what do
when it receives a connection request.
---->
the RequestID String
socket.Close
socket.Accept requestID
End If
---->
the RequestID String
Winsock is not closed
socket.Close 'Then Close the Connetion
socket.Accept requestID Reuquest the ID
End If
Connection
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 Winsock Connection
---->
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.