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.
|