Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 27th, 2005, 6:01 PM   #1
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Freezing Applets

I'm making a simple chat client. It's an ugly chat client at the moment, but I'm attempting to make a chat client. Anyway, my main program is called Client. The only other class I have at the moment is Prefs, which is a preferences frame. Both of these classes extend JFrame. The Prefs class allows you to pass an encryption key, IP, and your username (and I'm using a bad way to pass this information, so if anyone has any advice to make this more elegant, let me know). After you type in all of your information, you hit the connect button, which passes all of the info to Client, and then calls the client method connect(). Before doing this, though, I'm trying to dispose of the Prefs frame. Here is where the problem arises. I've tried many different ways to dispose of it, but nothing seems to work. It freezes my program every time. Can anyone point me in the right direction? Thanks.

Here's my code:

Client:
 import java.awt.*;
 import javax.swing.*;
 import java.io.*;
 import java.net.*;
 import java.awt.event.*;
 import javax.swing.border.*;
 import java.util.*;
 
 public class Client extends JFrame implements ActionListener 
 {
 	Socket sock;
 	JTextField say;
 	JTextArea chat;
 	Container cp = getContentPane();
 	ObjectInputStream recieve;
 	ObjectOutputStream send;
 	JTextArea users;
 	JMenu file;
 	JMenu prefs;
 	JMenuItem exit;
 	JMenuItem preferences;
 	static String userName;
 	static String key;
 	static String IP;
 	static Client c;
 	public Client()
 	{
 		file = new JMenu("File");
 		prefs = new JMenu("Preferences");
 		exit = new JMenuItem("Exit");
 		file.add(exit);
 		preferences = new JMenuItem("Set Preferences");	
 		prefs.add(preferences);
 		JMenuBar toolbar = new JMenuBar();
 		toolbar.add(file);
 		toolbar.add(prefs);
 		setJMenuBar(toolbar);
 		setSize(640, 480);
 		chat = new JTextArea("Connecting to server . . .", 28, 43);
 		say = new JTextField();
 		users = new JTextArea(28, 14);
 		users.setEditable(false);
 		users.setBorder(new EtchedBorder(Color.RED, Color.BLUE));
 		chat.setEditable(false);
 		cp.add(BorderLayout.EAST, users);
 		cp.add(BorderLayout.SOUTH, say);
 		cp.add(BorderLayout.WEST, chat);
 		say.addActionListener(this);
 		exit.addActionListener(this);
 		preferences.addActionListener(this);
 		chat.setBackground(new Color(255, 255, 128));
 		users.setBackground(Color.BLACK);
 		setVisible(true);
 		
 	}
 	
 	public void connect()
 	{
 		setVisible(true);
 		System.out.println("boo")	;
 		try
 		{
 			sock = new Socket("127.0.0.1", 6665);
 			recieve = new ObjectInputStream(sock.getInputStream());
 			send = new ObjectOutputStream(sock.getOutputStream());
 		}
 		catch(UnknownHostException e)
 		{
 			chat.append("\nHost not found");
 		}
 		catch(IOException e)
 		{
 			chat.append("\nServer not responsive");
 		}
 		
 		if(sock!=null)
 		{
 			chat.append("\nConnected");
 			chat.setFont(new Font("p", Font.PLAIN, 12));
 			talk();
 		}
 	}
 	
 	public void actionPerformed(ActionEvent e)
 	{
 		if(e.getSource() == say)
 		{
 			if(key==null)	
 			{
 				Prefs p = new Prefs();
 			}
 			else
 			{
 				String temp = say.getText();
 				say.setText("");
 				chat.append("\n" + temp);
 				try
 				{
 					send.writeObject(temp);
 				}		
 				catch(IOException i)
 				{
 		    		System.out.println("Couldn't send");
 				}
 		    }		    	
 		}
 		if(e.getSource() == exit)
 		{
 			dispose();
 		}
 		if(e.getSource() == preferences)
 		{
 			Prefs p = new Prefs();
 		}
 	}
 	
 	public void talk()
 	{
 		while(true)
 		{
 			String temp=" ";
 			try
 			{
 				temp = (String)recieve.readObject();
 			}
 			catch(IOException e)
 			{
 				System.out.println("Could not read from sock");
 			}
 			catch(ClassNotFoundException e)
 			{
 				System.out.println("Class not found");
 			}
 			chat.append("\n" + temp);
 		}
 	}
 	
 	public String encrypt(String mkey, String msg)
 	{
 		int binaryKey = Integer.parseInt(mkey, 2);
 		char [] tempMsg = msg.toCharArray();
 		for(int i = 0; i<tempMsg.length; i++)
 		{
 			tempMsg[i] ^= binaryKey;
 		}
 		return new String(tempMsg);
 	}
 	
 	
 	public static void main(String [] args)
 	{
 		c = new Client();
 		
 	}
 	
 	public void setEncryptionKeyAndUserName(String t_key, String t_userName, String t_IP)
 	{
 		key = t_key;
 		userName = t_userName;
 		IP = t_IP;
 		connect();
 	}
 }

Prefs:
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 
 public class Prefs extends JFrame implements ActionListener
 {
 	JPasswordField key;
 	JLabel keyPrompt;
 	JLabel blank;
 	JTextField userName;
 	JLabel namePrompt;
 	Container cp = getContentPane();
 	Button finish;
 	JTextField IP;
 	JLabel IP_Prompt;
 	
 	
 	public Prefs()
 	{
 	
 		try 
 		{
 			UIManager.setLookAndFeel(
 			"com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
 		} catch (Exception e) { }
 		FlowLayout f = new FlowLayout();
 		setLayout(f);
 		setSize(400, 200);
 		keyPrompt = new JLabel("Please enter your 8-bit key:");
 		IP_Prompt = new JLabel("Pleas enter the IP of the computer you will be connecting to:");
 		IP = new JTextField(20);
 		key = new JPasswordField(8);
 		namePrompt = new JLabel("Please enter your user name:");
 		userName = new JTextField(20);
 		finish = new Button("Connect");
 		finish.addActionListener(this);
 		blank = new JLabel("																												 ");
 		cp.add(keyPrompt);
 		cp.add(key);
 		cp.add(blank);
 		cp.add(IP_Prompt);
 		cp.add(IP);
 		cp.add(blank);
 		cp.add(namePrompt);
 		cp.add(userName);
 		cp.add(blank);
 		cp.add(finish);
 		setVisible(true);
 	}
 	
 	public void actionPerformed(ActionEvent e)
 	{
 		dispose();
 	    Client.c.setEncryptionKeyAndUserName(new String(key.getPassword()), userName.getText(), IP.getText());
 	}
 	
 }

I've added a test server for you, so you can simply connect at 127.0.0.1 .

Server:
 import java.awt.*;
 import javax.swing.*;
 import java.io.*;
 import java.net.*;
 import java.awt.event.*;
 public class Server
 {
 	public static void main(String[]args) throws Exception
 	{
 		ServerSocket s = new ServerSocket(6665);
 		Socket sock = s.accept();
 		ObjectOutputStream o = new ObjectOutputStream(sock.getOutputStream());
 		o.writeObject("BLAH");
 		ObjectInputStream i = new ObjectInputStream(sock.getInputStream());
 		String t = (String)i.readObject();
 		System.out.println(t);
 	}
 }
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 1st, 2005, 8:44 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Anybody?
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 4th, 2005, 6:04 AM   #3
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
i'm working on it. got a physics assignment also bugging me. just hang in there buddy...Ü

sofar it looks like the program is freezing as it waits for the server to respond with a message(which should happen after it connects) and that's probably why it's not repainting(i'm taking a guess here). in my (very basic) chat program i use a thread that waits for messages and actionPerformed( .... ) to send messages. have you tried to use threads? also a little thing that helps, is when you start a application like this, rather start with the DOS screen and as soon as you can send and receive messages, then you slap in some gui thingies.

what does your encrypt method do and how does it go about it's business? i'm not that well trained in java, so that looks a bit foreign to me.Ü

hope i helped you a bit. i'll carry on playing with it and figure out what goes wrong where. i'll let you know when i find something.
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Old Apr 4th, 2005, 3:04 PM   #4
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Hey! Thanks for the response.

I do believe that my server is responding. When I hardcode the IP address and everything, it works exactly as I expect it to, with the server sending it messages, and it being capable of sending messages back to the server. I've not yet used threads, but I could incorporate them. The thing is, this program works just how I want it to except the Prefs class freezes everything. Actually, I did exactly what you said when I first started this, and started by making a simple dos-based app, and it worked -- although I couldn't send and recieve at the same time, because I had not used threads. My encrypt method is supposed to just be a simple XOR encryption -- might not work yet, but it seemed to when I last checked. I can't remember, though, I might have taken it out.

Once again, thanks for the help!
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 5th, 2005, 3:40 AM   #5
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
there you go. it works quite well. you send a message, and the server responds with "got your message"(or something like that). i think it stopped freezing when i fiddled with your talk method. it reads a string, then puts it on the text area, then the whole application waits again at readObject. also changed it to accept IP instead of hardcoding the ip address in.

maybe i'll go play with it and figure out what went wrong.
Attached Files
File Type: zip chat.zip (2.6 KB, 41 views)
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Old Apr 8th, 2005, 8:25 PM   #6
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
I appologize for the late response. I've been busy with school and other things over the week, and must have completely overlooked your post. And thank you so much for helping me fix this.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Apr 13th, 2005, 5:07 AM   #7
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4 Easter Bunny is on a distinguished road
it's a pleasure.Ü
__________________
There's got to be more to life than being really, really
ridiculously good looking
Easter Bunny is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:01 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC