Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Networked Instant Messenger, Multi User Help.
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Miketron




PostPosted: 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.



import GUI
setscreen ("graphics:400;500,nobuttonbar,inttitle:Turing Messenger")
View.Update
colourback (54)
cls

var port : int
port := 5505

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

%--------Networking-----------
var strNetAddress : string
var stream : array 1 .. 10 of int
var intchoice : int
var strchoice : string (1)

%--------Message--------------
var strmessage : string
var strmessagetemp : string
var strmessagecloak : string

%--------Names----------------
var strname : string
var strstreamname : string

%--------TextBox--------------
var inttextbox : int
var inttitle : int

%--------MessageBox-----------
inttitle := GUI.CreateLabelFull (20, 280, "Messages", 250, 0, GUI.CENTER, 0)
inttextbox := GUI.CreateTextBoxFull (10, 10, 280, 265, GUI.INDENT, 0)

%--------Frame----------------
var startframe : int
var Hosty : int
var Joiny : int
var Host : int
var Join : int

%--------Server---------------
var servertitle : int
var servertextbox : int

servertitle := GUI.CreateLabelFull (330, 280, "Servers", 150, 0, GUI.CENTER, 0)
servertextbox := GUI.CreateTextBoxFull (320, 10, 180, 265, GUI.INDENT, 0)

var SERVER : boolean
var USERS : int := 0

%--------MessageReset---------
var resetcounter : int
resetcounter := 0

procedure DoNothing (text : string)
end DoNothing

procedure host

GUI.Dispose (startframe)
GUI.Dispose (Hosty)
GUI.Dispose (Joiny)
GUI.Dispose (Host)
GUI.Dispose (Join)
cls
GUI.Refresh

put "Chat started", Net.LocalAddress
put "Waiting for connection..."
stream (1) := Net.WaitForConnection (port, strNetAddress)
USERS := USERS + 1
strstreamname := strname
SERVER := true
GUI.Quit

end host

procedure join

GUI.Dispose (startframe)
GUI.Dispose (Hosty)
GUI.Dispose (Joiny)
GUI.Dispose (Host)
GUI.Dispose (Join)
cls
GUI.Refresh

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

get strmessage : *

resetcounter := resetcounter + 1

if strmessage not= "" then

strmessagetemp := strmessage

put : stream (1), strname, ": ", strmessagetemp

strmessagecloak := strname + ": " + strmessagetemp

GUI.AddLine (inttextbox, strmessagecloak)

end if
end loop

end chat

process NewClients

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
Sponsor
sponsor
rdrake




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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


import GUI
setscreen ("graphics:400;500,nobuttonbar,inttitle:Turing Messenger")
View.Update
colourback (54)
cls

var port : int := 5505

%--------Networking-----------
var strNetAddress : string
var stream : array 1 .. 10 of int

%--------Message--------------
var strmessage : string
var strmessagetemp : string
var strmessagecloak : string

%--------Names----------------
var strname : string
var strstreamname : string

%--------MessageBox--------------
var inttextbox : int
var inttitle : int
inttitle := GUI.CreateLabelFull (20, 280, "Messages", 250, 0, GUI.CENTER, 0)
inttextbox := GUI.CreateTextBoxFull (10, 10, 280, 265, GUI.INDENT, 0)

%--------Frame----------------
var startframe : int
var Joiny : int
var Join : int

%--------Server---------------
var USERS : int := 0
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
strNetAddress := GUI.GetText (ip2)
GUI.Disable (ip2)
end ipz

procedure name
strname := GUI.GetText (ip1)
GUI.Disable (ip1)

end name

procedure join

GUI.Dispose (Joiny)
GUI.Dispose (Join)
cls
GUI.Refresh
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)

loop
exit when GUI.ProcessEvent
end loop

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

GUI.Quit

end join

process chat

loop

if resetcounter >= 6 then
clearlines
locate (1, 1)
resetcounter := 0
end if

get strmessage : *

resetcounter := resetcounter + 1

if strmessage not= "" then

strmessagetemp := strmessage

put : stream (1), strname, ": ", strmessagetemp

strmessagecloak := strname + ": " + strmessagetemp

GUI.AddLine (inttextbox, strmessagecloak)

end if
end loop

end chat

process NewClients

loop
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

startframe := GUI.CreateLabelledFrame (10, 390, 293, 300, GUI.INDENT, "Chat")
Joiny := GUI.CreateLabel (55, 370, "Welcome to the chat, Enter your name.")
ip1 := GUI.CreateTextFieldFull (105, 345, 100, "", DoNothing, GUI.INDENT, 0, 0)
ipsend := GUI.CreateButton (125, 310, 0, "Enter", name)

loop
exit when GUI.ProcessEvent
end loop

cls
GUI.Refresh
Joiny := GUI.CreateLabel (55, 370, "Click the join button to join a chat.")
Join := 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
rdrake




PostPosted: Thu Jan 11, 2007 8:46 pm   Post subject: Re: Networked Instant Messenger, Multi User Help.

Unfortunately Turing (4.0.5c) crashes when I try to run your code. Though I suspect the issue has something to do with the following line.
code:
ip2 := GUI.CreateTextFieldFull (105, 345, 100, "", DoNothing, GUI.INDENT, 0, 0)
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




PostPosted: 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

Turing:

inttextbox := GUI.CreateTextBoxFull (10, 10, 280, 265, GUI.INDENT, 0)


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




PostPosted: 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.

Thank you all again for the help.

code:

import GUI
setscreen ("graphics:400;500,nobuttonbar,inttitle:Turing Messenger")
View.Update
colourback (54)
cls

var port : int := 5505

%--------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)

    loop
        exit when GUI.ProcessEvent
    end loop

    stream (1) := Net.OpenConnection (netaddress, port)

    if stream (1) <= 0 then
        put "Failure connecting to host, ", netaddress
        return
    else
        put "Connected to ", netaddress
    end if

    GUI.Quit

end Join

process chat

    loop

        if resetcounter >= 6 then
            clearlines
            locate (1, 1)
            resetcounter := 0
        end if

        get strmessage : *

        resetcounter := resetcounter + 1

        if strmessage not= "" then

            strmessagetemp := strmessage

            put : stream (1), strname, ": ", strmessagetemp

            strmessagecloak := strname + ": " + strmessagetemp

            GUI.AddLine (inttextbox, strmessagecloak)

        end if
    end loop

end chat


process recieve

    loop

        for i : 1 .. 10

            get : stream (i), strmessagecloak : *

            if strmessagecloak not= "" then

                GUI.AddLine (inttextbox, strmessagecloak)

            end if


        end for
    end loop

end recieve

startframe := GUI.CreateLabelledFrame (10, 390, 293, 300, GUI.INDENT, "Chat")
joiny := GUI.CreateLabel (55, 370, "Welcome to the chat, Enter your name.")
ip1 := GUI.CreateTextFieldFull (105, 345, 100, "", DoNothing, GUI.INDENT, 0, 0)
ipsend := GUI.CreateButton (125, 310, 0, "Enter", name)
inttitle := GUI.CreateLabelFull (20, 280, "Messages", 250, 0, GUI.CENTER, 0)
inttextbox := GUI.CreateTextBoxFull (10, 10, 280, 265, GUI.INDENT, 0)

loop
    exit when GUI.ProcessEvent
end loop

cls
GUI.Refresh
GUI.Dispose (joiny)
GUI.Dispose (ipsend)
GUI.Dispose (ip1)


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
Sponsor
sponsor
Ultrahex




PostPosted: 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:
code:
import GUI
setscreen ("graphics:400;500,nobuttonbar,inttitle:Turing Messenger")
View.Update
colourback (54)
cls

var port : int := 5505

%--------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

    stream (1) := Net.OpenConnection (netaddress, port)

    if stream (1) <= 0 then
        put "Failure connecting to host, ", netaddress
        return
    else
        put "Connected to ", netaddress
    end if

    GUI.Quit

end Join

process chat

    loop

        if resetcounter >= 6 then
            clearlines
            locate (1, 1)
            resetcounter := 0
        end if

        get strmessage : *

        resetcounter := resetcounter + 1

        if strmessage not= "" then

            strmessagetemp := strmessage

            put : stream (1), strname, ": ", strmessagetemp

            strmessagecloak := strname + ": " + strmessagetemp

            GUI.AddLine (inttextbox, strmessagecloak)

        end if
    end loop

end chat


process recieve

    loop

        for i : 1 .. 10

            get : stream (i), strmessagecloak : *

            if strmessagecloak not= "" then

                GUI.AddLine (inttextbox, strmessagecloak)

            end if


        end for
    end loop

end recieve

startframe := GUI.CreateLabelledFrame (10, 390, 293, 300, GUI.INDENT, "Chat")
joiny := GUI.CreateLabel (55, 370, "Welcome to the chat, Enter your name.")
ip1 := GUI.CreateTextFieldFull (105, 345, 100, "", DoNothing, GUI.INDENT, 0, 0)
ipsend := GUI.CreateButton (125, 310, 0, "Enter", name)
inttitle := GUI.CreateLabelFull (20, 280, "Messages", 250, 0, GUI.CENTER, 0)
inttextbox := GUI.CreateTextBoxFull (10, 10, 280, 265, GUI.INDENT, 0)

loop
    exit when GUI.ProcessEvent
end loop


GUI.Dispose (joiny)
GUI.Dispose (ipsend)
GUI.Dispose (ip1)

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




PostPosted: 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 Razz
Miketron




PostPosted: 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.

code:

process newclients

    loop

        for i : 3 .. 11

            if USERS < 10 then
           
                stream (i) := Net.WaitForConnection (port, netAddress (i))
                USERS := USERS + 1
               
            end if

        end for

    end loop

end newclients
Miketron




PostPosted: 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.

code:

process newclients

    loop

        for i : 3 .. 11

            if USERS < 10 then
           
                stream (i) := Net.WaitForConnection (port, netAddress (i))
                USERS := USERS + 1
               
            end if

        end for

    end loop

end newclients
rdrake




PostPosted: 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




PostPosted: 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




PostPosted: 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

end host

process newclients

    loop

        for i : 3 .. 11

            if USERS < 10 then
           
                stream (i) := Net.WaitForConnection (port, netAddress (i))
                USERS := USERS + 1
               
            end if

        end for

    end loop

end newclients


host

cls
fork newclients

loop

    for i : 2 .. 11

        if stream (i) > 0 then

            if Net.LineAvailable (stream (i)) then

                get : stream (i), strmessagecloak : *

                put strmessagecloak

                if strmessagecloak not= "" then

                    for j : 2 .. 11

                        if stream (j) > 0 then

                            if Net.LineAvailable (stream (j)) then

                                put : stream (j), strname, ": ", strmessagecloak

                            end if

                        end if

                    end for

                end if

            end if

        end if

    end for

end loop
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 21 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: