By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,778 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,778 IT Pros & Developers. It's quick & easy.

Thread Problem

P: n/a
I'm writing a Java IRC client and I have a problem in that, whenever the
program connects to an IRC server the graphics freezes. Someone said that I
needed to use threads using invokeLater so I came up with this...

//Smeg - Java IRC Client
//Written by Darrell Blake and released under the GNU GPL license. See
COPYING for more information on this license.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;

public class Smeg extends JFrame implements ActionListener
{
private JButton send;
private static JTextArea chatWindow;
private JTextField chatBox;
private static Container pane;
private static InetAddress serverName;
private static int connectionPort;
private static Socket link;
private static PrintWriter out;
private static BufferedReader in;
private String progName = "Smeg";
private String[] cmdArgs;
private String progVersion = "0.01";
private static String nick;
private static String realname;
private boolean connected;

public static void main(String[] args) throws IOException
{
Smeg SmegMain = new Smeg();
SmegMain.setSize(600,600);
SmegMain.setVisible(true);

serverName = InetAddress.getByName("irc.freenode.net"); //Set server
defaults.
connectionPort = 6667;
nick = "SmegNick";
realname = "SmegName";
}

public Smeg()
{
connected = false;

pane = getContentPane();
cmdArgs = new String[15];

setTitle(progName + " v" + progVersion);
pane.setLayout(new FlowLayout());

send = new JButton("Send");
send.addActionListener(this);

chatWindow = new JTextArea(35,52);
chatWindow.setWrapStyleWord(true);
chatWindow.setLineWrap(true);
chatWindow.setEditable(false);
chatWindow.setText("Welcome to " + progName + " v" + progVersion + "!\
\n");

chatBox = new JTextField(45);
chatBox.addActionListener(this);

pane.add(new JScrollPane(chatWindow));
pane.add(chatBox);
pane.add(send);

this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}

public void actionPerformed(ActionEvent e)
{
String message;
message = chatBox.getText();
chatBox.setText("");
if (!message.startsWith("/"))
chatWindow.setText(chatWindow.getText() + message + "\n");
else
{
cmdArgs = message.split(" ");
if (cmdArgs[0].equals("/server"))
SwingUtilities.invokeLater(new Runnable()
{
public void run() {
cmdServer();
}
});
else
JOptionPane.showMessageDialog(pane, "Unknown command!");
}
}

public void cmdServer()
{
String response;

try
{
serverName = InetAddress.getByName(cmdArgs[1]);

if (cmdArgs.length > 2)
connectionPort = Integer.parseInt(cmdArgs[2]);

System.out.println("Connecting to server " + serverName + " on port " +
Integer.toString(connectionPort) + "\n");
try
{
link = new Socket(serverName, connectionPort);

in = new BufferedReader
(new InputStreamReader
(link.getInputStream()));

out = new PrintWriter(
link.getOutputStream(),true);

out.println("USER " + nick + " +iw " + nick + " :" + realname);
out.println("NICK " + nick);
out.println("USERHOST " + nick);
out.println("MODE " + nick + " +iw");
while(true)
{
response = in.readLine();
System.out.println(response);
chatWindow.setText(chatWindow.getText() + response + "\n");
}
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
catch (UnknownHostException ex)
{
JOptionPane.showMessageDialog(pane, "Server not found!");
}
}

public void cmdJoin()
{
}

public void cmdPart()
{
}
}

The graphics is still freezing though. I've even tried sticking the GUI in a
method on it's own and running that from main in an invokeLater but the
graphics still freezes when I try to connect to a server. Can someone
please take a look at my code and tell me what I'm doing wrong?

Darrell

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Let your program to process timeout in case of server is busy and readline
never return.
What will happen if server doesn't response a line?

"Chris P. Bacon" <no@no.com> wrote in message
news:q3*****************@news-binary.blueyonder.co.uk...
I'm writing a Java IRC client and I have a problem in that, whenever the
program connects to an IRC server the graphics freezes. Someone said that I needed to use threads using invokeLater so I came up with this...

//Smeg - Java IRC Client
//Written by Darrell Blake and released under the GNU GPL license. See
COPYING for more information on this license.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;

public class Smeg extends JFrame implements ActionListener
{
private JButton send;
private static JTextArea chatWindow;
private JTextField chatBox;
private static Container pane;
private static InetAddress serverName;
private static int connectionPort;
private static Socket link;
private static PrintWriter out;
private static BufferedReader in;
private String progName = "Smeg";
private String[] cmdArgs;
private String progVersion = "0.01";
private static String nick;
private static String realname;
private boolean connected;

public static void main(String[] args) throws IOException
{
Smeg SmegMain = new Smeg();
SmegMain.setSize(600,600);
SmegMain.setVisible(true);

serverName = InetAddress.getByName("irc.freenode.net"); //Set server defaults.
connectionPort = 6667;
nick = "SmegNick";
realname = "SmegName";
}

public Smeg()
{
connected = false;

pane = getContentPane();
cmdArgs = new String[15];

setTitle(progName + " v" + progVersion);
pane.setLayout(new FlowLayout());

send = new JButton("Send");
send.addActionListener(this);

chatWindow = new JTextArea(35,52);
chatWindow.setWrapStyleWord(true);
chatWindow.setLineWrap(true);
chatWindow.setEditable(false);
chatWindow.setText("Welcome to " + progName + " v" + progVersion + "!\ \n");

chatBox = new JTextField(45);
chatBox.addActionListener(this);

pane.add(new JScrollPane(chatWindow));
pane.add(chatBox);
pane.add(send);

this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}

public void actionPerformed(ActionEvent e)
{
String message;
message = chatBox.getText();
chatBox.setText("");
if (!message.startsWith("/"))
chatWindow.setText(chatWindow.getText() + message + "\n"); else
{
cmdArgs = message.split(" ");
if (cmdArgs[0].equals("/server"))
SwingUtilities.invokeLater(new Runnable()
{
public void run() {
cmdServer();
}
});
else
JOptionPane.showMessageDialog(pane, "Unknown command!"); }
}

public void cmdServer()
{
String response;

try
{
serverName = InetAddress.getByName(cmdArgs[1]);

if (cmdArgs.length > 2)
connectionPort = Integer.parseInt(cmdArgs[2]);
System.out.println("Connecting to server " + serverName + " on port " + Integer.toString(connectionPort) + "\n");
try
{
link = new Socket(serverName, connectionPort);
in = new BufferedReader
(new InputStreamReader
(link.getInputStream()));
out = new PrintWriter(
link.getOutputStream(),true);

out.println("USER " + nick + " +iw " + nick + " :" + realname); out.println("NICK " + nick);
out.println("USERHOST " + nick);
out.println("MODE " + nick + " +iw");
while(true)
{
response = in.readLine();
System.out.println(response);
chatWindow.setText(chatWindow.getText() + response + "\n"); }
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
catch (UnknownHostException ex)
{
JOptionPane.showMessageDialog(pane, "Server not found!"); }
}

public void cmdJoin()
{
}

public void cmdPart()
{
}
}

The graphics is still freezing though. I've even tried sticking the GUI in a method on it's own and running that from main in an invokeLater but the
graphics still freezes when I try to connect to a server. Can someone
please take a look at my code and tell me what I'm doing wrong?

Darrell

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.