4 Replies Latest reply: Sep 16, 2010 7:45 PM by 843789 RSS

    Thread.start() doesn't work in a static method ?

    843789
      I noticed that Thread.start() worked perfectly in the main. but right now i need to start a different thread in a static method, but the system is denying me of doing so.

      This is what this object - Client should do.
      1. Connected to a local server
      2. If server prompts "WAIT", Client starts it's own server (i call it clientServer) and returns server the port clientServer listens on.
      3. If server prompts "HostAddr:Port", connect to the given host - in this case would be the clientServer

      After compilation this is the error i have got. The compiler is telling me it it doesn't recognize it. The error occurred at the last line of the try-block in my handshake() method. Please enlighten me what is wrong, and how can i possibly go about fixing it. And sorry about my bad coding style.
      Client.java:79: cannot find symbol
      symbol  : method start()
      location: class Client
                              clientServThread.start();
                                              ^
      1 error
      Following are my codes:
      import java.net.ServerSocket;
      import java.net.Socket;
      import java.io.*;
      import java.util.Scanner;
      
      public class Client implements Runnable{
          /******************************
          *ClientServer
          ******************************/    
          private Socket toAnotherClient;
          private static int servPort;
          public Client(Socket t){
              toAnotherClient = t;
          }
      
          public void run(){
              try{
                  InputStream incoming = toAnotherClient.getInputStream();    //incoming from another client
                  OutputStream outgoing = toAnotherClient.getOutputStream();    //outgoing to another client
                  if(!ChatUtility.ChatModule(incoming,outgoing))
                      ChatUtility.Disconnect(ssend,outgoing);    //dc from server and clientServer
              }
              catch(IOException e){
                  e.printStackTrace();
              }
              catch(Exception e){
                  System.out.println("Unknown exception, check ChatModule");
                  e.printStackTrace();
              }
          }
      
          /***********************************
          * Client
          ***********************************/
          public static InputStream srecv;    //incoming msg from server
          public static OutputStream ssend;    //outgoing msg to server
          public static final int SERVER_PORT = 19989;
          public static String IP = "127.0.0.1";
          public static void main(String[] args){
              System.out.println("Starting client...");
              System.out.println("attempting to connect to "+IP+" on port: "+SERVER_PORT);
              try{
                  //Estabilish connection with server
                  Socket toServer = new Socket(IP,SERVER_PORT);
                  //Estabilish I/O with server
                  srecv = toServer.getInputStream(); 
                  ssend = toServer.getOutputStream();
                  System.out.println("waiting for server ...");
                  //Initialize Client-Server Handshake
                  handshake(srecv, ssend);            
              }
              catch(IOException e){
                  e.printStackTrace();
              }
          }
          /*HandShake protocol, decides whether to StartServer or ConnectToAnotherClient*/
          private static void handshake(InputStream srecv, OutputStream ssend) throws IOException{
              byte[] buffer = new byte[512];
              while(srecv.available()>0){}
              int sizeRead = srecv.read(buffer);
              String msgFromServer = ChatUtility.parseByte(buffer, sizeRead);
              /*Case being first to connect to server - StartServer*/
              if(msgFromServer.equals("WAIT")){
                  try{
                  System.out.println("Recieved msg from server:" + msgFromServer +"\nStarting host service ...");
                  //determine service port
                  servPort = ChatUtility.portChooser();
                  ssend.write(("CLIENT_PORT "+Integer.toString(servPort)).getBytes());
                  //create servicesoc
                  System.out.println("Openning port :"+servPort);
                  ServerSocket clientServer = new ServerSocket(servPort);
                  Socket toAnotherClient = clientServer.accept();
                  System.out.println("Openning port :"+servPort);
                  System.out.println("Started listening ... ");
                  //start client-server thread
                  Client clientServThread = new Client(toAnotherClient);
                  System.out.println("A clientServThread has been created.");
                  clientServThread.start();
                  }
                  catch(IOException e){e.printStackTrace();}
              }
              /*Case being 2nd to connect to server - ConnectToAnotherClient*/
              if(msgFromServer.startsWith("PEER_LOC ")){
                  msgFromServer = msgFromServer.substring(9); //left only $h:$p
                  int seperator = msgFromServer.indexOf(":");
                  //obtain clientServer IP and port
                  String clientServAddr = msgFromServer.substring(0,seperator);
                  int clientServPort = Integer.parseInt(msgFromServer.substring(seperator+1));
                  try{
                      //Establish connection and IO with clientServer
                      Socket anotherClient = new Socket(clientServAddr,clientServPort);
                      InputStream crecv = anotherClient.getInputStream();  //outgoing msg to clientServer
                      OutputStream csend = anotherClient.getOutputStream();  //incoming msg from clientServer
                      //###########Start Chat#############
                      if(!ChatUtility.ChatModule(crecv,csend))
                          ChatUtility.Disconnect(ssend,csend);//dc from server and clientServer
                  }
                  catch(IOException e){
                      e.printStackTrace();
                  }
                  catch(Exception e){
                      System.out.println("Unknown exception, check ChatModule");
                      e.printStackTrace();
                  }
              }
          }
      }