Sockets : good practices for opening and closing ?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Sockets : good practices for opening and closing ?

Post by bangkok » Fri Apr 02, 2010 11:22 am

I have a server and 2 clients, using sockets.

Very simple application : a client sends a query to the server, the server sends back a result.

My question is : what are the good practices regarding opening/closing socket on the client side ?

Is it better to close the socket after each read commands ?

In other words.
For the client :
-for each query, I open

Code: Select all

open socket to fld "ip" with message connected
-I write the query
-then i read the result sent by the server
-and then I should

Code: Select all

 close socket theIP
?

Or is it better to close socket on the server side ?
Last edited by bangkok on Fri Apr 02, 2010 1:32 pm, edited 1 time in total.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Sockets : good practices for opening and closing ?

Post by Mark » Fri Apr 02, 2010 1:12 pm

bangkok,

No, you keep the socket open until you don't need it anymore. In some cases, you might want to keep the socket open forever and check whether the client is still on-line once in a while.

For a chat application, it is useful to keep the connection open, because opening and closing the socket each time might be too slow. If you just want to transfer one file, you might want to keep the socket open until the data transfer has finished.

In Clipboard Link I open a datagram socket to send a message and close it after writing to it. The other copies of Clipboard Link on the same network receive the message and and close the socket too. They return a reply over another datagram socket, which is also closed immediately. This way, I keep the needed network capacity minimised, while it isn't a problem if the message doesn't always arrive.

Whenever I want to transfer a clipboard to another computer, I open a normal socket, because I want it to be stable and I want to be sure that the data is transferred correctly (TCP assures this, if using a normal socket, but not if using a datagram socket). I close the socket after the transfer, because I don't need it anymore.

You should write your message "connected" in quotes. You also need a handler "connected", which does the writing. Then you follow with a read command with another message. On the client side, it should look like this:

Code: Select all

on foo
  put "10.0.0.27:8808" into mySock
  open socket to mySock with message "connected"
 end foo

on connected theSock
  write "Hello World" & cr to socket theSock
  read from socket theSock until cr with message "bar"
end connected

on bar theSock,theData
  put theData into fld "Some Field"
  close socket theSock
end bar
Best regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Sockets : good practices for opening and closing ?

Post by bangkok » Fri Apr 02, 2010 3:34 pm

Thanks Mark.

Post Reply