Computer Science Canada Need help with chat client |
Author: | Asok [ Fri May 20, 2005 9:19 pm ] |
Post subject: | Need help with chat client |
Hey everyone. I'm trying to modify the code in the networking tutorial (found here http://www.compsci.ca/v2/viewtopic.php?t=8087 ) so that it operates more like a chat client. The main change is basically that the text string input is being passed in from a TextField and the output is being passed out on a TextArea (java.awt.applet). The problem I'm encountering is that the server will only accept incoming information once and then drop the client. How should I go about making the server *always* accept information from the client? Any help is greatly appreciated, Thanks ![]() -Asok |
Author: | rizzix [ Fri May 20, 2005 11:18 pm ] | ||
Post subject: | |||
for the client it should be something along the lines:
PS: this is very very bad design.. but it does explain some concepts such as separating the Model (MessageListModel) and Controller (KeyAdapter) from the View (JTextArea) PPS: this code only covers sending messages (not recieving them) |
Author: | Asok [ Sat May 21, 2005 3:05 pm ] | ||
Post subject: | |||
wow rizzix, thank you so much however the code doesn't seem to run, error is on this line: Vector<String> messages = new Vector<String>(); and it is "generics not supported in -source 1.2(try source 1.5 to enable generics)" the editor I'm using is JBuilder 2005 Personal. anyways I thought that it might help if I post my client code here so you can get a sense of what I'm *trying* to do (it interfaces with your server code left unchanged from the tutorial, this build in particular cannot properly send or recieve messages from the server yet but it can connect)
Anyways I'd just like to thank you again for your help, I was never expecting so much ![]() |
Author: | Hikaru79 [ Sat May 21, 2005 3:53 pm ] |
Post subject: | |
Asok wrote: wow rizzix, thank you so much however the code doesn't seem to run, error is on this line:
Vector<String> messages = new Vector<String>(); and it is "generics not supported in -source 1.2(try source 1.5 to enable generics)" the editor I'm using is JBuilder 2005 Personal. The problem is that you're using an outdated version of java (1.2) when we are several years ahead of that (1.5). You can get the newest SDK from http://java.sun.com |
Author: | rizzix [ Sat May 21, 2005 11:27 pm ] |
Post subject: | |
asok the problem with your code is that your are establishing a connection to the server every time an action is performed.. you might want to move that snippet up.. i.e keep the connection open.. and simply send a message to the server when an action is performed.. also,, close your connection when the windows is closed ![]() anyways.. i've updated my code to actually send a message. to test it out (after you compile it).. just make sure you first run TestServer (from tutorial) and then run this program.. simple as that. |
Author: | Asok [ Sun May 22, 2005 12:52 am ] | ||
Post subject: | |||
hey rizzix ![]() I figured out why I was only able to ever send one message. It had to do with the: while ((input = clientIn.readLine()) != null) on the server side. I got rid of that and can now send as many messages as I want to the server ![]() I basically now have only 3 more issues to tackle: 1) getting the server to send it back to display it in the text area. I assume it's the same way as I sent data to the server from the client. But it's 1:40 AM and I'll test that in the morning ![]() 2) modifying the server code to allow multiple clients on at the same time. This will probably be the trickiest part because it'll require an overhaul of the server code(flexible array of clients perhaps?) and I'll wait till after I get one client working flawlessly before I attempt to modify the server code more. 3) correcting a bloody EOF error when trying to read a file. (your tutorial helped me out a lot with the concepts of I/O ![]()
rizzix, I can't thank you enough, your tutorials have been basically the key to understanding all of this (my textbook is terrible haha ![]() |
Author: | rizzix [ Sun May 22, 2005 1:32 am ] |
Post subject: | |
does the User class implement the Serializable interface? it should.. this interface does not define any methods as such.. it simply acts as a flag explictly stating that objects of that class can be serialized (read or written to a stream). |
Author: | Asok [ Sun May 22, 2005 10:32 am ] | ||
Post subject: | |||
yea it does rizzix, here's my user class ![]()
I'm still trying to figure out why there is the EOF error, it says there is no problem writing only reading but that may not be the actual case. |
Author: | rizzix [ Sun May 22, 2005 1:41 pm ] |
Post subject: | |
heh you sure there's something in that file? ![]() so yea.. you might want to write a dummy object to that file first and then run ur code.. |
Author: | Asok [ Sun May 22, 2005 2:33 pm ] |
Post subject: | |
when I open the text file all I get is two box characters so I know SOMETHING is in there but I'm not exactly sure what. |
Author: | rizzix [ Sun May 22, 2005 5:24 pm ] |
Post subject: | |
hmm.. well.. empty the file.. and write a dummy object to it.. then run ur program.. post if you still have errors. |
Author: | Asok [ Sun May 22, 2005 10:20 pm ] | ||
Post subject: | |||
thanks for the advice rizzix! now that txt file isn't empty because I'm writng the dummy object. the text file looks like this: Quote: ¬à sr User}×j-d ÇQ L strNamet Ljava/lang/String;L strPortq ~ L strServerq ~ xpt Guestt 5060t localhost the problem now is on the reading side. when I close and reopen it only displays the information in the dummy object. I think it has something to do with the logical aspect of my code. I've posted the updated code here:
Thanks again for all of your help rizzix ![]() |
Author: | rizzix [ Sat May 28, 2005 7:25 pm ] |
Post subject: | |
ok its really simple.. and yes its a logic problem.. first delete the user.txt file. now edit your code to check to see if user.txt exists.. if it does not exist.. write a dummy object to the file.. that will fix your problems.. btw in order to check if the file exist. you'd have to first create a File object. |