473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Thread Problem

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
1 2194
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tony Proctor | last post by:
Hi everyone We're experiencing some serious anomalies with the scheduling of ASP threads. I'd be interested to hear if anyone knows what algorithm is used (e.g. simple round-robin, or something...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
6
by: Tomaz Koritnik | last post by:
I have a class that runs one of it's method in another thread. I use Thread object to do this and inside ThreadMethod I have an infinite loop: While (true) { // do something Thread.Sleep(100);...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
4
by: fred | last post by:
I use a Synclock in a secondary thread and also stop the thread using the abort method. If the abort occurs while the thread is in the Synclock will the SyncLock always be released before the...
51
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() ...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
7
by: Sin Jeong-hun | last post by:
Hi. I'm writing a Client/Multi-threaded Server program on Windows Vista. It worked fine on Windows Vista, but when the server ran on Windows XP, I/O operation has been aborted because of either...
34
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.