Back  | Code Snippets |  [Bill's Home]

WinSock server (with multiple connections)

WinSock Server (with multiple connections) ZIP
NET Client/server ]

The server program can be modified so that it can handle more than one connection from a client. This is achieved with:

Private Sub myTCPServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
If Index = 0 Then

intMax = intMax + 1
Load myTCPServer(intMax)

' Accept the request with the requestID parameter.
myTCPServer(intMax).Accept requestID
ipaddress.Text = myTCPServer(intMax).RemoteHostIP

where the Index parameters is set to a 0 when there is a new connection. The intMax variable holds the number of connections, and the Load is used to load a new WinSock object. A sample screen shot from a server with two client connections is given next:

 

The code for the client form is:

Dim intMax As Long

Private Sub About_Click()
frmAbout.Show
End Sub

Private Sub exit_Click()
End
End Sub

Private Sub Form_Load()
intMax = 0
' Set the local port to 1001 and listen for a connection
listenport.Text = "1001"
localipaddress.Text = myTCPServer(0).LocalIP ' Show local IP address
Call show_status
End Sub


Private Sub show_status()
' Shows the state of first server connection
If (myTCPServer(0).State = sckClosed) Then
status.Text = "CLOSED"
ElseIf (myTCPServer(0).State = sckOpen) Then
status.Text = "OPEN"
ElseIf (myTCPServer(0).State = sckListening) Then
status.Text = "LISTENING..."
ElseIf (myTCPServer(0).State = sckConnecting) Then
status.Text = "CONNECTING"
ElseIf (myTCPServer(0).State = sckConnected) Then
status.Text = "CONNECTED"
ElseIf (myTCPServer(0).State = sckError) Then
status.Text = "ERROR"
Else
status.Text = myTCPServer(0).State
End If
End Sub


Private Sub listenport_Change()
If myTCPServer(0).State <> sckClosed Then myTCPServer(0).Close
myTCPServer(0).LocalPort = listenport.Text
myTCPServer(0).Listen
Call show_status
End Sub

Private Sub myTCPServer_Close(Index As Integer)
If myTCPServer(Index).State <> sckClosed Then myTCPServer(Index).Close
myTCPServer(Index).LocalPort = listenport.Text
myTCPServer(Index).Listen
ipaddress.Text = ""
iphost.Text = ""
remoteport.Text = ""
Call show_status
End Sub

Private Sub myTCPServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
If Index = 0 Then

intMax = intMax + 1
Load myTCPServer(intMax)

' Accept the request with the requestID parameter.
myTCPServer(intMax).Accept requestID
ipaddress.Text = myTCPServer(intMax).RemoteHostIP
If (myTCPServer(intMax).Protocol = 0) Then
iphost.Text = "TCP"
Else
iphost.Text = "UDP"
End If
remoteport.Text = myTCPServer(Index).remoteport
Check1.Value = 1 ' show that remote has connected
Call show_status
no_connections.Text = intMax
End If
End Sub


Private Sub myTCPServer_DataArrival(Index As Integer, ByVal requestID As Long)
' Read incoming data into the str variable,
' then display it to ShowText
Dim str As String
myTCPServer(Index).GetData str
ShowText.Text = ShowText.Text + str
Call show_status
End Sub

Private Sub SendTextData_KeyPress(KeyAscii As Integer)
Call show_status
If (KeyAscii = 13) Then
myTCPServer(0).SendData SendTextData.Text + vbCrLf
show_text_sent = show_text_sent + SendTextData.Text + vbCrLf
SendTextData.Text = ""
End If
End Sub

 

This program can be used to test the messages sent from the client to the server.

It can be seen that the local server port is 80, and that the client is using port 3078. A Microsoft Internet Explorer client which requires to access the index.html page will request it with:

GET /index.html HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, applica-tion/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */*
Accept-Language: en-gb
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Q312461)
Host: localhost
Connection: Keep-Alive

and a NetScape 4.7 client will access it with:

GET /index.html HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.77 [en] (Windows NT 5.0; U)
Host: localhost
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8

If we manually issue the following command from our client (and press RETURN twice):

GET /index.html HTTP/1.0

The sever responds back with the HTTP information and the WWW page:

HTTP/1.1 200 OK
Date: Sun, 20 Jan 2002 21:44:17 GMT
Server: Apache/1.3.14 (Win32)
Last-Modified: Thu, 17 Jan 2002 10:58:22 GMT
ETag: "0-67c6-3c46ae4e"
Accept-Ranges: bytes
Content-Length: 26566
Connection: close
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML><HEAD><TITLE>Bill Buchanan Home Page</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META etc ....

As shown next:

 

So how do you respond to the clients?

In the DataArrival subroutine the following code will be added:

 

if myTCPServer(1).BytesReceived then
myTCPServer(1).GetData str
end if
if myTCPServer(2).BytesReceived then
myTCPServer(2).GetData str
end if
if myTCPServer(3).BytesReceived then
myTCPServer(3).GetData str
end if

To send data to client 1, it is sent with:

myTCPServer(1).SendData str1;

For the ConnectionRequest sub:

if index=0 then
intMax=intMAx+1;
Load MyTCPServer(intMax)
MyTCPServer.LocalPort = 0;
MyTCPServer(intMax).Accept requestID
end if