2012-02-26 144 views
0

http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html在步驟3中列出的套接字服務器建立乾淨(Java版本「1.7.0_02」),並沒有錯誤運行,但它退出沒有錯誤,而不是等待接受客戶。套接字服務器構建問題

更新的ChatServer缺少ARG代碼:

的ChatServer:

import java.net.*; 
import java.io.*; 

public class ChatServer implements Runnable 
{ private ServerSocket  server = null; 
    private Thread   thread = null; 
    private ChatServerThread client = null; 

    public ChatServer(int port) 
    { try 
     { System.out.println("Binding to port " + port + ", please wait ..."); 
     server = new ServerSocket(port); 
     System.out.println("Server started: " + server); 
     start(); 
     } 
     catch(IOException ioe) 
     { System.out.println(ioe); } 
    } 
    public void run() 
    { while (thread != null) 
     { try 
     { System.out.println("Waiting for a client ..."); 
      addThread(server.accept()); 
     } 
     catch(IOException ie) 
     { System.out.println("Acceptance Error: " + ie); } 
     } 
    } 
    public void addThread(Socket socket) 
    { System.out.println("Client accepted: " + socket); 
     client = new ChatServerThread(this, socket); 
     try 
     { client.open(); 
     client.start(); 
     } 
     catch(IOException ioe) 
     { System.out.println("Error opening thread: " + ioe); } 
    } 
    public void start() { 
    thread = new Thread(this); 
    thread.start(); 
} 
    public void stop()     { /* no change */ } 
    public static void main(String args[]) { 
     ChatServer server = null; 
     if (args.length != 1) 
     System.out.println("Usage: java ChatServer port"); 
     else 
     server = new ChatServer(Integer.parseInt(args[0])); 
} 
} 

ChatServerThread:

import java.net.*; 
import java.io.*; 

public class ChatServerThread extends Thread 
{ private Socket   socket = null; 
    private ChatServer  server = null; 
    private int    ID  = -1; 
    private DataInputStream streamIn = null; 

    public ChatServerThread(ChatServer _server, Socket _socket) 
    { server = _server; socket = _socket; ID = socket.getPort(); 
    } 
    public void run() 
    { System.out.println("Server Thread " + ID + " running."); 
     while (true) 
     { try 
     { System.out.println(streamIn.readUTF()); 
     } 
     catch(IOException ioe) { 
      System.out.println(ioe.getMessage()); 
     } 
     } 
    } 
    public void open() throws IOException 
    { streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream())); 
    } 
    public void close() throws IOException 
    { if (socket != null) socket.close(); 
     if (streamIn != null) streamIn.close(); 
    } 
} 
+0

'建立乾淨,但不run'燦你詳細說明一下? – 2012-02-26 04:21:10

+0

是的,沒有編譯錯誤,在執行時應該產生一個監聽器,等待客戶端連接,但它不,它來代替似乎消失,沒有任何錯誤。儘管參考頁面上的第一步確實有效。 – Astron 2012-02-26 04:22:36

+0

嘗試處理IOException異常的ChatServerThread run方法,並打印錯誤。如果客戶端關閉了套接字,那麼將會有一個無限循環,您不會注意到,因爲該異常沒有處理。 – 2012-02-26 05:20:10

回答

1

編輯:我的更新回答一個工作解決方案。

更改這些方法在你ChatServer類爲像這些

public void start() { 
    thread = new Thread(this); 
    thread.start(); 
} 

public void stop() { 
    // You should implement this too 
} 

public static void main(String args[]) { 
    // Instantiate a CharServer with the listening port 9191 
    ChatServer chatServer = new ChatServer(9191); 
    // CharServer.start() should not be confused with Thread.start(); 
    // This calls our custom method up above, which includes a call to 
    // Thread(ChatServer).start(); 
    chatServer.start(); 

} 

其中9191是我編了一個端口號。

執行CharServer #main方法產生下面的輸出,並保持活着

Binding to port 9191, please wait ... 
Server started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=9191] 
Waiting for a client ... 
Waiting for a client ... 

你也應該實施的功能緣故stop()方法。

+0

添加了推薦代碼,但未生成錯誤。 – Astron 2012-02-26 15:51:52

+0

@Astron我已經用適當的解決方案更新了我的答案。請檢查一下。 – 2012-02-26 19:17:06

+0

按照您的解決方案,我意識到了基本名字沒有出現在的例子。我將用更正的代碼更新問題。 – Astron 2012-02-26 20:28:10

0
{ while (thread != null) 

你從來沒有線程,以便它都將是空 而你從來沒有創建一個線程

嘗試改變start()方法來:

public void start()     { 
    thread = new Thread(this); 
    thread.start(); 
} 
+0

如上所述更改了start(),結果相同。該線程應該如何設置,使其不總是爲空? – Astron 2012-02-26 05:42:50

+0

抱歉有錯字,應該是thread = new Thread(this);已經更新了我的答案 – objects 2012-02-27 05:15:03