'Develop/VB6'에 해당되는 글 2건

  1. 2013.04.08 Small TCP/IP Hello Client Software with VB 6.0
  2. 2013.04.08 Small TCP/IP Hello server with VB 6.0

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 멻ello� 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

 

 

http://op.htmsoft.com/articles/Articlea2-1-2.html


 

'Develop > VB6' 카테고리의 다른 글

Small TCP/IP Hello server with VB 6.0  (0) 2013.04.08
And

Small TCP/IP Hello server with VB 6.0

|

This article is first part of a two part article for people 
who want to start writing socket programming with VB.

This example uses an ocx component that comes with VB. You 
will need a from with a command button (command1), a socket 
component (winsock1) and a listbox (list1).

We will need to do the following steps to have our hello 
server running.

1- We must bind TCP/IP socket to port 1024 when form loads.

2- When we press Listen Key (command1) socket starts 
   listening port 1024. In this way program will be able to 
   accept connections. Listening socket is usually used for 
   just receiving TCP/IP connections. It will also queue 
   them until our program is free to serve them. We will 
   usually need another socket to service connections that
   has been queued. However as we want to create a small 
   server we will use a single socket for both listening 
   and communication with client programs.

3- When a socket on a remote program opens a connection to 
   our program, a Connection request event is triggered and 
   ConnectionRequest subroutine is automatically called.
   In this subroutine we can accept the connection.

4- Now that everything is prepared, whenever an accepted 
   socket tries to send us data another event called 
   DataArrival will be triggered. When we receive this event 
   we first read data that is sent to us and show it in a 
   listbox and then send a 'hello' string back to the client 
   program connected to us. This is why we call this server 
   a 'hello' server. 

5- After sending data, server has no reason to keep 
   connection so it must close the connection.
   When sending data is finished, a SendComplete event is 
   triggered. As we want to accept more connections we close 
   socket and then call listen function again to be ready 
   for accepting other incoming connections.

6- 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()
  Winsock1.Listen
End Sub

Private Sub Form_Load()
  Winsock1.Bind 1024
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
  If Winsock1.State <> sckClosed Then Winsock1.Close
  'Load Winsock1(newInstanceIndex)
  Winsock1.Accept requestID
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Winsock1.GetData a$, vbString
  'received data is in a$ , you can use it
  List1.AddItem ("Received : " + a$)
  Winsock1.SendData "hello" + vbCrLf
  'The string 'hello' is sent in answer to client request
  List1.AddItem ("Sent : Hello")
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, "Error"
End Sub

Private Sub Winsock1_SendComplete()
  Winsock1.Close
  Winsock1.Listen
End Sub


7- Server written in this article can serve one connection 
   at a time. If you want you can use more connections by 
   loading new sockets for each incoming connection.

In next part of this article we will write a client side 
program for this server. 

Client program will connect server program over network and 
will receive 'hello' string and then connection will be 
closed by server program. You can test this program by 
connecting to port 1024 using a telnet program. Whatever you
send to it, server will answer with a 'hello'.

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

 

'Develop > VB6' 카테고리의 다른 글

Small TCP/IP Hello Client Software with VB 6.0  (0) 2013.04.08
And
prev | 1 | next