Page 1 of 1

Chat application not working

Posted: Wed Oct 12, 2011 12:01 pm
by TFN
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

Re: Chat application not working

Posted: Wed Oct 12, 2011 5:45 pm
by mwieder
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.

Re: Chat application not working

Posted: Thu Oct 13, 2011 10:22 am
by TFN
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.

Re: Chat application not working

Posted: Wed Nov 02, 2011 1:29 pm
by TFN
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

Re: Chat application not working

Posted: Wed Nov 02, 2011 5:11 pm
by mwieder
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.

Re: Chat application not working

Posted: Fri May 09, 2014 4:19 am
by Alan Howard
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.

Re: Chat application not working

Posted: Fri May 09, 2014 4:29 am
by Simon
Hi Alan,
What have you tried?
Did you make the recommended changes?

Why did you change the quote to include "e cig"?

Re: Chat application not working

Posted: Fri May 09, 2014 4:47 am
by FourthWorld
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.

Re: Chat application not working

Posted: Mon May 12, 2014 10:50 am
by SparkOut
Why did you change the quote to include "e cig"?
Call me suspicious but maybe in a failed spam planting attempt?

Re: Chat application not working

Posted: Mon May 12, 2014 8:31 pm
by Simon
Yeah, single login at that change...
I was just having fun.

Simon