More Socket Problems.........

Dave dave at looktowindward.com
Thu Feb 28 11:52:23 EST 2008


Hi,

I the same app running on two machines. The two machines are called  
"thestateoftheart" which has a LAN IP Address of 192.168.1.101 and  
"looktowindward" which has an IP Address is 192.168.1.102.

Both machines can be servers and both can be clients of the other  
server. I have two buttons, one starts the server and one the client  
(see the two mouseUp handlers below, one on the "Start Server" Button  
and one on the "Start Client" button.

I hit the "StartServer" button on "thestateoftheart" and hit the  
"StartClient" button on "looktowindward".

This results in the following dump of the log file(s)

------------------------------------------------------------------------ 
----
SERVER LOG
------------------------------------------------------------------------ 
----

StartServer - Accept: 6000

ServerContactedByClient: 192.168.1.105:50088
ServerContactedByClient, Bad Client: 192.168.1.105:50088

ServerContactedByClient: 192.168.1.105:50089
ServerContactedByClient, Bad Client: 192.168.1.105:50089

ServerContactedByClient: 192.168.1.105:50090
ServerContactedByClient, Bad Client: 192.168.1.105:50090

ServerContactedByClient: 192.168.1.105:50091
ServerContactedByClient, Bad Client: 192.168.1.105:50091

------------------------------------------------------------------------ 
----
CLIENT LOG
------------------------------------------------------------------------ 
----

StartOneClient - Connect: 192.168.1.87:6000|looktowindward
ClientSocketOpen - Open Socket: 192.168.1.87:6000|looktowindward
ClientSocketOpen - Data Written: 192.168.1.87:6000|looktowindward  
00000027$SYS,Connect,looktowindward
ClientSocketConnectionDataSent - Data Written: 192.168.1.87:6000| 
looktowindward
socketError: 192.168.1.87:6000|looktowindward Error 54 reading socket

StartOneClient - Connect: 192.168.1.87:6000|looktowindward
ClientSocketOpen - Open Socket: 192.168.1.87:6000|looktowindward
ClientSocketOpen - Data Written: 192.168.1.87:6000|looktowindward  
00000027$SYS,Connect,looktowindward
ClientSocketConnectionDataSent - Data Written: 192.168.1.87:6000| 
looktowindward
socketError: 192.168.1.87:6000|looktowindward Error 54 reading socket

StartOneClient - Connect: 192.168.1.87:6000|looktowindward
ClientSocketOpen - Open Socket: 192.168.1.87:6000|looktowindward
ClientSocketOpen - Data Written: 192.168.1.87:6000|looktowindward  
00000027$SYS,Connect,looktowindward
ClientSocketConnectionDataSent - Data Written: 192.168.1.87:6000| 
looktowindward
socketError: 192.168.1.87:6000|looktowindward Error 54 reading socket

StartOneClient - Connect: 192.168.1.87:6000|looktowindward
ClientSocketOpen - Open Socket: 192.168.1.87:6000|looktowindward
ClientSocketOpen - Data Written: 192.168.1.87:6000|looktowindward  
00000027$SYS,Connect,looktowindward
ClientSocketConnectionDataSent - Data Written: 192.168.1.87:6000| 
looktowindward

-----------------------------------

Sometimes I don't get the socketErrors and I just get the last group  
in the Client Log:

StartOneClient - Connect: 192.168.1.87:6000|looktowindward
ClientSocketOpen - Open Socket: 192.168.1.87:6000|looktowindward
ClientSocketOpen - Data Written: 192.168.1.87:6000|looktowindward  
00000027$SYS,Connect,looktowindward
ClientSocketConnectionDataSent - Data Written: 192.168.1.87:6000| 
looktowindward

-------------------------------

Questions:

Why do I sometimes not get a socketError or a socketTimeout ?

Why does it sometimes repeat (as it should) for a few times and then  
stop?

All the Best
Dave

Here is the code:

The mouseUp handler are in the main stack of the app and the


------------------------------------------------------------------------
--
--  Server - mouseUp
--
------------------------------------------------------------------------
on mouseUp
   local myClientAllowList
   local myHostName

   put the hostName into myHostName
   if myHostName = "thestateoftheart" then
     put "192.168.1.101" & cr into myClientAllowList

     put "192.168.1.111" & cr into myClientAllowList -- Cause an error

   else if myHostName = "looktowindward" then
     put "192.168.1.102" & cr into myClientAllowList
   else
     answer error "Unknown Host:" && myHostName with "OK"
     exit mouseUp
   end if


   get StartServer(6000,myClientAllowList)
end mouseUp


------------------------------------------------------------------------
--
--  Client - mouseUp
--
------------------------------------------------------------------------
on mouseUp
   local myServerList
   local myHostName

   put the hostName into myHostName

   if myHostName = "thestateoftheart" then
     put "192.168.1.101:6000" & cr into myServerList
   else if myHostName = "looktowindward" then
     put "192.168.1.102:6000" & cr into myServerList
   else
     answer error "Unknown Host:" && myHostName with "OK"
     exit mouseUp
   end if

   get StartClient(myServerList,myHostName)
end mouseUp


The following is including in the App using the "start using" command:


--  StartServer

function StartServer theServerPortNumber,theClientAllowList

   get DebugLogString("StartServer - Accept:" && theServerPortNumber)

   put theClientAllowList into sgServerClientAllowList
   accept connections on theServerPortNumber with message  
"ServerContactedByClient"

   return empty
end StartServer


--  ServerContactedByClient

on ServerContactedByClient theIPAddress
   local myClientIPAddress
   local myResult

   put the result into myResult

   get DebugLogString("ServerContactedByClient:" && theIPAddress &&  
myResult)

   if myResult <> empty then
     breakpoint
     exit ServerContactedByClient
   end if

   set the itemDelimiter to ":"
   put item 1 of theIPAddress into myClientIPAddress

   if myClientIPAddress is not among the lines of  
sgServerClientAllowList then
     get DebugLogString("ServerContactedByClient, Bad Client:" &&  
theIPAddress)
     close socket theIPAddress
   end if

   read from socket theIPAddress for kPacketCountSize chars with  
message "ServerReceivedPacketSize"

end ServerContactedByClient



--  ServerReceivedPacketSize

on ServerReceivedPacketSize theIPAddress,thePacketSize
   local myResult
   local myPacketData
   local myPacketID
   local myPacketCommand
   local myConnectionID

   put the result into myResult

   get DebugLogString("ServerReceivedPacketSize:" && theIPAddress &&  
thePacketSize && myResult)

   if myResult <> empty then
     get DebugLogString("ServerReceivedPacketSize, Error:" && myResult)
     breakpoint
   end if

   read from socket theIPAddress for thePacketSize chars
   put the result into myResult
   put it into myPacketData

   get DebugLogString("ServerReceivedPacketSize, Packet Data:" &&  
myPacketData && myResult)

   if myResult <> empty then
     get DebugLogString("ServerReceivedPacketSize, Error:" && myResult)
     breakpoint
   end if

   --
   --  Get the Message Content
   --
   put item 1 of myPacketData into myPacketID
   put item 2 of myPacketData into myPacketCommand

   if myPacketID = "$SYS" then
     if myPacketCommand = "Connect" then
       put item 3 of myPacketData into myConnectionID

      end if

   end if

end ServerReceivedPacketSize


--  StartClient

function StartClient theServerList,theConnectionID

   set the socketTimeoutInterval to (2 * 1000)
   put theServerList into sgConnectToServerList
   repeat for each line myIPAddress in sgConnectToServerList
     send "StartOneClient myIPAddress,theConnectionID" to me in 0  
seconds
   end repeat

   return empty
end StartClient



--  StartOneClient

on StartOneClient theIPAddress,theConnectionID
   local myServerIPAddress
   local myServerPortNumber
   local mySocketAddress
   local myConnectString
   local myPacketSize
   local myResult

   put _DecodeIPAddress 
(theIPAddress,myServerIPAddress,myServerPortNumber) into myResult
   if myResult <> empty then
     get DebugLogString("StartOneClient - IP Address Error:" &&  
theIPAddress && myResult)
     breakpoint
     exit StartOneClient
   end if

    put myServerIPAddress & ":" & myServerPortNumber & "|" &  
theConnectionID into mySocketAddress

   get DebugLogString("StartOneClient - Connect:" && mySocketAddress  
&& myResult)

   open socket mySocketAddress with message "ClientSocketOpen"
   put the result into myResult
   if myResult <> empty then
     get DebugLogString("StartOneClient - Write Socket Count Error:"  
&& mySocketAddress && myPacketSize && myResult)
     breakpoint
     exit StartOneClient
   end if

end StartOneClient


--  ClientSocketOpen

on ClientSocketOpen theIPAddress
   local myConnectString
   local myConnectionID
   local myPacketSize
   local myResult

    put the result into myResult

   get DebugLogString("ClientSocketOpen - Open Socket:" &&  
theIPAddress && myResult)

   if myResult <> empty then
     get DebugLogString("ClientSocketOpen - Open Socket Error:" &&  
theIPAddress && myResult)
     breakpoint
     exit ClientSocketOpen
   end if

   set the itemDelimiter to "|"
   put item 2 of theIPAddress into myConnectionID
   if myConnectionID = empty then
     get DebugLogString("ClientSocketOpen - Bad ConnectionID:" &&  
theIPAddress && myConnectionID)
     breakpoint
   end if

   put "$SYS,Connect," & myConnectionID into myConnectString
   put _StringPadLeft(length(myConnectString),kPacketCountSize,"0")  
into myPacketSize
   write myPacketSize & myConnectString to socket theIPAddress with  
message "ClientSocketConnectionDataSent"

   get DebugLogString("ClientSocketOpen - Data Written:" &&  
theIPAddress && myPacketSize & myConnectString)
end ClientSocketOpen

   on ClientSocketConnectionDataSent theIPAddress
   local myResult

   put the result into myResult
   if myResult <> empty then
     get DebugLogString("ClientSocketConnectionDataSent - Write  
Socket Count Error:" && theIPAddress && myResult)
     breakpoint
     exit ClientSocketConnectionDataSent
   end if

   get DebugLogString("ClientSocketConnectionDataSent - Data  
Written:" && theIPAddress)

end ClientSocketConnectionDataSent

--  socketTimeout

on socketTimeout theIPAddress
   local myIPAddressAndPort
   local myConnectionID
   local myResult

   set the itemDelimiter to "|"
   put item 1 of theIPAddress into myIPAddressAndPort
   put item 2 of theIPAddress into myConnectionID

   put the result into myResult

   get DebugLogString("socketTimeout - Timeout Error:" &&  
theIPAddress && myResult)

   if myConnectionID = empty then
     get DebugLogString("socketTimeout - Bad ConnectionID:" &&  
theIPAddress && myConnectionID)
     breakpoint
   end if

   send "StartOneClient myIPAddressAndPort,myConnectionID" to me in 0  
seconds

end socketTimeout


--  socketError

on socketError theIPAddress,theErrorMessage
   local myIPAddressAndPort
   local myConnectionID

   set the itemDelimiter to "|"
   put item 1 of theIPAddress into myIPAddressAndPort
   put item 2 of theIPAddress into myConnectionID

   get DebugLogString("socketError:" && theIPAddress && theErrorMessage)

   if myConnectionID = empty then
     get DebugLogString("socketError - Bad ConnectionID:" &&  
theIPAddress && myConnectionID)
     breakpoint
   end if

   send "StartOneClient myIPAddressAndPort,myConnectionID" to me in 2  
seconds

end socketError









More information about the Use-livecode mailing list