Home Index

Small TCP/IP Hello Client Software with VB 6.0
 
Date: January 01 2001
Subject: Online Programmer Ezine, Volume 2, No.1
Article: Small TCP/IP Hello Client Software with VB 6.0

 

This article is second part of a two-part article for 
programmers who want to start writing socket programs with 
VB.

In previous part of this article we saw how we can write a 
simple hello server in VB. We used a socket object that 
comes with VB for this purpose. Mswnsock.ocx is a very 
stable socket object that we used in our program. 

This example uses Winsock component again. You will need a 
form with a command button (command1), a socket component 
(winsock1) and a list box (list1) in this example.

We will need to do the following steps to have our hello 
client connect to our server and communicate with it.

1- This time we don’t need to bind anything.

2- when we press connect key (command1) socket starts 
connecting to a server via TCP/IP network. Connect function 
needs two parameters before it can connect to a server. 

IP address specifies the host that server is running on it. 
As we want the client to connect server program running on 
our own server we use loop back address that points to our 
computer. Loop back address is ‘127.0.0.1’. You can use 
address of any other host instead of this. 

Second parameter is TCP port. As you know can assign a port 
number between 1 and above 65000 to each clinet/server 
application. 

This is TCP channel we use to avoid communication conflicts 
between different programs that communicate between two 
hosts. We chose an optional port number of 1024.


3- When a client socket opens a connection to server program,
a connect event is triggered on client program. When we 
receive this event we start sending data to server and 
also simultaneously show it in listbox1. Message sent to 
server is ‘hi!’

4- After server has finished receiving sent data, it sends 
back answer data that is ‘hello’ string. It will close 
connection after it has sent its data. So closing 
connections is done by server side. 

5- If any error occurs on socket an 'onerror' event is 
triggered. In this case we will display an error message.

And now program source:

Private Sub Command1_Click()
If Winsock1.State <> sckClosed Then
Winsock1.Close
End If
Winsock1.Connect "127.0.0.1", 1024
End Sub

Private Sub Winsock1_Connect()
Winsock1.SendData "Hi!" + vbCrLf
List1.AddItem "Sent to Server: Hi!"
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData a$, vbString
List1.AddItem "Received from server: " + a$
End Sub

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)

MsgBox "Error in socket : " + Description, vbOKOnly

End Sub

If you need codes for this article you can visit our site 
and go to ezine page. There you will find a link to zip 
source files.

http://op.htmsoft.com/ezines.html



Home Index
© 2000 OnlineProgrammer (HtmSoft.com). All Rights Reserved