Sockets, help a newbie?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Sockets, help a newbie?

Post by trenatos » Fri Jul 05, 2013 8:04 am

Greetings!

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 127.0.0.1 (Running a local test-server) and write a hardcoded string to the socket?

I would greatly appreciate the help.
Marcus

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 280
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: Sockets, help a newbie?

Post by Newbie4 » Fri Jul 05, 2013 8:39 am

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
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Sockets, help a newbie?

Post by trenatos » Fri Jul 05, 2013 9:01 am

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?
Marcus

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Sockets, help a newbie?

Post by trenatos » Fri Jul 05, 2013 9:06 am

Nevermind!

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 :)
Marcus

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 280
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: Sockets, help a newbie?

Post by Newbie4 » Fri Jul 05, 2013 12:57 pm

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!
Cyril
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Sockets, help a newbie?

Post by trenatos » Fri Jul 05, 2013 5:55 pm

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.
Marcus

trenatos
Posts: 189
Joined: Wed Jul 03, 2013 6:46 am
Location: Virginia, USA
Contact:

Re: Sockets, help a newbie?

Post by trenatos » Sun Jul 07, 2013 2:00 am

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.
Marcus

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”