Home Parallel Port Serial Port Serial Info Parallel Info USB .NET Webboard I/O Today API Project Directory

 Hardware Interfacing and TCP/IP interfacing in VB programming.
Thai Page Clcik Here
Programming in Visual Basic to control through network part 2

  After learning about basic internet hardware through network having protocol TCP/IP as help of communication between computers, programming on Windows operation can be convenient for communication with network that is MS Winsock Control that has in Visual Studio. In this section, we will program by using Visual Basic because it's easy, convenient, and fast to understand. 

First of all, open VB, and then create the new Project Standard EXE. Next, we must add Winsock Control by clicking on menu.  

Add Winsock Control

Choosing control named Microsoft Winsock Control 6.0 (for VB6)

Properties Winsock Dialog
Properties Detail
Name The name of control that we can name.
LocalPort Ports that we will set in communication.
Protocal Selection the type of protocol. We use 0-TCPProtocal.
RemoteHost IP Address of computer that use for communication.
RemotePort IP Address of computer that use for communication.
Winsock Procedure
In the part of Windows Code Object -->Winsock, there is Procedure for communication both Server and Client. We will make understanding about the followings;

-Close is the event when stopping or cancel communication of server and client by Function Winsock.Close that we can verify the opposite side whether there is communication, may write the message to warn.

-Connect is the event that client has sending signal back to server, making Procedure of Server work. Similarly, you can use Message to verify.

-ConnectionRequest is the event when client send signal back to server Procedure. This part will be worked together with requestID As Long that is generated in system will not be as before. Server use ID from controller in order to correctly communicate.

-DataArrival this event is occurred when having communication between Server and Client Porcedure work together with the value of bytesTotal As Long it get.

-Error is the event that is error between Server and Client by sending Number As Integer that which is the error number with its details in that event is Description As String. 

- SendProgress will be happened while having sending data. This event will work when sending is complete, then making Event SendComplete.

-SendComplete is the event when having sending to the opposite side completely.


Winsock Properties & Events
Accept (requestID) is agreement between Server and Client in choosing the same ID Control number in order to correctly communicate.

Close is sending signal to cancel communication between them, either Server or Client use Function. Also, making Procedure close of the opposite side work.

Connect is sending signal that there is communication now that it make Procedure of the opposite side work.

Getdata is getting data when the opposite side sends by this command in the part of Procedure DataArrival because it is the event when the opposite side sends data to here.

Listenis manner that verify signal that send that the opposite side respond making request communication.

LocalHostNamethis command will send the name of Computer name of that computer.

    Debug.Print  Winsock1. LocalHostName

LocalIPset and return value the IP Address of Computer name of computer that is communicating.

    Debug.Print  Winsock1.LocalIP 

LocalPort set and return value the name of Computer name of computer that is communicating.

    Debug.Print  Winsock1.LocalPort

RemoteHost set and return value the name of Computer name of computer that is communicating.

   Winsock1.RemoteHost =MyServer

RemoteHostIPset IP Address

    Winsock1. RemoteHostIP =

RemoteHostPort set port number that is used for interaction.

     Winsock1. RemoteHostIP =5000

SocketHandlewill return the way in interaction that can see in the following;

   Debug.Print Winsock1.SocketHandle

Statewill return value of status of Socket while interacting that verify status by these Constant such as sckClosed (value=0) Socket opening, sckOpen (value= 1) Socket opening, or sckError(value = 9) Socket having error, etc.


Basic Winsock Process Connect
From explaining about Winsock control, in this topic, we will begin programming to communicate with protocol TCP/IP that communication must have Server and Client that have 2 programs. Actually, they are the same program, but open 2 windows by setting that which one is Server or Client. 

The event that must be done as Server is verifying signal from Client. In this section will use Event Click of the button named cmdListen. 

Private Sub cmdListen_Click()
    Winsock1.LocalPort = txtPortSvr.Text
End Sub

The second event as Client will respond by sending signal to Server. 

Private Sub cmdConnect_Click()
    Winsock1.RemoteHost ="" 
     Winsock1.RemotePort = "5000" 
'you can set other number, but the both side will be equal.
End Sub

When click the button cmdConnect, Winsock Control of Sever will occur Event Connect that in Event, we will put Message to warn like the following;

Private Sub Winsock1_Connect()
     MsgBox "Got massage", vbExclamation, "Chat by MS Winsock Control 6"
End Sub

Actually, Server with Client can communicate, let's send Data.

Private Sub cmdSend_Click()
     Winsock1.SendData "Hi! How are you ? "
End Sub

Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
     Dim StrValue As String
     Winsock1.GetData StrValue
End Sub

If you want to cancel communication, you use command close. When Server or Client use this command will occur Event Close to Winsock control of that side like we create the button close.

Private Sub cmdClose_Click()
    Winsock1.SendData "ยกเลิกการติดต่อแล้วครับ"

End Sub

Suppose that Server has clicking, Winsock Control of Client will occur Event Close by writing code Message to warn like the following;

Private Sub Winsock1_Close()
    MsgBox "Cancle connection already", vbExclamation, "Chat by MS Winsock     Control 6"
End Sub

So, we can program such as Chat,Survey System, etc. Then we will have the example program with Source code in the next section.


The learning outcome of this article

1. To understand programming for communication with network via the protocol TCP/IP that is the protocol that is used for communication in internet.
2. Can use MS Winsock control 6 for programming, knowing Event,Properties, application, programming to connect via TCP/IP.
3. To apply programming Chat,Interface Hardware,Game via Internet system. 

Data Reference : Visual Basic Programming Guide / http://www.vb-world.net / http://earthweb.com /

Copyright (c) 2000-2008 thaiio.com all rights reserved.  Contact webmaster :webmaster@thaiio.com