A question about Server Socket !
- skinkairewalker
- Enthusiast
- Posts: 636
- Joined: Fri Dec 04, 2015 9:26 pm
A question about Server Socket !
Hello everyone, I would like to know how the server acts in multiconnections: When 2 users connect to a socket server and send a text message: "Login: USERNAME: PASSWORD", server receive both messages, the first connected and sent to message runs the functions first, and the second waits in the queue until the server runs?
Re: A question about Server Socket !
A server uses threads.
- skinkairewalker
- Enthusiast
- Posts: 636
- Joined: Fri Dec 04, 2015 9:26 pm
Re: A question about Server Socket !
so if I want to initialize a mysql connection for each data request, I should do this:infratec wrote:A server uses threads.
Code: Select all
SEvent = NetworkServerEvent()
If SEvent
ClientID = EventClient()
Select SEvent
Case #PB_NetworkEvent_Data
ConsoleColor(14,0)
PrintN("New Message From ["+Str(ClientID)+"]")
ReceiveNetworkData(ClientID, *RcvMsg, 1000)
PrintN("Received Msg : "+PeekS(*RcvMsg, -1, #PB_UTF8))
RcvMsg = PeekS(*RcvMsg, -1, #PB_UTF8)
FillMemory(*RcvMsg,65536)
If (StringField(RcvMsg,1,":") = "LogIn")
TempMySqlConn.i = OpenDatabase(#PB_Any, "host=MyServer.com port=3306 dbname=Mydb", "userdb", "XXdjdya")
If TempMySqlConn <> 0
PrintN("[MySQL] Database connected !")
If (DatabaseQuery(.........)
..........
endif
Else
ConsoleColor(12,0)
PrintN("[MySQL] Erro in connect on Database.")
End
EndIf
EndIf
Until XX = XX
or can i set ' All MySql Connection ' id to ' 0 ' like this :
Code: Select all
OpenDatabase(0, "host=MyServer.com port=3306 dbname=Mydb", "userdb", "XXdjdya")
Re: A question about Server Socket !
Yes up to a point, the socket buffer space isn't unlimited. It is possible to overflow the receive buffer if there is a lot of unprocessed incoming data. If this happens further received data will be dropped by the TCP/IP stack because it has nowhere to put it.skinkairewalker wrote:Server receive both messages, the first connected and sent to message runs the functions first, and the second waits in the queue until the server runs?
The example you show is serial. The database connection and query are both triggered by the network event and the network response loop pauses until the query is complete. This set up could be more vulnerable to overflow problems than threaded processing, depending on work load. (Threaded processing is more difficult to design, program and, especially, debug though).
Opening successive connections on a single ID will probably be a bad idea unless you have a matching CloseDatabase; I would expect it to lead to a memory leak in your program.skinkairewalker wrote:or can i set ' All MySql Connection ' id to ' 0 ' like this :??Code: Select all
OpenDatabase(0, "host=MyServer.com port=3306 dbname=Mydb", "userdb", "XXdjdya")
1) Each concurrent database connection open will occupy some RAM on the server. Server memory management is quite complex so it's difficult to say exactly how much; but generally more connections = more RAM. Too many connections open will deprive the system of RAM and cause trouble elsewhere. How many connections is too many will depend on how much RAM your server has, what other duties it performs and what the underlying operating system is.skinkairewalker wrote:so if I want to initialize a mysql connection for each data request, I should do this:
2) You may need to implement some kind of marshalling, although exactly what you need depends on the circumstances your process will encounter.
It could range from low concurrency (a low number of simultaneous users), low demand (not making many database queries), low query execution time (queries end quickly) through to high concurrency (many simultaneous users), high demand (making queries all the time), high execution time (queries are slower to finish).
At the low end of the scale you might be able to satisfy demand with a single thread and connection, however once the connection becomes saturated users will end up having to wait for the system to catch up and/or will experience problems. If this is the case then you will need multiple threads and connections to keep up with demand and will need some kind of marshalling.
'Env' wrote a module for load balanced worker threads. I haven't tried it out but it looks promising, you might gain something from it if you decide on multi-threading. My only reservation being that he uses KillThread to shut down threads. As you are working with databases you would need to make alternate arrangements to allow your threads to self-terminate properly to make sure that transactions get committed properly and connections are closed properly. See viewtopic.php?f=12&t=72247