Page 1 of 1

Problem with CloseNetworkConnection

Posted: Sat Jul 22, 2006 8:56 pm
by pfoo
Hello !

I'm working on a chat server/client application and I've got some problem with the CloseNetworkConnection function.
When a client send "QUIT" to my server, I use CloseNetworkConnection(ClientID) (for example, clientid = 1904). No problem here. But if the same client reconnect (same ip ? No sure, i'm working localy), there are problem with his new clientid. The "Case 1" told me that the ClientID is 1908, but when the client send something, "Case 2" told me that his clientID is 1904...
But, if the client disconnect by himself (= "Case 4" for server), everything work good ..

Any idea ?

Posted: Sat Jul 22, 2006 8:58 pm
by Shannara
One of the things you need to make sure that if clientid is a global or in a link list and not removed, you need to manually set it to 0 after disconnecting the client. I found this out the hard way.

Posted: Sat Jul 22, 2006 9:34 pm
by pfoo
I tried to manually set ClientID to 0, but it don't change anything ...

Simplified code :

Code: Select all

If InitNetwork() = 0
  MessageRequester("Error", "Can't initialize the network !", 0)
  End
EndIf


Port = 7000
Buffer = AllocateMemory(1000)

; ------------------------------------------------------
; ------------------------------------------------------

OpenConsole()
  If CreateNetworkServer(Port)
    PrintN("Server created (Port "+Str(Port)+").")
  Else
    PrintN("Server cannot be open (port in use?)")
    Delay(3000)
    End
  EndIf

    Repeat
    
    
      SEvent = NetworkServerEvent()
      If SEvent
        ClientID = NetworkClientID()
        Select SEvent
        
          Case 1
            PrintN("New connection: " +Str(ClientID))
            SendNetworkString(ClientID, "HELO"+Chr(10)+Chr(13))

          Case 2
            ReceiveNetworkData(ClientID, Buffer, 1000)
            Debug NetworkClientID()
            PrintN("New data from "+Str(clientID))
            PrintN(PeekS(Buffer))

      ; ------------------------------------------------------
      ; # QUIT
      ; ------------------------------------------------------
              If (StringField(PeekS(Buffer), 1, Chr(10)) = "QUIT")
                CloseNetworkConnection(ClientID)
              EndIf

          Case 3
            Debug "receiving a file from: "+Str(clientID)
              
          Case 4
            Debug "quit: "+Str(ClientID)

        ; fin du <Select>
        EndSelect
      ; fin du <If>
      EndIf
        
    Until Quit = 1
End

Posted: Sun Jul 23, 2006 12:15 pm
by pfoo
Note : I'm still using v3.94 windows (waiting for PB 4.0 linux before downloading v4)