Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
TFN
- Posts: 3
- Joined: Wed Oct 12, 2011 11:55 am
Post
by TFN » Wed Oct 12, 2011 12:01 pm
Hello, I am creating a chat program for a school project. I managed to get it working, however it's not now and I have been trying to fix it for about two weeks and can't find what is wrong with it.
Here is the code :
Server:
Code: Select all
Global pSocket, ClientName, ConnectedClients, TheMessage
Local ClientList
on mouseUp
If the label of me is "Start Server" then
accept connections on port 1995 with message "ClientConnected"
set the label of me to "Stop Server"
else
Stop_Server
end if
end mouseUp
on Accept_Connection pSocket, ConnectedClients
put pSocket & return after ConnectedClients
read from socket pSocket until return \
with message "message_received"
end Accept_Connection
on Stop_Server ConnectedClients
set the label of me to "Start Server"
put empty into ConnectedClients
repeat for each line tSocket in the opensockets
close socket tSocket
end repeat
end Stop_Server
on message_received ConnectedClients, pSocket, TheMessage
repeat for each line tSocket in ConnectedClients
write TheMessage to socket tSocket
end repeat
read from socket pSocket until return \
with message "message_received"
end message_received
on socketClosed pSocket,ConnectedClients
delete line lineoffset(pSocket, ConnectedClients) of ConnectedClients
end socketClosed
on closeStack
Stop_Server
end closeStack
Client:
Code: Select all
global pSocket, TheMessage, ChatSocket
on mouseUp
If the label of me is "Connect" then
ConnectTo
else
DisconnectFrom
end if
end mouseUp
on ConnectTo
if ChatSocket is empty then
open socket field "Host" & ":1995" with message "Accept_Connection"
end if
end ConnectTo
on DisconnectFrom pSocket
write "SocketClosed" to socket pSocket
close socket ChatSocket
put empty into ChatSocket
set the label of button "Connect" to "Connect"
end DisconnectFrom
on chatmessage TheMessage, ChatSocket
If ChatSocket is not empty then
write TheMessage & return to socket ChatSocket
end if
end chatmessage
on ClientConnected
put pSocket into ChatSocket
set label of button "Connect" to "Disconnect"
read from socket ChatSocket until return \
with message "ClientMessageReceived"
end ClientConnected
on ClientMessageReceived pSocket, data
put TheMessage & return after field "ChatWindow"
read from socket ChatSocket until return \
with message "ClientMessageReceived"
end ClientMessageReceived
on SocketError pSocket, pError
put empty into field "log"
put pError & return after field "log"
DisconnectFrom
end SocketError
on socketClosed
put empty into ChatSocket
end socketClosed
on closeStack
DisconnectFrom
end closeStack
And thanks for helping in advance
-
mwieder
- VIP Livecode Opensource Backer

- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
-
Contact:
Post
by mwieder » Wed Oct 12, 2011 5:45 pm
Well, at first glance you've got the client and server code mixed into each other a bit. Try these changes:
Server:
Code: Select all
on mouseUp
If the label of me is "Start Server" then
accept connections on port 1995 with message "Accept_Connection"
set the label of me to "Stop Server"
else
Stop_Server
end if
end mouseUp
Client:
Code: Select all
on ConnectTo
if ChatSocket is empty then
open socket field "Host" & ":1995" with message "ClientConnected"
end if
end ConnectTo
The message in the accept or open socket commands is a callback handler that will be triggered at the same end of the conversation when the appropriate event occurs. In other words, the Accept_Connection handler will be triggered on the server when a client connects and the ClientConnected handler will be triggered in the Client when the socket connection to the server is complete, rather than being a "message" that is sent to the other computer.
-
TFN
- Posts: 3
- Joined: Wed Oct 12, 2011 11:55 am
Post
by TFN » Thu Oct 13, 2011 10:22 am
Thanks for the reply, however the program still doesn't work. I can connect to the server but I can't send a message and when I disconnect I can't reconnect to the same server.
-
TFN
- Posts: 3
- Joined: Wed Oct 12, 2011 11:55 am
Post
by TFN » Wed Nov 02, 2011 1:29 pm
I fixed the problem. I just found a older working version of the program and built on that.
However now I am trying to use 2D arrays and was wondering if it were possible to pass the contents of one Array to another using sockets. I am making a list of clients which the Client to see who is connected on the server and sort and search on the list.
Thanks in advance
TFN
-
mwieder
- VIP Livecode Opensource Backer

- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
-
Contact:
Post
by mwieder » Wed Nov 02, 2011 5:11 pm
As long as you're not talking about multidimensional arrays, check out the combine and split commands. On the sending end, combine will turn the array into a list, which makes sending it to the other side trivial; and on the receiving end, split turns it back into an array.
But since I notice that you mention 2D arrays you might try (warning: untested) base64encoding the array and sending the result, then base64decoding it on the receiving end.
-
Alan Howard
- Posts: 1
- Joined: Fri May 09, 2014 4:17 am
Post
by Alan Howard » Fri May 09, 2014 4:19 am
TFN wrote:Hello, I am creating a chat program for a school project. I managed to get it working, however it's not now and I have been trying to fix it for about two weeks and can't find what is wrong with it by e cig.
Here is the code :
Server:
Code: Select all
Global pSocket, ClientName, ConnectedClients, TheMessage
Local ClientList
on mouseUp
If the label of me is "Start Server" then
accept connections on port 1995 with message "ClientConnected"
set the label of me to "Stop Server"
else
Stop_Server
end if
end mouseUp
on Accept_Connection pSocket, ConnectedClients
put pSocket & return after ConnectedClients
read from socket pSocket until return \
with message "message_received"
end Accept_Connection
on Stop_Server ConnectedClients
set the label of me to "Start Server"
put empty into ConnectedClients
repeat for each line tSocket in the opensockets
close socket tSocket
end repeat
end Stop_Server
on message_received ConnectedClients, pSocket, TheMessage
repeat for each line tSocket in ConnectedClients
write TheMessage to socket tSocket
end repeat
read from socket pSocket until return \
with message "message_received"
end message_received
on socketClosed pSocket,ConnectedClients
delete line lineoffset(pSocket, ConnectedClients) of ConnectedClients
end socketClosed
on closeStack
Stop_Server
end closeStack
Client:
Code: Select all
global pSocket, TheMessage, ChatSocket
on mouseUp
If the label of me is "Connect" then
ConnectTo
else
DisconnectFrom
end if
end mouseUp
on ConnectTo
if ChatSocket is empty then
open socket field "Host" & ":1995" with message "Accept_Connection"
end if
end ConnectTo
on DisconnectFrom pSocket
write "SocketClosed" to socket pSocket
close socket ChatSocket
put empty into ChatSocket
set the label of button "Connect" to "Connect"
end DisconnectFrom
on chatmessage TheMessage, ChatSocket
If ChatSocket is not empty then
write TheMessage & return to socket ChatSocket
end if
end chatmessage
on ClientConnected
put pSocket into ChatSocket
set label of button "Connect" to "Disconnect"
read from socket ChatSocket until return \
with message "ClientMessageReceived"
end ClientConnected
on ClientMessageReceived pSocket, data
put TheMessage & return after field "ChatWindow"
read from socket ChatSocket until return \
with message "ClientMessageReceived"
end ClientMessageReceived
on SocketError pSocket, pError
put empty into field "log"
put pError & return after field "log"
DisconnectFrom
end SocketError
on socketClosed
put empty into ChatSocket
end socketClosed
on closeStack
DisconnectFrom
end closeStack
And thanks for helping in advance
I also having those same problem. how can I get rid from this. Please give me some instruction about this.
-
Simon
- VIP Livecode Opensource Backer

- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
Post
by Simon » Fri May 09, 2014 4:29 am
Hi Alan,
What have you tried?
Did you make the recommended changes?
Why did you change the quote to include "e cig"?
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
-
FourthWorld
- VIP Livecode Opensource Backer

- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
-
Contact:
Post
by FourthWorld » Fri May 09, 2014 4:47 am
TFN wrote:I am trying to use 2D arrays and was wondering if it were possible to pass the contents of one Array to another using sockets.
Arrays are my favorite data format when communicating between client and server apps both made in LC. You can run an array through the arrayEncode function to turn it into a binary stream for sending, and convert the stream back into an array on the other side with arrayDecode.
-
SparkOut
- Posts: 2943
- Joined: Sun Sep 23, 2007 4:58 pm
Post
by SparkOut » Mon May 12, 2014 10:50 am
Why did you change the quote to include "e cig"?
Call me suspicious but maybe in a failed spam planting attempt?
-
Simon
- VIP Livecode Opensource Backer

- Posts: 3901
- Joined: Sat Mar 24, 2007 2:54 am
Post
by Simon » Mon May 12, 2014 8:31 pm
Yeah, single login at that change...
I was just having fun.
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!