Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-26-2008, 09:41 AM
Member
 
Join Date: May 2008
Posts: 26
Rep Power: 0
Master Zero is on a distinguished road
Default [SOLVED] "Threading" a method?
I am trying my hands at socket programming but I am kind of stuck. I am not sure how to implement multi-threading in java. I have a method call listen which I would like to run on a separate thread so that it will not freeze the GUI while it waits. The class is below, and any help is greatly appreciated.

Code:
import java.io.*;
import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.util.Date;
import java.awt.event.*;

public class ChatServer extends JFrame implements ActionListener {

	/**
	 * @param args
	 */
	
	// Variables
	TextField msg;
	TextArea room;
	
	JButton listen;
	
	PrintStream os;
	DataInputStream is;
	Socket clientSocket;
	ServerSocket server;
	
	public ChatServer() {
		server = null;
		clientSocket = null;
		
		msg = new TextField();
		room = new TextArea();
		
		listen = new JButton("Listen");
		
		this.add("South", msg);
		this.add("Center", room);
		this.add("North", listen);
			listen.addActionListener(this);
		
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				try {
					if (server != null) {
						server.close();
					}
					if (clientSocket != null) {
						clientSocket.close();
					}
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					room.append(e1.getMessage());
				}
				
				System.exit(0);
			}
		});
	}
	
	public static void main(String[] args) {
		ChatServer f = new ChatServer();
			f.setBounds(0, 0, 250, 200);
			f.setTitle("Chat Server");
			f.setVisible(true);
	}
	
	public boolean keyDown(Event evt, int key) {
		if (key == 10) {
			room.append(msg.getText() + "\n");
			msg.setText("");
		} return super.keyDown(evt, key);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.toString().split(",")[1].substring(4).equals("Listen")) {
			try {
				server = new ServerSocket(4000);
				
				room.append("Server now listening for client...\n");
				this.clientSocket = server.accept();
				
				room.append("Client has connected!\n");
				os = new PrintStream(this.clientSocket.getOutputStream());
				is = new DataInputStream(this.clientSocket.getInputStream());
				
				this.sendText("Welcome, current login time is :" + new Date());	
				listen();
			} catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
				room.append(e2.getMessage());
			}
		}
	}
	
	public void sendText(String message) {
		os.println(message);
		os.flush();
	}
	
	public void listen() {
		try {
			room.append(is.readLine() + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			room.append(e.getMessage());
			System.exit(0);
		}
		listen();
	}
}

Last edited by Master Zero; 05-26-2008 at 09:44 AM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 05-26-2008, 09:53 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
You want to add another thread basically. Before that seems to me your GUI freez. May be you use some deprecated methods which is not allowed in my Java version.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-26-2008, 10:06 AM
Member
 
Join Date: May 2008
Posts: 26
Rep Power: 0
Master Zero is on a distinguished road
Default
I am not sure how to go about you solution; but yeah, I wish to call the method below on its own thread.

Code:
public void listen() {
		try {
			room.append(is.readLine() + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			room.append(e.getMessage());
			System.exit(0);
		}
		listen();
	}
Right now, the method is simply calling it’s self over and over.

Thanks for the reply.

Last edited by Master Zero; 05-26-2008 at 10:09 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-26-2008, 10:45 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
I just say keyDown() and readLine() are deprecated since 1.1

You can try something like this. In this way, not effect to GUI thread.

Code:
public class ThreadTest implements Runnable{

    Thread processThread;
    
    public void run() {
        while(true) {
            // Do the process
        }
    }

}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-26-2008, 05:17 PM
Member
 
Join Date: May 2008
Posts: 26
Rep Power: 0
Master Zero is on a distinguished road
Default
Thanks a million!
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-27-2008, 04:26 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,513
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
No problem my friend. Nice to hear that I'm help to solve your question.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error! "filename" is not abstract and does not override abstract method... hasani6leap New To Java 6 10-27-2008 01:25 AM
[SOLVED] Exception in thread "main" java.util.NoSuchElementException thevoice New To Java 5 05-14-2008 02:43 PM
"Jumble" or "Scramble" Program Shadow22202 Java Applets 8 04-30-2008 04:42 AM
I am confused about is the "add" method... mathias New To Java 1 08-01-2007 06:29 AM
Hwlp with "Open", "Save", "Save as..." trill New To Java 1 07-31-2007 08:53 AM


All times are GMT +2. The time now is 04:37 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org