Page 1 of 1

Stop one server stealing another server's events..

Posted: Wed Apr 13, 2011 11:49 pm
by zoot33
The help file demonstrates a server with the below code, and this works great when there is only one of them.

I have entirely separate server applications running on the same pc with separate port numbers. When one of the servers does the 'SEvent = NetworkServerEvent()' line, it regularly takes the other server's events.
Is this a situation to use EventServer() ? Are there any demonstration codes using this command ?

How can I rewrite the below code, so that this server only affects events arriving on port 6832, and doesn't interfere with events arriving on port 6833 for example ?

Thanks.

p.s. if my question isn't clear enough, I can write a much fuller version if required.

Code: Select all

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

Port = 6832
*Buffer = AllocateMemory(1000)

If CreateNetworkServer(0, Port)
  MessageRequester("PureBasic - Server", "Server created (Port "+Str(Port)+").", 0)
  
  Repeat
    SEvent = NetworkServerEvent()
    If SEvent
      ClientID = EventClient()
      Select SEvent
        Case #PB_NetworkEvent_Connect
          MessageRequester("PureBasic - Server", "A new client has connected !", 0)
        Case #PB_NetworkEvent_Data
          MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has send a packet !", 0)
          ReceiveNetworkData(ClientID, *Buffer, 1000)
          MessageRequester("Info", "String: "+PeekS(*Buffer), 0)
        Case #PB_NetworkEvent_File
          MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has send a file via the network !", 0)
          ReceiveNetworkFile(ClientID, "C:\TEST_Network.ftp3")
        Case #PB_NetworkEvent_Disconnect
          MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has closed the connection...", 0)
          Quit = 1
      EndSelect
    EndIf
  Until Quit = 1 
  MessageRequester("PureBasic - Server", "Click to quit the server.", 0)
  CloseNetworkServer(0)
Else
  MessageRequester("Error", "Can't create the server (port in use ?).", 0)
EndIf
End   

Re: Stop one server stealing another server's events..

Posted: Thu Apr 14, 2011 1:13 am
by netmaestro
Purebasic doc for EventServer() wrote:This function is only needed on the server side. It returns the handle of the server which has recived data, allowing multiple servers to be managed on one computer.

Code: Select all

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

Port = 6832
*Buffer = AllocateMemory(1000)

If CreateNetworkServer(0, Port)
  MessageRequester("PureBasic - Server", "Server created (Port "+Str(Port)+").", 0)
  
  Repeat
    SEvent = NetworkServerEvent()
    If SEvent
      If EventServer() = 0
        ClientID = EventClient()
        Select SEvent
          Case #PB_NetworkEvent_Connect
            MessageRequester("PureBasic - Server", "A new client has connected !", 0)
          Case #PB_NetworkEvent_Data
            MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has send a packet !", 0)
            ReceiveNetworkData(ClientID, *Buffer, 1000)
            MessageRequester("Info", "String: "+PeekS(*Buffer), 0)
          Case #PB_NetworkEvent_File
            MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has send a file via the network !", 0)
            ReceiveNetworkFile(ClientID, "C:\TEST_Network.ftp3")
          Case #PB_NetworkEvent_Disconnect
            MessageRequester("PureBasic - Server", "Client "+Str(ClientID)+" has closed the connection...", 0)
            Quit = 1
        EndSelect
      Else
        ; test for another server here and handle its events
        
      EndIf
      
    EndIf
  Until Quit = 1 
  MessageRequester("PureBasic - Server", "Click to quit the server.", 0)
  CloseNetworkServer(0)
Else
  MessageRequester("Error", "Can't create the server (port in use ?).", 0)
EndIf
End   

Re: Stop one server stealing another server's events..

Posted: Thu Apr 14, 2011 11:42 am
by zoot33
I understand that it is possible to run multiple servers from the same application with this type of code. However, how would you run two servers from two separate applications, without one application picking up the other's data ?

Scenario:
There are streams of data arriving on 6832 and 6833. I have two server type applications running, both contain Sevent=NetworkServerEvent() type event loops. When SERVER-A runs the NetworkServerEvent() it picks up whatever is there.. sometimes its own data (6832) and sometimes data meant for the other server on 6833. If the data is meant for itself the SERVER-A processes it. If its not meant for itself, it dumps it !

How is it possible to run two server type applications simutaneously ?

Re: Stop one server stealing another server's events..

Posted: Thu Apr 14, 2011 3:22 pm
by Rook Zimbabwe
You probably aren't going to like this one... it is far too simple.

You arrange to parse each incoming... like add 3 characters to the beginning of every line sent to the server.

Instead of whatever you were sending you send "XX1"+whatever$ for server 1 and "XX2"+whatever$ that would identify server 2

In the servers... you cut away the first three characters of whatever is recieved and if it is the appropriate 3 characters for THAT server the program acts...

OK all that stated why not just combine both servers and build 1 program for the server? :mrgreen:

Re: Stop one server stealing another server's events..

Posted: Thu Apr 14, 2011 3:53 pm
by zoot33
Thanks Rook, I can see that both your ideas would work. I don't have control of the second application (or its source code), so I'd like to find a way of it not getting in the way of a new networked applet that I am about to write. On my provisional tests, it sometimes grabs data meant for my prog.

I would like to understand how commercial programs manage to accept networked data without getting in each other's way. For example a skype call doesn't interfere with watching a u-tube video.

Please give me some pointers,

Thanks.

Re: Stop one server stealing another server's events..

Posted: Thu Apr 14, 2011 4:17 pm
by TomS
That's what ports are for.
If data that's intended to go to one server ends up at another server, there must be an error at the sender.

Re: Stop one server stealing another server's events..

Posted: Thu Apr 14, 2011 8:35 pm
by Rook Zimbabwe
OK then modify it... when you are sending stuff to YOUR server use that "XX1"+ code and when NOT don't... after all the other server won't be able to parse the info so it should be handled by data collection and ignored...

ALSO: add a couple to your created server port... and NAME your server in your program so you can just send to it.