Chat application not working
Posted: 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:
Client:
And thanks for helping in advance
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
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