2017-05-26 29 views
1

在稱爲ServerRecieve的線程類中有一個名爲numberPart的字符串。 .start()被調用的位置在名爲Server的不同類中。如何從另一個文件(多線程Java套接字編程)訪問名爲「numberPart」的字符串

'numberPart'最終將被用作稍後文件傳輸的端口。

我的問題是:如何訪問Server類中的numberPart變量?在運行中(右左窗口服務器,客戶端)的代碼

截圖:

server on left window, client on the right

在屏幕截圖(服務器)的左側窗口中你可以看到這的第一個端口號右窗口的命令行參數是4021通過短信發送,服務器成功收到消息"File transfer port found: 4021"。不幸的是,這個變量位於不同的類中。我想知道如何訪問該類中名爲Server的變量。

ServerRecieve代碼:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.Socket; 

public class ServerRecieve extends Thread 
{ 

    Socket servSocket; 
    boolean m_bRunThread = true; 
    boolean ServerOn = true; 

    public ServerRecieve(Socket s) 
    { 
     super(); 
     servSocket = s; 
    } 
    public void run() 
    { 
     while(true) 
     { 
      try 
      { 
       BufferedReader readFromClient = new BufferedReader(new InputStreamReader(servSocket.getInputStream())); 
       String fromClient = readFromClient.readLine(); 
       String a = fromClient; 
       int i; 
       for(i = 0; i < a.length(); i++) 
       { 
        char c = a.charAt(i); 
        if('0' <= c && c <= '9') 
        { 
         break; 
        } 
       } 
       String alphaPart = a.substring(0, i); 
       String numberPart = a.substring(i); 
       System.out.println("Recieved from client: " + alphaPart +"\n"); 

       System.out.println("File transfer port found: " + numberPart + "\n"); 
       //String[] filePortNumber = null; 
       //filePortNumber[0] = numberPart; 
       // Server thing = new Server(filePortNumber); 
       if(fromClient.equals(null)) 
       { 
        System.exit(0); 
       } 
       OutputOptions(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      finally 
      { 

      } 
     } 
    } 

    void OutputOptions() 
    { 
     System.out.println("Enter an option ('m', 'f', 'x'): "); 
     System.out.println("(M)essage (send)"); 
     System.out.println("(F)ile (request) "); 
     System.out.println("e(X)it "); 
    } 
} 

Server源:

import java.io.*; 
import java.net.*; 
import java.util.*; 
import javax.imageio.IIOException; 
public class Server 
{ 
    private String[] serverArgs; 
    public Socket socket; 
    public Socket fileSocket; 
    public boolean keepRunning = true; 
    public int ConnectOnce = 0; 
    public String option = ""; 
    public boolean isConnected = false; 
    public String FILE_TO_SEND = "/Users/nanettegormley/Documents/workspace/assignment2/src/servers/cdm.jpg"; 

    public Server(String[] args) throws IOException 
    { 
     // set the instance variable 

     this.serverArgs = args; 
     if(ConnectOnce == 0) 
     { 
      int port_number1 = Integer.valueOf(serverArgs[1]); 
      ServerSocket serverSocket = new ServerSocket(port_number1); 
      socket = serverSocket.accept(); 
      ConnectOnce = 4; 
      isConnected = true; 

     } 

    } 
    public String[] serverRun2(String[] args) throws IOException 
    { 

     serverArgs = args; 
     serverArgs = Arrays.copyOf(args, args.length); 
     serverSend.start(); 
     return serverArgs; 
    } 

    Thread serverSend = new Thread() 
    { 
     public void run() 
     { 
      OutputOptions(); 
      while(isConnected) 
      { 
       try 
       { 
        ServerRecieve serverThread = new ServerRecieve(socket); 
        serverThread.start(); 


        // input the message from standard input 
        BufferedReader input2= new BufferedReader(new InputStreamReader(System.in)); 

        option = input2.readLine(); 
        if(option.equals("m") || option.equals("M")) 
        { 
         StandardOutput(); 
        } 
        if(option.equals("f") || option.equals("F")) 
        { 
         FileTransferSend(); 
        } 
        if(option.equals("x") || option.equals("X")) 
        { 
         System.exit(0); 

        } 

       } 
       catch (Exception e) 
       { 
        System.out.println(e.getMessage()); 
       } 
      } 
     } 
    }; 


public void StandardOutput() 
{ 
     try 
     {   
      //Send the message to the server 
      OutputStream os = socket.getOutputStream(); 
      OutputStreamWriter osw = new OutputStreamWriter(os); 
      BufferedWriter bw = new BufferedWriter(osw); 

      //creating message to send from standard input 
      String newmessage = ""; 
      try 
      { 
       System.out.println("Enter your message: "); 
       // input the message from standard input 
       BufferedReader input2= new BufferedReader(new InputStreamReader(System.in)); 
       String line = ""; 

       line= input2.readLine(); 
       newmessage += line + " "; 

      } 
      catch (Exception e) 
      { 
       System.out.println(e.getMessage()); 
      } 
      String sendMessage = newmessage; 
      bw.write(sendMessage + "\n"); 
      bw.newLine(); 
      bw.flush(); 
      System.out.println("Message sent to client: "+sendMessage); 
      StandardInput(); 
      //run(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      finally 
      { 

      } 

} 
void FileTransferSend() 
{ 
    //connect to the filetransfer 



    try 
    { 
     System.out.println("Which file do you want? "); 
     Scanner scanner = new Scanner(System.in); 
     String filename = scanner.nextLine(); 
     FileInputStream fis = new FileInputStream(new File(filename)); 
     DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(fileSocket.getOutputStream())); 
     int element; 
     while((element = fis.read()) !=1) 
     { 
      dos.write(element); 
     } 
     byte[] byteBuffer = new byte[1024]; // buffer 

     while(fis.read(byteBuffer)!= -1) 
     { 
      dos.write(byteBuffer); 
     } 
     OutputOptions(); 


     // dos.close(); 
     // fis.close(); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    finally 
    { 

    } 
} 

void OutputOptions() 
{ 
    System.out.println("Enter an option ('m', 'f', 'x'): "); 
    System.out.println("(M)essage (send)"); 
    System.out.println("(F)ile (request) "); 
    System.out.println("e(X)it "); 
} 


public void StandardInput() 
{ 
    OutputOptions(); 
    while(true) 
    { 

     try 
     { 
      // input the message from standard input 
      BufferedReader input2= new BufferedReader(new InputStreamReader(System.in)); 
      String line2 = ""; 

      option= input2.readLine(); 
      if(option.equals("m") || option.equals("M")) 
      { 
       StandardOutput(); 
      } 
      if(option.equals("f") || option.equals("F")) 
      { 
       FileTransferSend(); 
      } 
      if(option.equals("x") || option.equals("X")) 
      { 
       System.exit(0); 

      } 

     } 
     catch (Exception e) 
     { 
      System.out.println(e.getMessage()); 
     } 
     finally 
     { 

     } 
    } 

} 




} 

全部代碼的所有文件:

https://www.dropbox.com/s/0yq47gapsd3dgjp/folder33.zip?dl=0

我的問題是:我可以對哪些變化代碼,這將使我能夠訪問numberPart whi le在裏面Server

編輯:有沒有辦法來碰到一個沒有得到任何答案的問題,或者我應該刪除這個並重新發布它的地方?

+0

你確定你發佈了足夠的代碼嗎?請詳細解釋您遇到的問題 –

+0

@ScaryWombat上次我發佈了所有代碼,因爲我沒有太簡明。有5個文件,這裏是所有代碼的鏈接[https://www.dropbox.com/s/0yq47gapsd3dgjp/folder33.zip?dl=0]。 – hey

回答

1

我認爲你可以使用偵聽器或回調模式來解決這個問題。

(我失去了我的Java內存現在,我做C#,所以請多多包涵。)

​​

然後讓服務器類實現該接口

public Server implements PortAssignable { 
... 
} 

而且ServerReceive

// Constructor 
public ServerRecieve(Socket s, PortAssignable portNotifyListener) { 
    _portNotifyListener = portNotifyListener; 
    ... your other code ... 
} 

確保在創建ServerReceive的實例時,您將y我們的Server實例,通過this

ServerRecieve serverThread = new ServerRecieve(socket, this); 

現在,當你得到你numberPart,你的下一行可以

_portNotifyListener.assignPort(numberPart); 

你選擇如何實現你的Server類的assignPort方法是你。

P.S.我從/r/programming看到這個問題。

+0

謝謝。我會試試這個。 – hey

+0

「_portNotifyListener.assignPort(numberPart);'應位於Server類還是ServerRecieve類中? – hey

+0

在將值賦給'numberPart'後,在ServerReceive類中。 – user01CU812