Winsock For Beginners by Mahangu Weerasinghe

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 two places.  It requires code in the Form_Load (or any other section), and code in the Winsock1_DataArrival Section

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.StrData 'Tells Winsock to get the data from the Port

MsgBox  SrtData 'Displays the data in a Message Box

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 , Command1_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.

Author's E-mail-vbdude777@email.com

Author's Website-http://mahangu.tripod.com

Add your tutorials to this site Mail Me

 

Google
  Web www.gauravcreations.com

GauravCreations's Store / Home / Register / Awards / Opinions / Downloads / Query

Linux Hosting India | Linux Forum

Copyright © 2001-04 [Gaurav Creations]. All rights reserved