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 3

    The fundamental of programming by Winsock Control 6 from the part 2 must be written programming to get and send message, called chat. The main of program can be both Server and Client in the same program. Then open VB and create New Project, then Add Winsock Control. 

 
Add Winsock Control


From the blank form of us, so now, we must Add Winsock Control to Form by dragging Tool Dialog Box to paste, then following by other Control like below.

 

Number Control  Name Activity
1 Command Button cmdDisconnect Cancel to communicate
2 Command Button cmdlisten Send signal to verify
3 Command Button cmdconnect Connect communication
4 Command Button cmdSend Send message
5 Option Button OptServer Chose as Server
6 Option Button OptClient Chose as Client
7 TextBox txtName Show the name communicating
8 TextBox txtPortSvr Put the value of Port of Server
9 TextBox txtIP Put the value of IP Address
10 TextBox txtPortClt Put the value of Port of Client
11 TextBox TxtInput Put message to send the side communicating
12 TextBox TxtMassage Show message while communicating
13 TextBox txtMonitor Show message of status of communication
Command Button Code
It is the code for cancel communication when clicking button.

Private Sub cmdDisconnect_Click()
If OptServer.Enabled = True Then
   Frmserver.Enabled = True
   FrmClient.Enabled = False
   OptServer.Enabled = True
   OptClient.Enabled = True
   Else
   Frmserver.Enabled = False
   FrmClient.Enabled = True
   OptServer.Enabled = True
   OptClient.Enabled = True
End If
If Winsock1.State <> sckClosed Then  ' Verify the status of Socket if it is not equal to zero
   On Error GoTo ErrLabel:   ' If there is some error, let's do at ErrLabel
   Winsock1.SendData "Cancle connection already"   'send message back
   Winsock1.Close   ' command Winsock to stop communicating
End If
Exit Sub
ErrLabel:  

' There will be showing error messages and their number that will show at TextBox , named txtMonitor
txtMonitor.Text = txtMonitor.Text & Err.Description & " - Error number: " & Err.Number & vbCrLf  
txtMonitor.SelStart = Len(txtMonitor.Text)
End Sub

Listen button will send signal to verify Client that are there any respond communication.

Private Sub cmdlisten_Click()
    On Error GoTo ErrLabel:  '  If there is some error, let's do at ErrLabel  
    Winsock1.LocalPort = txtPortSvr.Text 'Set the value of Port of Socket that is used for communication.
    Winsock1.Listen  ' send signal to verify that are there any respond.
    TxtInput.SetFocus
    Frmserver.Enabled = False
    FrmClient.Enabled = False
    OptServer.Enabled = False
    OptClient.Enabled = False
    Exit Sub
ErrLabel:
txtMonitor.Text = txtMonitor.Text & Err.Description & " - Error number: " & Err.Number & vbCrLf
txtMonitor.SelStart = Len(txtMonitor.Text)
End Sub

When Server send Listen signal to Client , answering by press the Connect button in order to send message. 

Private Sub cmdconnect_Click()
    On Error GoTo ErrLabel:
    Winsock1.RemoteHost = txtIP.Text        'Set IP Address to Winsock
    Winsock1.RemotePort = txtPortClt.Text    'Set Port to Winsock
    Winsock1.Connect     ' 'Connecting to Server
    TxtInput.SetFocus
    Frmserver.Enabled = False
    FrmClient.Enabled = False
    OptServer.Enabled = False
    OptClient.Enabled = False
    Exit Sub
ErrLabel:
txtMonitor.Text = txtMonitor.Text & Err.Description & " - Error number: " & Err.Number & vbCrLf
txtMonitor.SelStart = Len(txtMonitor.Text)
End Sub

After connecting between them, after this we can send message by put message at TextBox named TxtInput that when push the button send, the message will send to the opposite side.

Private Sub cmdSend_Click()
If TxtInput.Text <> "" Then   ' If there are not the blank message will do this condition.
    On Error GoTo ErrLabel:   
    Winsock1.SendData TxtInput.Text    ' Send message that is in TextBox
    TxtMassage.Text = TxtMassage.Text & "Me: " & TxtInput.Text & vbCrLf
    TxtInput.Text = ""
    TxtMassage.SelStart = Len(TxtMassage.Text)  
End If
Exit Sub
ErrLabel:
txtMonitor.Text = txtMonitor.Text & Err.Description & " - Error number: " & Err.Number & vbCrLf
txtMonitor.SelStart = Len(txtMonitor.Text)
End Sub

 

Winsock Procedure Code
When the side that communicates by pushing the button close to cancel communicating, this procedure will work.

Private Sub Winsock1_Close()
MsgBox "cancel communication", vbExclamation, "Chat by Winsock Control 6"
End Sub

This event will occur when Server has sending signal to verify when press the button Listen.

Private Sub Winsock1_Connect()
On Error GoTo ErrLabel:
MsgBox "respond communication", vbExclamation, "Chat by MS Winsock Control 6"
Winsock1.SendData "Hello, how are you?" ' send message back to answer
Exit Sub
ErrLabel:
txtMonitor.Text = txtMonitor.Text & Err.Description & " - Error number: " & Err.Number & vbCrLf
txtMonitor.SelStart = Len(txtMonitor.Text)
End Sub

During communication, procedure will verify request in order to accept requested and use ID to correct communication.

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
On Error GoTo ErrLabel:
If Winsock1.State <> sckClosed Then Winsock1.Close
   Winsock1.Accept requestID   ' verify and accept value of control
   Exit Sub 
ErrLabel:
txtMonitor.Text = txtMonitor.Text & Err.Description & " - Error number: " & Err.Number & vbCrLf
txtMonitor.SelStart = Len(txtMonitor.Text)
End Sub

When having message that is sent to event DataArrival, it will work by we take that value to show in TextBox by the command GetData . 

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
If Me.WindowState = 1 Then Me.WindowState = 0
   Dim StrValue As String
   On Error GoTo ErrLabel:
   Winsock1.GetData StrValue ' from message that opposite side send, will keep in variable named StrValue จากนั้นจะนำไปแสดงที่ TxtBox
   TxtMassage.Text = TxtMassage.Text & txtName.Text & ": " & StrValue & vbCrLf
   TxtMassage.SelStart = Len(TxtMassage.Text)
   Exit Sub
ErrLabel:
txtMonitor.Text = txtMonitor.Text & Err.Description & " - Error number: " & Err.Number & vbCrLf
txtMonitor.SelStart = Len(txtMonitor.Text)
End Sub

During communication, let it happen error, in order to realize where error, which should be solved is. That event will work together with that error. 

Private Sub Winsock1_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)

txtMonitor.Text = txtMonitor.Text & Err.Description & " - Error number: " & Err.Number & vbCrLf
txtMonitor.SelStart = Len(txtMonitor.Text)

End Sub

All I write, you will notice that in the part of showing message of status during communication has every procedure that is showing message and number of error.

 

Option Code
It's necessary for you to program only one program, but can be Server and Client, for coding is just Enabled Frame in the part of Server or Client.

Private Sub OptClient_Click()
   FrmClient.Enabled = True
   Frmserver.Enabled = False
   txtIP.SetFocus
End Sub

Private Sub OptServer_Click()
   Frmserver.Enabled = True
   FrmClient.Enabled = False
   txtPortSvr.SetFocus
   txtPortSvr.SelStart = 0
   txtPortSvr.SelLength = Len(txtPortSvr.Text)
End Sub

 

Menu Code

 In this part, I will explain just menu tool that has IP Address Local for verify IP address of computer.

Private Sub mnuChkIP_Click()
MsgBox "This comouter has IP Address : " & Winsock1.LocalIP, vbInformation, "Local IP Address"
End Sub

 Also, Port Local for see IP address of port used for communicating.

Private Sub mnuPort_Click()
MsgBox "This comouter has Port : " & Winsock1.LocalPort, vbInformation, "Local IP Address"
End Sub

 

Using Program Connect
     After complying as EXE, open program twice, there will be two windows like the picture. The first window, will be supposed to be server and client. In the server window, setting Port is not the same with the example, but 2 programs must be similar. Then, clicking the button, listen. 

     For Client, choose Option Button as Client. After that setting number of IP Address of server computer, but if it is not be connected network system, standalone, then put IP of yours instead. Then, press the button Connect of Server, it will have respond as Message Box. So now, all finished, they can send message to interact.

I hope that you can use programming to apply with other programs; we may be your method and experiment for the next section.

Click here to download Visual Basic Project Code at Download 

 

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