Search Tools Links Login

Simple Winsock Tutorial


Visual Basic 6, or VB Classic

This is a short and to the point winsock tutorial. Pleae note this only covers tranferring text between two computers. Nothing more.

Original Author: Codeaholic

Code



Winsock Tutorial




WINSOCK TUTORIAL



Ok, heres how to use the Winsock
control included with Visual Basic.


First add a Winsock control to a
blank form, and then add two command buttons to the form.

Double click one of the command buttons and insert the following code:


Private Sub Command1_Click()

Winsock1.Connect "localhost", 3000

End Sub


This code simply tells the Winsock
control to connect to the IP of your computer (if you wanted to connect to another
computer you would substitute localhost for the IP of the computer you wanted
to connect to). This code also sets the port to connect on. Something above
3000 is good because below this there is alot of service port used by windows
eg. netbios

Now double click the other command button and add the following code:


Private Sub Command2_Click()

Winsock1.SendData "hello this is a test"

End Sub



This bit of code is self-explanatory. It sends whatever you tell it.

Right.... we've now coded the client program. Now for the server. Minimize the
project you've just created and start a new one.

Double click on the form and choose the Form_Activate event. Insert the code
like this:


Private Sub Form_Activate()

Winsock1.LocalPort = 3000

Winsock1.Listen

End Sub


This code defines the port to listen
on and sets the Winsock controls state to listen.

Now insert this code:


Private Sub Winsock1_ConnectionRequest(ByVal
requestID As Long)

Winsock1.Close

Winsock1.Accept requestID

End Sub


This code accepts a connection.

Add this code as well:


Private Sub Winsock1_DataArrival(ByVal
bytesTotal As Long)

Dim n As String

Winsock1.GetData n

MsgBox n

End Sub


This code gets the data received
from the other program when you click command2. Please not you must declare
the variable used to store the data you got.
If you do not you will just
get ? Marks.


Now run both of the programs and
click the first command button to connect and the second one to send data to
the other program. The other program should show the message you told the first
program to send.









About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 97 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.