LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java threaded functions???? (https://www.linuxquestions.org/questions/programming-9/java-threaded-functions-455382/)

trscookie 06-16-2006 08:05 AM

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!!!!

mrcheeks 06-16-2006 04:27 PM

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


All times are GMT -5. The time now is 03:22 AM.