2011-05-20 68 views
0

我正在寫我的第一個套接字項目,我有一個問題。當客戶端向服務器發送字符串時,服務器無法獲得此消息。爲什麼此套接字程序不傳輸數據?

public class ClientActivity { 

    public static final String serverIP = "127.0.0.1"; 
     private Socket clientSocket = null; 
     private static BufferedOutputStream out = null; 

    public static void main(String[] args) throws IOException { 
     ClientActivity me = new ClientActivity(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     out.write("Some string"); 
    } 

    public ClientActivity() throws UnknownHostException, IOException { 
      clientSocket = new Socket(serverIP, serverPort); 
      out = new BufferedOutputStream(clientSocket.getOutputStream()); 
    } 
} 

這裏是服務器的代碼:

public class DataTransferServer { 

    private ServerSocket serverSocket; 
    private ArrayList<DataReaderThread> workingThreads; 
    private Boolean isListening; 
    private Thread listener; 

    public static void main(String[] args) throws IOException { 
     new DataTransferServer(IProtocolConstants.SERVER_PORT).startServer(); // FIXME 
    } 

    public DataTransferServer (int port) throws IOException { 
     System.out.println("Launch"); 
     serverSocket = new ServerSocket(port); 
     workingThreads = new ArrayList<DataReaderThread>(); 
     isListening = true; 
     listener = new ConnectionListener(); 
    } 

    public void startServer() throws IOException { 
     listener.start(); 
    } 

    private class ConnectionListener extends Thread { 
     @Override 
     public void run() { 
      DataReaderThread newThread = null; 
      while (isListening) { 
       try { 
        newThread = new DataReaderThread(serverSocket.accept(), DataTransferServer.this); 
        newThread.start(); 
        workingThreads.add(newThread); 
        System.out.println("! thread started"); 
       } catch (IOException e) { 
        e.printStackTrace(); // FIXME 
       } 
      } 
      if (newThread != null) { 
       newThread.interrupt(); 
       newThread = null; 
      } 
     } 
    } 


} 

這裏是聽衆:

public class DataReaderThread extends Thread { 
    private Socket socket = null; 
    private BufferedInputStream in = null; 
    private byte[] stuffBytes = null; 

    public DataReaderThread(Socket socket) throws IOException { 
     super("DataReaderThread"); 
     this.socket = socket; 
     in = new BufferedInputStream(socket.getInputStream()); 
stuffBytes = new byte[10]; 
    } 

    public void run() { 
     try { 
      while (in.read(stuffBytes, 0, stuffBytes.length) != -1) { 
System.out.println("Received data:" + new String(stuffBytes)); 
      } 
      in.close(); 
      socket.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public Socket getSocket() { 
     return socket; 
    } 
+0

當靜態main引用'out'(它是ClientActivity me實例中的成員)時,您如何獲得ClientActivity編譯? – Andrew 2011-05-20 21:44:30

+0

對不起,我的失敗,編輯 – davs 2011-05-20 21:46:34

回答

3

我猜BufferedOutputStream out在緩衝區滿之前不會發送TCP數據包。

嘗試調用writeflush將數據立即發送:

out.write("Some string"); 
out.flush(); 

您也可以考慮使用PrintWriter類,而不是,它有一個自動沖洗機制。

+0

非常感謝!但在將來我想發送byte [] ....所以我不能使用PrintWriter來處理我的情況(因爲它至少可以傳遞char [],String ...) – davs 2011-05-20 21:44:51

2

-1發佈前編譯代碼...

public class ClientActivity { 

    public void write(String data) { 
     out.write(data.getBytes()) 
     out.flush() 
    } 

    public static final String serverIP = "127.0.0.1"; 
     private Socket clientSocket = null; 
     private BufferedOutputStream out = null; 

    public static void main(String[] args) throws IOException { 
     ClientActivity me = new ClientActivity(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     me.write("Some string"); 
    } 

    public ClientActivity() throws UnknownHostException, IOException { 
      clientSocket = new Socket(serverIP, serverPort); 
      out = new BufferedOutputStream(clientSocket.getOutputStream()); 
    } 
}