Sockets server multithreaded?

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
marcioalex
Posts: 1
Joined: Sat Feb 20, 2010 7:11 pm

Sockets server multithreaded?

Post by marcioalex » Sat Feb 20, 2010 7:15 pm

Hi,

I'm wondering if I build a Rev Sockets Server application it will handle multiple incoming connections at the same time or I have to do any special handling with it.

If it handles it automatically, may I use one single connection to the database and run queries from the connection handling calls or I should open and close the connection for each incoming connection?

Does anyone knows about it?

Thanks.

Marcio Alexandroni.

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Re: Sockets server multithreaded?

Post by Janschenkel » Fri Feb 26, 2010 2:56 pm

The only 'multi-threaded' part is the raw socket communication; but each request will be handled sequentially.
So when you use a script like the following echo server example

Code: Select all

constant kPORT = 12345
local sRunning, sSockets
on mouseUp
  if sRunning is not "true" then
    accept connections on port kPORT with message "NewConnection"
    put true into sRunning
  else
    close socket kPORT
    repeat for each line tSocket in sSockets
      if tSocket is among the lines of the openSockets then
        close socket tSocket
      end if
    end repeat
    put empty into sSockets
    put false into sRunning
  end if
end mouseUp

on NewConnection pSocket
  if pSocket is not among the lines of sSockets then
    put return & pSocket after sSockets
  end if
  read from socket pSocket until empty with message "ReadChunk"
end NewConnection

on ReadChunk pSocket, pData
  write pData to socket pSocket with message "WroteChunk"
end ReadChunk

on WroteChunk pSocket
  read from socket pSocket until empty with message "ReadChunk"
end WroteChunk
Then no 'NewConnection', 'ReadChunk' and 'WroteChunk' event will be handled at the same time; but while the engine is reading from or writing to a socket in this way, other events can be processed. So there is a limited form of multi-threading when using socket communication. While this won't scale to thousands of simultaneous connections when your server logic needs to tap into databases, it should work fine for 'workgroup' applications.

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Post Reply