LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2006, 08:05 AM   #1
trscookie
Member
 
Registered: Apr 2004
Location: oxford
Distribution: gentoo
Posts: 463

Rep: Reputation: 30
Smile java threaded functions????


hello all, im building a client and server, which i have working however ive noticed a flaw in my client, i made a threaded class to handle the connection because it needs a loop and if i have this in my main class, my program will get stuck in the loop.

this is the code:

Code:
package current.client;
import java.io.*;
import java.net.*;

public class clientThread extends Thread {
        public boolean connected = false;
        
        String fromServer;
        String fromUser;
        Socket socket = null;
        PrintWriter out = null;
        BufferedReader in = null;
        BufferedReader stdIn = new BufferedReader( new InputStreamReader( System.in ) );
        
        
    public String connect( String ip, int port ) throws IOException {
        String value;
        
        try {
            socket = new Socket( ip, port );
            out = new PrintWriter( socket.getOutputStream(), true );
            in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
            value = "State: Connected to: " + ip + ":" + port;
            connected = true;
        } catch( UnknownHostException errUE ) {
            value = "State: Cant find server.";
            connected = false;
        } catch( IOException errIO ) {
            value = "State: Cant IO with server make sure its running.";
            connected = false;
        }
        return value;
    }
now this code works fine, however, if the client receives data from the server the main class doesnt receive the information as this class gets there first. is there a way to make this class into a threaded function i.e. public connect() extends Thread?? is that possible? if not is there a way i could get this class to forward the information to a handle in my clients main class??

please please help!!!!
 
Old 06-16-2006, 04:27 PM   #2
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 52
Maybe you can try using the observer pattern : see java.util.Observer, java.util.Observable
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any functions in java like scanf( ) in c? nickhx Programming 9 03-22-2011 07:57 AM
Stop java program(threaded program..should end cleanly) rmanocha Programming 4 11-09-2004 09:36 AM
What is the difference between a "Threaded version" and "Non Threaded" packages? davidas Linux - Newbie 1 04-05-2004 06:23 PM
calling command line functions in java darthczyz Programming 2 12-09-2003 07:01 AM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration