Simple Winsock Tutorial
![Visual Basic 6, or VB Classic Visual Basic 6, or VB Classic](https://www.fortypoundhead.com//assets/img/icons/v7/vb6.png)
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 Ok, heres how to use the Winsock First add a Winsock control to a Private Sub Command1_Click() This code simply tells the Winsock Private Sub Command2_Click() Private Sub Form_Activate() This code defines the port to listen Private Sub Winsock1_ConnectionRequest(ByVal This code accepts a connection. Private Sub Winsock1_DataArrival(ByVal This code gets the data received Now run both of the programs and
control included with Visual Basic.
blank form, and then add two command buttons to the form.
Double click one of the command buttons and insert the following code:
Winsock1.Connect "localhost", 3000
End Sub
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:
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:
Winsock1.LocalPort = 3000
Winsock1.Listen
End Sub
on and sets the Winsock controls state to listen.
Now insert this code:
requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub
Add this code as well:
bytesTotal As Long)
Dim n As String
Winsock1.GetData n
MsgBox n
End Sub
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.
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.
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.