2011-09-19 87 views
1

我很抱歉問這樣一個基本的問題,但我新的線程,socket編程。我正在嘗試編寫一個多線程套接字程序,但它總是給出錯誤。當我調試它時,我無法找到它爲什麼會給出這個愚蠢的錯誤。你能幫我解決我的錯誤嗎?如何在java中創建threadedsocket連接?

我的客戶端類:

public class Client { 

    public void run() throws IOException{ 
     Socket s = new Socket("localhost",9999); 
     PrintStream ps = new PrintStream(s.getOutputStream()); 
     ps.println(readFromConsole()); 
    } 

    public String readFromConsole(){ 
     String result ; 
     Scanner in = new Scanner(System.in); 
     result = in.nextLine(); 
     return result; 
    } 
    public static void main(String[] args) throws Exception { 
     Client c = new Client(); 
     c.run(); 
    } 
} 

我的服務器類:

public class Server { 
    ServerSocket ss; 
    public void run() throws IOException{ 
     ss = new ServerSocket(9999); 
     while(true){ 
      TestForThread t; 
      try { 
       t = new TestForThread(ss.accept()); 
       Thread testThread = new Thread(t); 
       testThread.start(); 
      } catch(IOException e){ 
       e.printStackTrace(); 
      } 
     } 
     //BufferedReader br = 
     //new BufferedReader(new InputStreamReader(sForAccept.getInputStream())); 
     //String temp = br.readLine(); 
     //System.out.println(runCommand(temp)); 
    } 
    protected void finalize() { 
     // Objects created in run method are finalized when 
     // program terminates and thread exits 
     try { 
      ss.close(); 
     } catch (IOException e) { 
      System.out.println("Could not close socket"); 
      System.exit(-1); 
     } 
    } 

    public static void main(String[] args) throws Exception{ 
     Server s = new Server(); 
     s.run(); 
    } 
} 

我的線程類::

public class TestForThread implements Runnable { 

    private Socket client; 
    public TestForThread(Socket c){ 
     client = c; 
    } 

    public void run() { 
     String line; 
     BufferedReader in = null; 
     PrintWriter out = null; 
     try { 
      in = new BufferedReader(new InputStreamReader(client 
       .getInputStream())); 
      out = new PrintWriter(client.getOutputStream(), true); 
     } catch (IOException e) { 
      System.out.println("in or out failed"); 
      System.exit(-1); 
     } 

     while (true) { 
      try { 
       line = in.readLine(); 
       String commandResult = runCommand(line); 
       System.out.println(commandResult); 
      } catch (IOException e) { 
       System.out.println("Read failed"); 
       System.exit(-1); 
      } 
     } 
    } 

    public String runCommand(String command) throws IOException { 
     Runtime runtime = Runtime.getRuntime(); 
     Process process = runtime.exec(command); // you might need the full path 
     InputStream is = process.getInputStream(); 
     InputStreamReader isr = new InputStreamReader(is); 
     BufferedReader br = new BufferedReader(isr); 
     String line; 
     String answer = ""; 
     while ((line = br.readLine()) != null) { 
      System.out.println(line); 
      answer = answer + line + "\n"; 
     } 
     return answer; 
    } 
} 

非常感謝你。

+0

我看不出哪裏是在你的程序中的線程。你只需從cmd讀取一行並將其發送到套接字 –

+0

@Dan Bizdadea:我在Server類中使用它。在我的跑步方法。 – mehmetozer

+1

你得到的錯誤是什麼? – Jesper

回答

1

在您的代碼,您使用的BufferedReader包裝從插座中的InputStream,並調用的readLine()來獲取客戶端發送的字符串。據http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html#readLine(), 的readLine()將等待 '\ n' 或 '\ r'。問題在於Client.readFromConsole(),因爲它不返回包含行終止字符的字符串。

通過添加「\ n」或「\ r」()方法,通過Client.readFromConsole返回的字符串中,是這樣的:

public String readFromConsole() { 
    String result ; 
    Scanner in = new Scanner(System.in); 
    result = in.nextLine(); 
    return result+System.getProperty("line.separator"); 
} 

服務器將不下去的第一個請求之後了。

+0

你能應該使用System.getProperty(「line.separator」)而不是「\ r」,使之更加系統無關? –

+0

@Ibrahim Arief:我不知道是否有這樣的事情存在,謝謝指出。是的,我認爲它更好。 – riza