Download ActiveSocket    |     Visit the ActiveSocket website

Index - Log in! - Sign up! - Visit website
Multiple clients connecting to single server at same time
 
ActiveXperts Software Forum Index -> ActiveSocket Toolkit -> General
 
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Dale Garrison



Joined: 27 Mar 2008
Posts: 1
Location: Oklahoma City, OK

PostPosted: Thu Mar 27, 2008 5:51 pm    Post subject: Multiple clients connecting to single server at same time

I have a question concerning the following scenario:
1. Server program starts and "listens" for incoming connection
2. Client 1 program establishes a link with server
3. Server and Client 1 pass data back and forth
4. While Client 1 has a connection to the server, Client 2 establishes a connection to the same server and "sends" data to the server. Client 2 then waits for "HasData" to go TRUE.
5. Client 1 completes the transmission to the server and Disconnects
6. The connection from Client 2 to server ends when Client 1 disconnects. Why is this?

Below are the server and client programs I am using. (To run, change their extensions to .vbs) On the same computer I...
1. Start the server program
2. Start the client program
3. Start the same client program a second time
The programs create logs in a c:\ASocketLog folder that tell what each program is doing. The second iteration of the client program establishes a connection to the server before the 1st client has completed. Then when the 1st iteration of the client program completes and disconnects, connection is also lost between the server and the second client iteration. Why is this?

***************Client Program***************************
Option Explicit
Dim wshShell, fso , objTcp, objIcmp, objConstants, objAutoIt, ssLog
Dim EndofTransmission, PortNumber, strConnection, strRequest, strServerName
Dim strMsg, arrMsg, StopTime, strFileName, ClientNo, PostTime, strPacket

Const HKEY_LOCAL_MACHINE = &H80000002
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const ForWriting = 2
Const ForReading = 1
Const ForAppending = 8
EndofTransmission = Chr(4)
PortNumber = 2401
strMsg="A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,w,X,Y,Z,EOF"
arrMsg = Split(strMsg,",")

Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set objTcp = CreateObject( "ActiveXperts.Tcp" )
Set objIcmp = CreateObject("ActiveXperts.Icmp")
Set objConstants = CreateObject( "ActiveXperts.ASConstants" )
objTcp.Protocol = objConstants.asSOCKET_PROTOCOL_RAW
strServerName = WshShell.ExpandEnvironmentStrings("%computername%")
If Not fso.FolderExists("c:\ASocketLog") Then _
fso.CreateFolder "c:\ASocketLog"
on Error Resume Next
ClientNo=0
Do
ClientNo=ClientNo+1
strFileName = "c:\ASocketLog\TestClient-" & ClientNo & ".txt"
Err.Clear
If fso.FileExists(strFileName) Then _
fso.DeleteFile strFileName,True
Loop Until Err.Number = 0
On Error goto 0
Set ssLog=fso.OpenTextFile(strFileName,ForWriting,True)

StopTime = DateAdd("s",30,Now)
PostTime = DateAdd("s",2,Now)
objTcp.Connect strServerName, PortNumber
Do Until objTcp.ConnectionState = objConstants.asSOCKET_CONNSTATE_CONNECTED
Wscript.Sleep 500
If Now > StopTime Then
Wscript.Echo "Client(" & ClientNo & ") Time out occurred trying to connect to " & strServerName
Wscript.Quit
End If
objTcp.Connect strServerName, PortNumber
If objTcp.ConnectionState = objConstants.asSOCKET_CONNSTATE_DisCONNECTED And Now > PostTime Then
ssLog.WriteLine Now & vbtab & "Waiting for Connection"
PostTime = DateAdd("s",2,Now)
End If
Loop

ssLog.WriteLine Now & vbtab & "Connection made"
strConnection = strServerName

For Each strMsg in arrMsg

strPacket = "From Client(" & ClientNo & ")=" & strMsg & EndofTransmission
objTcp.SendBytes strPacket
If objTcp.LastError <> 0 Then
Wscript.echo "Client(" & ClientNo & ") Error Transmitting to " & strConnection & " (" & objTcp.GetErrorDescription(objTcp.LastError) & ")"
Wscript.Quit
End If
ssLog.WriteLine Now & vbtab & "Sent " & strPacket

'Receive Request
strRequest = ""
Do
'Wait for request to be sent
PostTime = DateAdd("s",2,Now)
Do Until objTcp.HasData
If Now > PostTime Then
ssLog.WriteLine Now & vbtab & "Waiting for HasData to go TRUE"
PostTime = DateAdd("s",2,Now)
End If
wscript.sleep 250
'If at any time the connection becomes disconnect this exits this subroutine
If objTcp.ConnectionState = objConstants.asSOCKET_CONNSTATE_DISCONNECTED Then
Wscript.echo "Client(" & ClientNo & ") Error: Connection was lost to server " & strConnection
Wscript.quit
End If
Loop
strRequest = strRequest & objTcp.ReceiveBytes
If objTcp.LastError <> 0 Then
Wscript.echo "Client(" & ClientNo & ") Error receiving from " & strConnection & " (" & objTcp.GetErrorDescription(objTcp.LastError) & ")"
Wscript.Quit
End If
Loop Until Right(strRequest,1) = EndofTransmission

strRequest = Left(strRequest,Len(strRequest)-1)
ssLog.WriteLine Now & vbtab & "Received: " & chr(34) & strRequest & chr(34) & " from " & strConnection

Wscript.Sleep 2000
Next

ObjTcp.Disconnect

ssLog.Close
Wscript.echo "Normal Communications disconnect from client(" & ClientNo & ") to " & strConnection
wscript.quit

**************Server Program ********************************
Option Explicit
Dim wshShell, fso , objTcp, objIcmp, objConstants, objAutoIt, ssLog
Dim EndofTransmission, PortNumber, strConnection, strRequest, StopTime
Const HKEY_LOCAL_MACHINE = &H80000002
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Const ForWriting = 2
Const ForReading = 1
Const ForAppending = 8
EndofTransmission = Chr(4)
PortNumber = 2401

Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set objTcp = CreateObject( "ActiveXperts.Tcp" )
Set objIcmp = CreateObject("ActiveXperts.Icmp")
Set objConstants = CreateObject( "ActiveXperts.ASConstants" )
objTcp.Protocol = objConstants.asSOCKET_PROTOCOL_RAW
If Not fso.FolderExists("c:\ASocketLog") Then _
fso.CreateFolder "c:\ASocketLog"

Set ssLog=fso.OpenTextFile("c:\ASocketLog\TestSrvr.txt",ForWriting,True)

objTcp.StartListening PortNumber

Do
If objTcp.ConnectionState = objConstants.asSOCKET_CONNSTATE_DISCONNECTED Then _
objTcp.StartListening PortNumber
ssLog.WriteLine Now & vbtab & "Now Listening..."
Stoptime = DateAdd("s",60,Now)
Do While objTcp.ConnectionState = objConstants.asSOCKET_CONNSTATE_LISTENING
Wscript.sleep 500
If Now > StopTime Then
objTcp.Disconnect
ssLog.Close
Wscript.Echo "Server Time out occurred while waiting for connection"
wscript.quit
End If
Loop
If objTcp.ConnectionState = objConstants.asSOCKET_CONNSTATE_DISCONNECTED Then
ssLog.Close
Wscript.echo "Server Error: Connection disconnected while listening"
Wscript.quit
End If

strConnection = objTcp.RemoteAddress

'Receive Request
Do
strRequest = ""
Do
'Wait for request to be sent
Do Until objTcp.HasData
wscript.sleep 250
'If at any time the connection becomes disconnect this will exit
If objTcp.ConnectionState = objConstants.asSOCKET_CONNSTATE_DISCONNECTED Then
ssLog.Close
Wscript.echo "Server Error: Connection was lost to client " & strConnection
Wscript.quit
End If
Loop
strRequest = strRequest & objTcp.ReceiveBytes
If objTcp.LastError <> 0 Then
ssLog.Close
Wscript.echo "Server Error receiving from " & strConnection & " (" & objTcp.GetErrorDescription(objTcp.LastError) & ")"
Wscript.Quit
End If
Loop Until Right(strRequest,1) = EndofTransmission

'Echo request back to client
objTcp.SendBytes strRequest
If objTcp.LastError <> 0 Then
ssLog.Close
Wscript.echo "Server Error Transmitting to " & strConnection & " (" & objTcp.GetErrorDescription(objTcp.LastError) & ")"
Wscript.Quit
End If

'Record message received
strRequest = Left(strRequest,Len(strRequest)-1)
ssLog.WriteLine Now & vbtab & "Received: " & chr(34) & strRequest & chr(34) & " from " & strConnection

Loop Until objTcp.ConnectionState = objConstants.asSOCKET_CONNSTATE_DISCONNECTED
Loop While True
Back to top
Display posts from previous:   
Post new topic   Reply to topic
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum