![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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);
}
}
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Anybody?
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#4 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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!
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
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.
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
|
|
#6 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
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.
__________________
"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." - Dwight D. Eisenhower |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
it's a pleasure.Ü
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|