Jump to content

IM Client help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Khaotic

Khaotic

    Learning Programmer

  • Members
  • PipPipPip
  • 62 posts
I have done everything i could think of to figure out why it keeps freezing, does anyone see the problem? I know the problem has somthing to do with the connection class...

public class IMClient extends JFrame implements ActionListener , KeyListener, MouseListener{
	public JButton Connect = new JButton("Attempt to connect to IP");
	public JButton send = new JButton("Send Text");

	public JLabel lbl1 = new JLabel("IP Address");
	public static JTextField ip = new JTextField("127.0.0.1",9);
	public static JTextArea convo, text;

	public JPanel panel1 = new JPanel(new FlowLayout());
	public JPanel panel2 = new JPanel(new FlowLayout());
	public JPanel panel3 = new JPanel(new BorderLayout());
	public JMenuItem close;

    public IMClient() {
    	this.setTitle("2-way IM Client by Khaotic");

    	panel1.add(lbl1);
    	panel1.add(ip);
    	panel2.add(Connect);
    	panel3.add(panel1,BorderLayout.NORTH);
    	panel3.add(panel2,BorderLayout.SOUTH);

    	Connect.addActionListener(this);

    	this.add(panel3);
    	this.setSize(300,100);
    }

    public void actualWin() {
    	JFrame IMWindow = new JFrame("Conversation with " + ip.getText());
    	IMWindow.setLayout(new BorderLayout());
    	IMWindow.setDefaultCloseOperation(IMWindow.DISPOSE_ON_CLOSE);
       	IMWindow.setSize(500,530);
    	IMWindow.setVisible(true);

    	convo = new JTextArea("",18,40);
    	text = new JTextArea("",8,40);
    	JScrollPane con = new JScrollPane(convo);
    	JScrollPane txt = new JScrollPane(text);
    	JPanel jpanel1 = new JPanel();
    	JPanel jpanel2 = new JPanel();

    	JMenuBar top = new JMenuBar();
    	JMenu menu = new JMenu("Client");
    	close = new JMenuItem("Close Client");
    	JMenu menu2 = new JMenu("Conversation");
    	JMenuItem stuff = new JMenuItem("Some stuffs");

    	jpanel2.setLayout(new BorderLayout());

    	menu.add(close);
    	top.add(menu);
    	menu2.add(stuff);
    	top.add(menu2);
    	text.setLineWrap(true);
    	convo.setLineWrap(true);
    	jpanel1.add(con);
    	jpanel2.add(txt,BorderLayout.NORTH);
    	jpanel2.add(send,BorderLayout.SOUTH);

		send.addActionListener(this);
    	close.addActionListener(this);
    	text.addKeyListener(this);

    	IMWindow.add(top,BorderLayout.NORTH);
		IMWindow.add(jpanel1,BorderLayout.CENTER);
    	IMWindow.add(jpanel2,BorderLayout.SOUTH);

    	convo.setEditable(false);
    	convo.setText("Waiting for bi-connection establishment...\n");

    	text.setBackground(Color.darkGray);
    	text.setForeground(Color.white);
    	text.setEditable(false);
    	text.addMouseListener(this);
    	text.setText("Click here to type your message");

    	send.setEnabled(false);
	 }


    public static void main(String[] args) {
        IMClient window = new IMClient();
        window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
        window.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
		if(e.getSource() == Connect) {
			actualWin();
			connection connectionObj = new connection();
			try {
				connectionObj.connect();
			}
			catch(IOException i) {

			}
		}
		else if(e.getSource() == close) {
			System.exit(0);
		}
		else if(e.getSource() == send) {
    		convo.setText(convo.getText() + "You say: \n" + text.getText() + '\n');
    		text.setText("");
    	}
    }
    public void keyReleased(KeyEvent e) {

    }
    public void keyPressed(KeyEvent e) {

    }
    public void keyTyped(KeyEvent e) {
			if(text.getText().equals("")) {
				send.setEnabled(false);
			}
			else {
				send.setEnabled(true);
			}
    }
    public void mouseClicked(MouseEvent e) {
    	if((e.getSource() == text) && text.isEditable() == false) {
    		text.setText("");
    		text.setEditable(true);
    	}
    }
    public void mouseEntered(MouseEvent e) {

    }
    public void mouseExited(MouseEvent e) {

    }
    public void mousePressed(MouseEvent e) {

    }
    public void mouseReleased(MouseEvent e) {

    }
}

class connection {
	public void connect() throws IOException {

	Socket echoSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            echoSocket = new Socket(IMClient.ip.getText(), 9871);
            out = new PrintWriter(echoSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                                        echoSocket.getInputStream()));
        } catch (UnknownHostException e) {
            IMClient.convo.setText("Don't know about host: taranis.");
            //System.exit(1);
        } catch (IOException e) {
            IMClient.convo.setText("Couldn't get I/O for "
                               + "the connection to: taranis.");
            //System.exit(1);
        }

	BufferedReader stdIn = new BufferedReader(
                                   new InputStreamReader(System.in));
	String userInput;

	while ((userInput = stdIn.readLine()) != null) {
	    out.println(userInput);
	    System.out.println("echo: " + in.readLine());
	}
	}
}

Check out my site: www.khaoticirc.net

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I haven't tried to run the code, but this looks suspicious:
while ((userInput = stdIn.readLine()) != null) {
	    out.println(userInput);
	    System.out.println("echo: " + in.readLine());
	}

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog