Page 1 of 1

Network problem, help!

Posted: Tue Mar 07, 2006 10:42 am
by karu
Sorry my bad english
If client connect first time to server, server assign id to connected client. So, in the future all the messages that server receive from this client is with this id. Ok all good but, when server disconnect the client and client connect again to server, server assign new id to this client, but messages that client send to server are with old id.

LOG

Code: Select all

Server created (Port 9999).
Client nr: 3832 connected
3832> aaa
3832> sss
3832> fff
3832> iii
Client nr: 3832 disconnected by server
Client nr: 3820 connected       *** same client
3832> aaa    *** old id 
3832> ddd    *** old id 
3832> fff    *** old id 

Code: Select all

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

Port = 9999
Buffer = AllocateMemory(1000)

If CreateNetworkServer(Port)

  Debug "Server created (Port "+Str(Port)+")."
  
  Repeat
      
    SEvent = NetworkServerEvent()
  
    If SEvent
    
      ClientID = NetworkClientID()
  
      Select SEvent

        Case 1
            Debug "Client nr: " + Str(ClientID) + " connected"
            SendNetworkString(ClientID, "Hello!")
  
        Case 2
          Buffer = AllocateMemory(1000)
          ReceiveNetworkData(ClientID, Buffer, 1000)
          pikkus= FindString(PeekS(Buffer), Chr(13), 1)
          mess.s= Left(PeekS(Buffer), pikkus)
          FreeMemory(*Buffer)       

          Debug Str(ClientID)+ "> "+ mess
          
          If Left(mess, 3)= "iii"
              SendNetworkString(ClientID, "Go away!")
              Debug "Client nr: " + Str(ClientID) + " disconnected by server"
              CloseNetworkConnection(ClientID)
          EndIf
          
        Case 4
          Debug "Client nr: " + Str(ClientID) + " disconnected"

      EndSelect
    EndIf

    If SEvent = 0
      Delay(1)
    EndIf

  Until Quit = 1 
  
  CloseNetworkServer()
Else
  MessageRequester("Error", "Can't create the server (port in use ?).", 0)
EndIf

  
End   

Posted: Tue Mar 07, 2006 4:21 pm
by Thalius
Hihi,

Works here.. using PB 3.94.
Server created (Port 9999).
Client nr: 896 connected
896>
896>
896>
896>
896>
896>
896>
896>
Client nr: 896 disconnected
Client nr: 896 connected
896>
896>
896>
896>
896>
896>
Client nr: 888 connected
888>
888>
888>
888>
888>
888>
888>
888>
888>
Client nr: 896 disconnected
Client nr: 896 connected
896>
896>
896>
896>
896>
896>
or am i missing here something ? =)

Thalius

Posted: Tue Mar 07, 2006 5:44 pm
by Tranquil
You need to take a closer look on packet handling on TCP/IP.

ReceiveNetworkData(ClientID, Buffer, 1000)

returns the number of bytes received from the network or a WSError if the connection is closed or something else.

You need to check if the received bytes is exactly contains the numbers of bytes of your sended message.

If you call SendnetworkString() it is not garanted that you only have to call ReveiveNetworkData() once a time!

The same for SendNetworkData(). Lets expect the following:
You trie to send a message of 60000 Bytes via Internet to another user. This user has got a very small bandwith connection, maybe not all 60000 bytes could be send the first time!
In this case SendNetworkData() (and hopefully SendNetworkString too) will return an error (Constant SOCKET_ERROR ). With WSAGetLastError_() API you could get the error which occures. In this case a #WSAEWOULDBLOCK should be returned. And you have to try to send your datas again and again till it is successfull!

I STRONGLY advice to read this:

http://msdn.microsoft.com/library/defau ... odes_2.asp

Nearly nobody follows this and nearly 90% of networking codes using SendNetworkData() and ReceiveNetworkData() in this forum are still wrong!!

Posted: Tue Mar 07, 2006 8:14 pm
by Thalius
i was referring to teh socketID issue =)

Didnt say his code was perfect. Atm i am working on a Class ( PB4/OOP ) which simplifies this a bit for newcomers. Kind of strips it back to 2 lists Rec() Send(). Tho i am still halfway biting my teeth on thinking OOP-Like again after like 2 years not touching any OOP code =)

When i get the time ill post a simple example / and or the funct- class but due timeissues may take a while.



Thalius

Posted: Sat Aug 12, 2006 12:23 pm
by pfoo
I've got the same problem (see http://www.purebasic.fr/english/viewtop ... t=clientid)

It seems that everything work correctly with PB4 :)

But thx Tranquil, i'd to take care of this !
I don't want to see the same problem than MSN got, "the message cannot be send"