2012-07-19 50 views
2

我想在java中使用套接字编写客户机 - 服务器系统,但是我似乎无法读取从服务器发送到客户机的数据。从套接字读取时获取空字符串

下面是客户端的代码:

public class ClientSocket 
{ 
    Socket clientSocket = null; 
    PrintWriter out = null; 
    BufferedReader in = null; 


    // establish a connection to All Care's server application through socket 4444 (adjust localhost to reflect the IP address that the server 
    // is being run from) 
    public ClientSocket() 
    { 
     try 
     { 
      clientSocket = new Socket("localhost", 4445); 

      out = new PrintWriter(clientSocket.getOutputStream()); 
      in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
     } 
     catch (IOException e) 
     { 
      System.out.println("Could not connect to All Care Server Application"); 
     } 
    } 

    public void closeClientSocket() 
    { 
     try 
     { 
      clientSocket.close(); 
     } 
     catch (IOException e) 
     { 
      System.out.println("Could not close connection to All Care Server Application"); 
     } 
    } 

    public String getMessageFromServer() 
    { 
     try 
     { 
      String input = in.readLine(); 

      return input; 
     } 
     catch (IOException e) 
     { 
      System.out.println("Could not read message from server"); 
     } 

     return "No Data"; 
    } 

    public void sendMessageToServer(String message) 
    { 
     out.write(message); 
    } 
} 

这里是服务器代码:

public class ArFileServer { 

    public static void main(String[] args) 
    { 
     ServerSocket serverSocket = null; 
     boolean listening = true; 

     try 
     { 
      serverSocket = new ServerSocket(4445); 

      // infinite loop to continually listen for connection requests made by clients 
      while (listening) 
      { 
       new ClientConnection(serverSocket.accept()).start(); 

       if (serverSocket != null) 
       { 
        System.out.println("Connection to client established"); 
       } 
      } 

      serverSocket.close(); 
     } 
     catch (IOException e) 
     { 
      System.out.println("Error could not create socket connection to port"); 
     } 
    } 
} 

public class ClientConnection extends Thread 
{ 
    private Socket socket = null; 

    public ClientConnection(Socket socket) 
    { 
     super("ClientConnection"); 
     this.socket = socket; 
    } 

    // the thread that runs after a connection to the server has been accepted 
    @Override 
    public void run() 
    { 
     try 
     { 
      PrintWriter out = new PrintWriter(socket.getOutputStream()); 
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

      sendMessagetoClient(out, "CONNECTION SUCCESS"); 

      // check login credentials sent from client to the server 

      // if valid send back their encrypted password, otherwise output a login error message 

      // wait for user input and then do various processes based on their requests 

      in.close(); 
      out.close(); 
      socket.close(); 
     } 
     catch (IOException e) 
     { 
      System.out.println("Client socket connection error"); 
     } 
    } 

    // sends a message to the client 
    void sendMessagetoClient(PrintWriter out, String message) 
    { 
     out.write(message); 
    } 

    // listens for a message from the client 
    String getMessageFromClient(BufferedReader in) 
    {  
     try 
     { 
      String input = in.readLine(); 
      return input; 
     } 
     catch (IOException e) 
     { 
      System.out.println("Could not read message from client"); 
     } 

     return "No Data"; 
    } 

这里是使用的是看代码IM的行,如果被发送的数据。

System.out.println(clientSocket.getMessageFromServer()); 

回答

2

在你sendMessageToClient()方法,你需要刷新:

void sendMessagetoClient(PrintWriter out, String message) 
{ 
    out.write(message); 
    out.flush(); 
} 

或者,当您创建PrintWriter,使用带有autoflush构造:

PrintWriter out = new PrintWriter(socket.getOutputStream(),true); 

而当你写,而不是out.write(message)使用printf()println()

+0

像一个魅力一样工作! autoflush会工作吗? – 2012-07-19 03:55:07

+0

@MatthewPigram是的,我编辑的答案包括作为一个可能的修复。但是调用'out.write()'不会自动刷新,你需要调用'printf()'或'println()'来自动刷新。 – 2012-07-19 03:59:08

+0

啊谢谢一堆,我曾尝试使用println,并想知道为什么它是npt工作,这是因为我不自动刷新!泰 – 2012-07-19 04:05:34

-2

在克林特代码,你不与服务器套接字连接。 为克林特套接字连接

socket soc= new socket ("server host ip",port); 
+1

见行'ClientSocket的()'构造函数:'ClientSocket的=新的Socket( “localhost” 的,4445);' – 2012-07-19 03:46:04

+1

叶I DO连接,我甚至得到服务器进行打印时,连接接受 – 2012-07-19 03:47:56

+1

要清楚ClientConnection是服务器代码(即当客户端连接被接受时由服务器运行的线程) – 2012-07-19 03:50:39

0

这里有几个问题。

  1. 您正在阅读的文章,但您并未撰写文章。

  2. 您不检查readLine()的结果为null,这意味着对等关闭了连接,这意味着您必须同样做。

  3. 写完后,您并没有冲洗PrintWriter

  4. 您正在以错误的顺序关闭东西。您必须关闭已连接到套接字的输出写入器/流。这样做会刷新它,然后关闭输入流/阅读器和套接字。以错误的顺序执行此操作会丢失同花。一旦你关闭了输出,你就不需要另外两个关闭。

  5. 您正在使用PrintWriter,,它通过网络吞服异常,您需要了解通信中的异常和错误,并且您也不检查错误。使用BufferedWriter.