About Winsock in Visual Basic.
Posted: Thursday, August 04, 2005
by Keral
BlueSoftWeb
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.
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.
how do u send an array through winsock??
yes, very interesting ,now i am able to work in winsock
This tutorial sucks, it is unclear
Keral I purchase a script from you but heard nothing? It has been 2 days I am getting impatient - DomainControl ring a bell ?