Page 1 of 1

Sockets, help a newbie?

Posted: Fri Jul 05, 2013 8:04 am
by trenatos

This is my first post on the forum so I guess I'll introduce myself.
I'm Marcus, I'm a CFML(Web) programmer wanting to get into desktop software.

Right now I'm looking into creating a simple chat program that connects to one of my servers using sockets.

The server listens and simply echoes whatever is typed at it, and creates a local file with the echoed characters just for testing.
The server is confirmed to work using telnet.

But I can't get LiveCode to connect or send anything.

I'm looking at one of the lessons (Sorry, I can apparently not post links)

But I just can't get it to work.

Could someone write me a simple example to open a socket to (Running a local test-server) and write a hardcoded string to the socket?

I would greatly appreciate the help.

Re: Sockets, help a newbie?

Posted: Fri Jul 05, 2013 8:39 am
by Newbie4
The server piece would have a field for the messages (text) and a start button (start) with this code:

button: Start

Code: Select all

on mouseUp
  accept connections on port 9001 with message "someoneConnectedToMe"
end mouseUp

on someoneConnectedToMe theirSocket
   read from socket theirSocket until return with message "newMessage"
end someoneConnectedToMe

on newMessage theirSocket  theMessage
   put theirSocket & ":" && theMessage & return after field "text"
   read from socket "theirSocket until return with message "newMessage"
end newMessage
In your case, you would use "localhost:9001" as the server address (servAddr). Type that into the field and press the start button. The messages will shot in the text field.

The Client piece would have a field for the server address (servAddr), a field for the message (message) and 2 buttons (one to "connect"' and one to "send")

button: Connect

Code: Select all

global servAddr
on mouseUp
  put field "servAddr" into servAddr
  open socket to servAddr
  if the Result <> "" then
    put "result:" && the result
  end if
end mouseUp
button: Send

Code: Select all

global servAddr
on mouseUp
  write field "message" & return to socket servAddr
end mouseUp
You enter the server address into the servAddr and press the connect button. After that type each message in the message field and press the send button
This is as simple as it can get. I hope this helps you get started

Re: Sockets, help a newbie?

Posted: Fri Jul 05, 2013 9:01 am
by trenatos
Thank you very much!

The LiveCode app not sends a message and I can see it's being received by the server.

But I can't figure out the reading end.

First off, you mention port 9001, don't you mean 2001?

Re: Sockets, help a newbie?

Posted: Fri Jul 05, 2013 9:06 am
by trenatos

I thought sockets were bi-directional, and nothing happened.
But when I set up the server to respond on port 9001, it seems to have worked just fine! (At least a preliminary test)

Thanks Newbie4, I owe you a beer :)

Re: Sockets, help a newbie?

Posted: Fri Jul 05, 2013 12:57 pm
by Newbie4
Sorry, I just picked port 9001 to use. I did not realize that you were using 2001. (It does not matter which port you use. Just as long as it does not conflict with one in use on your computer).

I don't think that you can do this as easily in any other language. In fact some of my student wrote such a program and others went on to write the start of a multi-computer, multi-player game. I will put up their code soon so that you can see how they did it.

Anyway, I am glad that you got it working.
and thanks for the beer!

Re: Sockets, help a newbie?

Posted: Fri Jul 05, 2013 5:55 pm
by trenatos
I'm using it together with one of my ColdFusion servers, to learn I'll be writing a chat application that syncs with an online chat I built.
And next up will probably be a file-synching tool.

In extension I'd love to look at creating some games too.

I'm impressed with LiveCode so far.

I've messed with Delphi/Lazarus in the past, and LiveCode feels like a simpler-to-get-going version of Delphi.

Re: Sockets, help a newbie?

Posted: Sun Jul 07, 2013 2:00 am
by trenatos
Playing some more witch sockets, I've now run into a buffer issue.
Over the course of connecting/disconnecting and sending a few messages, it seems LiveCode messes up the buffer.
I've checked my server code and it's sending the correct text, but LC seems to not be updating the buffer properly.

Is there a clear buffer command I can use?

Can't find anything in the help file.