Posted: Wed Jan 10, 2007 11:43 pm Post subject: Networked Instant Messenger, Multi User Help.
Alright, I've been working on this for a fair while and it was working properly until I hit a bit of a bump. The messenger wasn't multi user, so I have now attempted to make it able to run up to 10 users. For some reason it is not working, and if anyone would help me out I would be very greatful. It's probably something very simple but I cannot put my finger on it. The error you get is that it the stream is found to have no value when it goes to retrieve a message and prior to adding the arrays, it worked with a singlr stream.
put "Host Address: "
get strNetAddress
stream (1) := Net.OpenConnection (strNetAddress, port)
if stream (1) <= 0 then
put "Failure connecting to host, ", strNetAddress
return
else
put "Connected to ", strNetAddress
end if
SERVER := false
GUI.Quit
end join
process chat
loop
if resetcounter >= 6 then
clearlines
locate (1, 1)
resetcounter := 0
end if
loop
port := port + USERS
if USERS < 10 then
stream (USERS) := Net.WaitForConnection (port, strNetAddress)
USERS := USERS + 1
end if
end loop
end NewClients
process recieve
loop
for i : 1 .. 10
if Net.CharAvailable (stream (i)) then
get : stream (i), strmessagecloak : *
if strmessagecloak not= "" then
GUI.AddLine (inttextbox, strmessagecloak)
end if
end if
end for
end loop
end recieve
put "Welcome to the Mike's chat"
put "Please enter your name: "
get strname : *
port := port + USERS
cls
GUI.Refresh
startframe := GUI.CreateLabelledFrame (10, 390, 293, 300, GUI.INDENT, "Chat")
Hosty := GUI.CreateLabel (55, 360, "Click the Host button to host a chat.")
Joiny := GUI.CreateLabel (55, 370, "Click the join button to join a chat.")
Host := GUI.CreateButton (55, 330, 0, "Host Chat", host)
Join := GUI.CreateButton (160, 330, 0, "Join Chat", join)
loop
exit when GUI.ProcessEvent
end loop
cls
GUI.Refresh
loop
delay (2000)
fork chat
fork recieve
if SERVER = true then
fork NewClients
end if
end loop
Sponsor Sponsor
rdrake
Posted: Wed Jan 10, 2007 11:55 pm Post subject: Re: Networked Instant Messenger, Multi User Help.
I recken you will need to write a quick server application. The server will accept connections from the clients, and receive messages from the clients. Once a message is received, the server will send the message out to all clients.
Doing this in a centralized way will simplify things greatly.
Miketron
Posted: Thu Jan 11, 2007 9:51 am Post subject: Re: Networked Instant Messenger, Multi User Help.
Is it possible to work through an individual client like this? Or do I absolutely NEED to have a server application aswell? I've seen coding for a server based messenger, but I would prefer to have it in a single client that can act as a message window aswell as the actual server.
rdrake
Posted: Thu Jan 11, 2007 10:29 am Post subject: Re: Networked Instant Messenger, Multi User Help.
It is possible, you just need to have a client designate itself as the server. This way it still relays the messages to other clients, but it also acts as a client in the process.
A centralized server-based method would be much simpler, though it is possible to do otherwise.
Miketron
Posted: Thu Jan 11, 2007 10:53 am Post subject: Re: Networked Instant Messenger, Multi User Help.
Alright, I have taken your advice and have started making a small server based messenger but in the process of cleaning up the client, I have hit yet another pot hole. When I added GUI text fields, it now will not let me type. The code is as follows
I have absolutely idea why it calls a procedure that does nothing at all...
Just a few other nitpicks:
Variable names and method/procedure/function names start with a lowercase letter. They can either be in [c]thisStyle[/c] or [c]this_style[/c], it's your choice. Just keep your choice consistant. Names of classes usually start with capital letters, though.
Please indent your code. If you press F2, Turing will do it for you.
[code ] and [/code ] tags are an excellent idea, minus the spaces of course.
Sorry I can't help more, like I said, Turing gives me an "Access Violation" error when I try running your code.
Ultrahex
Posted: Thu Jan 11, 2007 9:20 pm Post subject: Re: Networked Instant Messenger, Multi User Help.
Ok it appears as though line 26 is causing the error
if you comment them the program works fine... (well except that textbox is not there)
it appears as though you cant create the CreateTextBoxFull before your GUI.CreateTextFieldFull ... im not sure why this is, hopefully someone could figure this out in turing.
Hope This Helps.
Miketron
Posted: Thu Jan 11, 2007 10:21 pm Post subject: Re: Networked Instant Messenger, Multi User Help.
Ah thank you for the help, I just loaded the text boxes after the buttons and it now works.
P.S My teacher doesn't know anything about the network functions of turing, and more than basic GUI. For some reason the program skips the second button, likely assuming that it has processed it's event.
%--------Networking-----------
var netaddress : string
var stream : array 1 .. 10 of int
%--------Message--------------
var strmessage : string
var strmessagetemp : string
var strmessagecloak : string
%--------Names----------------
var strname : string
%--------MessageBox--------------
var inttextbox : int
var inttitle : int
%--------Frame----------------
var startframe : int
var joiny : int
var join : int
var joiny2 : int
var join2 : int
var ip1 : int
var ip2 : int
var ipl : int
var ipsend : int
%--------MessageReset---------
var resetcounter : int
resetcounter := 0
procedure clearlines
locate (1, 1)
put "" : 40
locate (2, 1)
put "" : 40
locate (3, 1)
put "" : 40
locate (4, 1)
put "" : 40
locate (5, 1)
put "" : 40
locate (6, 1)
put "" : 40
end clearlines
procedure DoNothing (text : string)
end DoNothing
procedure ipz
netaddress := GUI.GetText (ip2)
GUI.Disable (ip2)
end ipz
procedure name
strname := GUI.GetText (ip1)
GUI.Quit
end name
procedure Join
GUI.Dispose (joiny)
GUI.Dispose (join)
joiny := GUI.CreateLabel (55, 370, "Please enter the ip below, and press enter.")
ip2 := GUI.CreateTextFieldFull (105, 345, 100, "", DoNothing, GUI.INDENT, 0, 0)
ipsend := GUI.CreateButton (125, 310, 0, "Enter", ipz)
joiny2 := GUI.CreateLabel (55, 370, "Click the join button to join a chat.")
join2 := GUI.CreateButton (105, 330, 0, "Join Chat", Join)
loop
exit when GUI.ProcessEvent
end loop
cls
GUI.Refresh
loop
delay (2000)
fork chat
fork recieve
end loop
Sponsor Sponsor
Ultrahex
Posted: Thu Jan 11, 2007 11:00 pm Post subject: Re: Networked Instant Messenger, Multi User Help.
Ok, i was going through your code a little bit, and there is some things you need to fix up cause of this bug, it will happen again with the code you have in your Join procedure. what you need to do is make all your buttons and all that GUI at one point in the program, then hide and show your GUI buttons. This is probably the easier way to get out of this bug happening.
also when you Dispose of stuff Remeber use the following code after:
code:
cls %Clears Screen
GUI.Refresh %Refreshes GUI
GUI.ResetQuit %Turns GUI.ProcessEvent back to False
(BTW next time you paste your code you could use the tags, when you post just hit more tags, then its code and hit code again to close
Here is your code a little bit farther:
%--------Networking-----------
var netaddress : string
var stream : array 1 .. 10 of int
%--------Message--------------
var strmessage : string
var strmessagetemp : string
var strmessagecloak : string
%--------Names----------------
var strname : string
%--------MessageBox--------------
var inttextbox : int
var inttitle : int
%--------Frame----------------
var startframe : int
var joiny : int
var join : int
var joiny2 : int
var join2 : int
var ip1 : int
var ip2 : int
var ipl : int
var ipsend : int
%--------MessageReset---------
var resetcounter : int
resetcounter := 0
procedure clearlines
locate (1, 1)
put "" : 40
locate (2, 1)
put "" : 40
locate (3, 1)
put "" : 40
locate (4, 1)
put "" : 40
locate (5, 1)
put "" : 40
locate (6, 1)
put "" : 40
end clearlines
procedure DoNothing (text : string)
end DoNothing
procedure ipz
netaddress := GUI.GetText (ip2)
GUI.Disable (ip2)
end ipz
procedure name
strname := GUI.GetText (ip1)
GUI.Quit
end name
procedure Join
GUI.Dispose (joiny2)
GUI.Dispose (join2)
ip2 := GUI.CreateTextFieldFull (105, 345, 100, "", DoNothing, GUI.INDENT, 0, 0)
ipsend := GUI.CreateButton (125, 310, 0, "Enter", ipz)
joiny := GUI.CreateLabel (55, 370, "Please enter the ip below, and press enter.")
cls
GUI.Refresh
GUI.ResetQuit
loop
exit when GUI.ProcessEvent
end loop
joiny2 := GUI.CreateLabel (55, 370, "Click the join button to join a chat.")
join2 := GUI.CreateButton (105, 330, 0, "Join Chat", Join)
cls
GUI.Refresh
GUI.ResetQuit
loop
exit when GUI.ProcessEvent
end loop
cls
GUI.Refresh
GUI.ResetQuit
loop
delay (2000)
fork chat
fork recieve
end loop
Miketron
Posted: Fri Jan 12, 2007 11:46 am Post subject: Re: Networked Instant Messenger, Multi User Help.
Alright, I got it working. Had to find myself another version of turing just to use the .ResetQuit function
Miketron
Posted: Wed Jan 17, 2007 9:33 am Post subject: Re: Networked Instant Messenger, Multi User Help.
I am now having problems adding more users to the chat. In this process below it should be waiting until someone connects to the server before it continues, but it doesn't seem to be.
Posted: Wed Jan 17, 2007 9:33 am Post subject: Re: Networked Instant Messenger, Multi User Help.
I am now having problems adding more users to the chat. In this process below it should be waiting until someone connects to the server before it continues, but it doesn't seem to be.
Posted: Wed Jan 17, 2007 10:07 am Post subject: Re: Networked Instant Messenger, Multi User Help.
Why are you using the following code...?
code:
for i : 3 .. 11
% ...
end for
Couldn't you just get rid of this, or is there a particular problem you use this to work around?
Also, constants (ie. USERS) do not normally change. Consider changing its casing (something along the line of users would do nicely).
Miketron
Posted: Wed Jan 17, 2007 12:19 pm Post subject: Re: Networked Instant Messenger, Multi User Help.
code:
for i : 3 .. 11
% ...
end for
it's to limit the amount of people in the chat, and it's needed to change the name of the different net addresses.
Miketron
Posted: Thu Jan 18, 2007 9:18 am Post subject: Re: Networked Instant Messenger, Multi User Help.
For some reason the Adding New Clients process isn't working properly and it isn't waiting for the connection. After the first connection to I have to reset it back to waiting?
code:
var port : int := 3000
%--------Networking-----------
var netAddress : array 2 .. 11 of string
var stream : array 2 .. 11 of int
for i : 2 .. 11
stream (i) := 0
end for
var intchoice : int
var strchoice : string (1)
%--------Message--------------
var strmessage : string
var strmessagetemp : string
var strmessagecloak : string
%--------Names----------------
var strname : string
%--------Server---------------
var USERS : int := 1
procedure host
put "Chat started ", Net.LocalAddress
put "Waiting for connection..."
stream (2) := Net.WaitForConnection (port, netAddress (2))
USERS := USERS + 1