Writers' Community!
Home Page Two Columnists Q&A Submit an Article FAQs Contact Author Login
Article Submission
We Need YOUR Articles!
We'll Promote Them for FREE!

Author Login

New Authors
Register Here


Now Serving 8,200 Authors
71,981 Quality Articles
& 7,110 Current Users Online!
Featured Authors
Joel Hirschhorn (2,847)
Connor Davidson (5,541)
Mark Parsec (16,631)
Julian Price (12,254)
Michael Ramzy (821)
Edward Rhymes (9,204)
Dianne Lehmann (5,838)
Fran Larson (20,012)
Gregory Lewis (1,456)
Ira Coffin (13,580)
Joel Hendon (18,567)
Sandra E. Graham (9,984)
Shari Vaudo (1,123)
Steve Kovacs (4,352)

View All Featured Authors
Most Recent
Was Ares I-X designed by NASAs own CAD-system?

How To Decide A Niche For Your Membership Software?

How to Remove the Vocals from a Song

Jazz Up and Organize Your Inbox with Color Categories

Preventing Malware While Browsing The Internet

Should A Cad Come With The Operation System?

How To Setup An Email Out of Office Auto Reply In Mircosoft Outlook

How can I open a 2007 Microsoft Office file in an earlier version of Office?

What are the Key Requirements for Privacy Control Software?

Wny Do You Need Open Source Audits

Home » Categories » Computers & Networking » Software » About Winsock in Visual Basic. » Printer Friendly

About Winsock in Visual Basic.

Rated 3 out of 5
No Reader Ratings Available ?
Rate It  /  View Comments  /  View All Articles submitted by Keral
Submitted Thursday, August 04, 2005
Keral (204)
BlueSoftWeb
Log in to become a member of Keral's Fan Club!


Hello Everybody, This Winsock Tutorial is for anyone who has not heard of winsock or have never programmed with winsock control. First of all I would like to tell you that there are two type of protocols in winsock control through which we can have a successful connection. They are TCP and UDP But here we will only discuss TCP. UDP is also Great But generally TCP Protocol is Used. Now Lets Start....

Designing Part:-

First of all add winscok control to a Standard exe project named 'Client'. Now Place that Winsock Control on the form. It is invisible at runtime so its location is not important. Place Two Text-Boxes named txtIP and txtSend also place Command Buttons named cmdConnect and cmdSend on this Form and in Last Place a List-Box control names 'lstMessages' on the Form. Set Text-Boxes' Text property to "" and cmdConnect and cmdSend's Caption Property to "Connect" and "OK" respectively. Rename our Form to 'frmClient'. Set cmdSend's Default Property to True. We will let the Default name for the Winsock Control as this is the Winsock Tutorial.

Open another Standard exe project in another window. All the Controls would be same as Client Project except txtIP and cmdConnect they both are not needed here. Name this Project as 'Server' and its Form as 'frmServer'.

Now the Coding Part for the Client Project. Write the Following Code into Code Window:-

Private Declare Function SendMessage Lib
"user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Declare Function ReleaseCapture Lib
"user32" () As Long

Private Sub cmdConnect_Click()

On Error Resume Next

Winsock1.Connect txtIP.Text, "1412" 'Just remember this Port Number Should be Same on which our Server is Listening

End Sub

Private Sub cmdSend_Click()

On Error Resume Next

Winsock1.SendData "Client:- " & txtSend.Text

lstMessages.AddItem "Client:- " & txtSend.Text

txtSend.Text = ""

txtSend.SetFocus

End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

'For making the Form Movable

ReleaseCapture

SendMessage Me.hwnd, &HA1, 2, 0&

End Sub

Private Sub Label1_Click()

On Error Resume Next

'Letting server know that client has Disconnected.

Winsock1.SendData "Client is Disconnected!"

DoEvents

Unload Me

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

On Error Resume Next

Dim str As String

Winsock1.GetData str

lstMessages.AddItem str

End Sub

And The Following Code into The Server project. It is Much Same as The Client Part Except that we have to Set Winsock Control to listen on specific Port on the Form's Load Event.

Private Declare Function SendMessage Lib
"user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Declare Function ReleaseCapture Lib
"user32" () As Long

Private Sub cmdSend_Click()

On Error Resume Next

'This data will be sent to the Client

Winsock1.SendData "Server:- " & txtSend.Text

lstMessages.AddItem "Server:- " & txtSend.Text

txtSend.Text = ""

txtSend.SetFocus

End Sub

Private Sub Form_Load()

On Error Resume Next

'If one Copy of Our Application is already running then don't load a new one

If Not App.PrevInstance = True Then

Winsock1.LocalPort = 1412 'This can be any Valid Port Number

'Wait for Clients to Connect with Your Server.

Winsock1.Listen

End If

End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

'for making a form Movable

ReleaseCapture

SendMessage Me.hwnd, &HA1, 2, 0&

End Sub

Private Sub Label1_Click()

On Error Resume Next

'So that it will not raise an error after sending the data to the server which is already disconnected

Winsock1.SendData "Server is Disconnected!"

'Here DoEvents gives time to perform the winsock operation before unloading it from memory

DoEvents

'Now Unload it

Unload Me

End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)

On Error Resume Next

'First Check if the Winsock Control is Connected or not If connected then Close it

If Winsock1.State <> sckClosed Then Winsock1.Close

'Now accept the Request

Winsock1.Accept requestID

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

On Error Resume Next

Dim str As String

'Now we will store data that has came into this string

Winsock1.GetData str

'And Display that data in the listbox

lstMessages.AddItem str

End Sub

That's It Bye Until Next tutorial In which we will see about the ByteArrays() and UDP Protocol. You can Download the Demo for Both of these Project to Study it and Please Note that if You are testing it on a Stand-alone Computer then Let the IP Address Be "127.0.0.1". Yeah, You can change the Port Number but you will have to change it in Both the Projects. They Both have to be Same for Winsock to Communicate.

If you have any further questions then log on to http://www.bluesoftweb.com  and contact me.



Reprint Rights

Log in to become a member of Keral's Fan Club!

Comments on this article:


» left by Snehal from India (4 years 85 days ago.)
Reader Rating: 5 out of 5
I have seen this someplace else also. But I read it evertime i see this tutorial. Thanks a lot and write some more of this so that the community can benefit from this.
Respond to this comment

» left by jax from aust (3 years 148 days ago.)
Reader Rating: 5 out of 5
how do u send an array through winsock??
Respond to this comment

» left by mukund from india (3 years 121 days ago.)
Reader Rating: 5 out of 5
yes, very interesting ,now i am able to work in winsock
Respond to this comment

» left by Anonymous (3 years 104 days ago.)
Reader Rating: 1 out of 5
This tutorial sucks, it is unclear
Respond to this comment

» left by Anthony from Wageningen, Netherlands (2 years 10 days ago.)
Reader Rating: 1 out of 5
Keral
I purchase a script from you but heard nothing? It has been 2 days I am getting impatient - DomainControl ring a bell ?

Respond to this comment

Was this article helpful to you? Leave a Public Comment or Question:

This Article has been viewed 3,750 times.
Article added to SearchWarp.com on 8/4/2005 1:25:04 AM.
View other articles written by Keral (204)
Subscribe to 'BlueDoftWeb.Com News'


If you found this article interesting, you may want to check out:

Disclaimer:  All information on this site is provided for informational purposes only! By no means is any information presented herein intended to substitute for the advice provided to you by any health care or other professional or organization.


Today's Most Popular
Improve PC Performance - 6 Tips You Must Know.

Defect Classification In Software Testing

Recover Deleted Email Messages in Vista Windows Mail

Linux Commands Tutorials - Using the ls Command with Examples of Options - A Hands-On Tutorial Help

What Shows Up On a Criminal Record Background Check?

About Winsock in Visual Basic.

Internet Explorer 7 (IE7) As a Ftp Client-Does Not Work

Windows Vista and Internet Explorer 7 Delete View Objects Bug

How to Change Folder Icon or Folder Color?

What is Software Piracy?

Viewed from Cache. Load Time: 0.000.

Home  |  Page Two  |  FAQ's  |  Contact  |  Terms of Service  |  Article Submission Guidelines  |  Questions & Answers  |  Privacy  |  Mission / About
Copyright © 1999-2009 SearchWarp.com, All Rights Reserved - SearchWarp.com is an IcoLogic, Inc. Company