WINSOCK VISUAL BASIC
Winsock in Visual Basic allows programmers to send and receive messages. It send and receives messages on a specific port and IP Address. The following article will explain how to use Winsock in VB and how to build some basic programs using it.
To start using the Winsock control in Visual Basic one must first add the Winsock control to it. To do that right click on the Tool Box and click components.
After opening the Components dialog box you will have to choose the component called "Microsoft Winsock Control 6." under the "Controls" tab and then click on "OK". Once you have done this you will see a new icon in the Tool Box that has two computers connected to each other. Now choose this Tool and make its object on the form. Once that is done you can edit its propertied such as -
1.Name
2.Local Port
3.Protocol
4.Remote Host
5.Remote Port
As we already know that every component in Visual Basic 6.0 has certain properties. Similarly, the Winsock object too has certain properties that are explained below.
This is the name that you give to that particular object in VB. The default name for this component is Winsock. But you can change the name however you want to suit your taste. Say, if you are using one Winsock to connect to internet and the other to Yahoo Messenger, you can name them internetWin and YmsgWin respectively. But, that is not a rule; you can name them in any way you want.
Here you specify the port on which the Winsock object must listen to. Say for example Internet Explorer or any other web Browser communicates over port number 80 for messages and responses coming from websites. If you want to tap into MSN Messenger's messages you would use port number 1863.
This property allows you to decide whether you want to use the TCP/IP protocol or the UDP protocol.
TCP/IP > 0 - sckTCPProtcol
UDP > 1 - sckUDPProtocol
Here you give the IP address of the computer that you want send messages. It may not necessarily be a computer it can also be a server. Say for example the IP address of Wikipedia is 207.142.131.202.
Here you will be specifing the port on which the Remote Host should receive the message. Say for example all web servers reply mostly only to port number 80, MSN Messenger only replies on port number 1863.
This part will explain how to listen on a particular port on a network using winsock.
Doing this is a very basic task and does not take more than 5 minutes. First you will have to specify a port on which you want you Winsock control to listen. Let us say the name of your winsock control is "Wiki". You will specify "Wiki" the Local Port and tell it to listen.
Private Sub Form_Load ()
'Set the socket's port
Wiki.LocalPort="10010"
'Listen on this port
Wiki.Listen
End Sub
This tells you how to simply connect to a computer and put a Message Box saying if it has or if an error has occurred.
To simply connect to a computer you must know that computer's IP address and the port on which you want to receive its messages. Once it is connected it will display that it has connected or it will show an error telling why it wasn't able to connect. Here to we will be naming the winsock control as "Wiki".
Private Sub Form_Load()
Wiki.RemoteHost = "127.0.0.1"
Wiki.RemotePort = "10010"
Wiki.Connect
End Sub
Private Sub Wiki_Connect()
MsgBox "Connected..."
End Sub
Private Sub Wiki_Error (ByVal Number as Integer, Description As String, _ ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, _ ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox "Error:"&Description
End Sub
''This article is still under development...''
'Simple chat client:'
Private Sub Form_Load()
Wiki.RemoteHost = "127.0.0.1"
Wiki.RemotePort = "10010"
Wiki.Connect
End Sub
Change the remotehost to the specified computer, next create a command button and two text boxes,
one for the chat and another to say.
Name the first textbox to txtChat, the second to txtSay and the commandbutton to cmdSend.
Make sure multi-line is set to true from the text box properties!
Next copy the following code:
Private Sub cmdSend_Click()
Wiki.SendData (txtSay.Text) ' Sends the text
txtChat.Text = txtChat.Text & vbNewLine & txtSay.Text ' Shows us what we sent
txtSay.Text = "" ' Clears the textbox
End Sub
Private Sub Wiki_DataArrival(ByVal bytesTotal As Long)
Dim Chat As String ' Declare the string
Wiki.GetData Chat ' Gets the data
txtChat.Text = txtChat.Text & vbNewLine & Chat ' Shows the received text (data)
End Sub
Thats only for the client, for the server you just need to make it listen for connections and accept if any is coming.
| Contents |
| Getting Started |
| Properties |
| Name |
| Local Port |
| Protocol |
| Remote Host |
| Remote Port |
| Listening on a Port |
| Explanation |
| Coding |
| How to Connect to a Computer |
| Explanation |
| Coding |
Getting Started
To start using the Winsock control in Visual Basic one must first add the Winsock control to it. To do that right click on the Tool Box and click components.
After opening the Components dialog box you will have to choose the component called "Microsoft Winsock Control 6." under the "Controls" tab and then click on "OK". Once you have done this you will see a new icon in the Tool Box that has two computers connected to each other. Now choose this Tool and make its object on the form. Once that is done you can edit its propertied such as -
1.Name
2.Local Port
3.Protocol
4.Remote Host
5.Remote Port
Properties
As we already know that every component in Visual Basic 6.0 has certain properties. Similarly, the Winsock object too has certain properties that are explained below.
Name
This is the name that you give to that particular object in VB. The default name for this component is Winsock. But you can change the name however you want to suit your taste. Say, if you are using one Winsock to connect to internet and the other to Yahoo Messenger, you can name them internetWin and YmsgWin respectively. But, that is not a rule; you can name them in any way you want.
Local Port
Here you specify the port on which the Winsock object must listen to. Say for example Internet Explorer or any other web Browser communicates over port number 80 for messages and responses coming from websites. If you want to tap into MSN Messenger's messages you would use port number 1863.
Protocol
This property allows you to decide whether you want to use the TCP/IP protocol or the UDP protocol.
TCP/IP > 0 - sckTCPProtcol
UDP > 1 - sckUDPProtocol
Remote Host
Here you give the IP address of the computer that you want send messages. It may not necessarily be a computer it can also be a server. Say for example the IP address of Wikipedia is 207.142.131.202.
Remote Port
Here you will be specifing the port on which the Remote Host should receive the message. Say for example all web servers reply mostly only to port number 80, MSN Messenger only replies on port number 1863.
Listening on a Port
This part will explain how to listen on a particular port on a network using winsock.
Explanation
Doing this is a very basic task and does not take more than 5 minutes. First you will have to specify a port on which you want you Winsock control to listen. Let us say the name of your winsock control is "Wiki". You will specify "Wiki" the Local Port and tell it to listen.
Coding
Private Sub Form_Load ()
'Set the socket's port
Wiki.LocalPort="10010"
'Listen on this port
Wiki.Listen
End Sub
How to Connect to a Computer
This tells you how to simply connect to a computer and put a Message Box saying if it has or if an error has occurred.
Explanation
To simply connect to a computer you must know that computer's IP address and the port on which you want to receive its messages. Once it is connected it will display that it has connected or it will show an error telling why it wasn't able to connect. Here to we will be naming the winsock control as "Wiki".
Coding
Private Sub Form_Load()
Wiki.RemoteHost = "127.0.0.1"
Wiki.RemotePort = "10010"
Wiki.Connect
End Sub
Private Sub Wiki_Connect()
MsgBox "Connected..."
End Sub
Private Sub Wiki_Error (ByVal Number as Integer, Description As String, _ ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, _ ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox "Error:"&Description
End Sub
''This article is still under development...''
'Simple chat client:'
Private Sub Form_Load()
Wiki.RemoteHost = "127.0.0.1"
Wiki.RemotePort = "10010"
Wiki.Connect
End Sub
Change the remotehost to the specified computer, next create a command button and two text boxes,
one for the chat and another to say.
Name the first textbox to txtChat, the second to txtSay and the commandbutton to cmdSend.
Make sure multi-line is set to true from the text box properties!
Next copy the following code:
Private Sub cmdSend_Click()
Wiki.SendData (txtSay.Text) ' Sends the text
txtChat.Text = txtChat.Text & vbNewLine & txtSay.Text ' Shows us what we sent
txtSay.Text = "" ' Clears the textbox
End Sub
Private Sub Wiki_DataArrival(ByVal bytesTotal As Long)
Dim Chat As String ' Declare the string
Wiki.GetData Chat ' Gets the data
txtChat.Text = txtChat.Text & vbNewLine & Chat ' Shows the received text (data)
End Sub
Thats only for the client, for the server you just need to make it listen for connections and accept if any is coming.
This article provided by Wikipedia. To edit the contents of this article, click here for original source.
psst.. try this: add to faves

العربية
中国
Français
Deutsch
Ελληνική
हिन्दी
Italiano
日本語
Português
Русский
Español