2015-10-08 137 views
0

這是我的服務器代碼:服務器沒有得到來自客戶端的數據 - JAVA

package ServerSideHammingCodeCheckingAndResponse; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class Server 
{  
private ServerSocket serverSocket; 
private int port; 

public Server(int port) 
{ 
    this.port = port; 
} 

public void start() throws IOException 
{ 
    System.out.println("Server starts at port:" + port); 
    serverSocket = new ServerSocket(port); 

    System.out.println("Waiting for client..."); 
    Socket client = serverSocket.accept(); 

    sendMessage(client, "This is Hamming Code Checking."); 

    boolean checkInput = false; 
    String input = null; 
    while (!checkInput) 
    { 
     input = getMessage(client); 

     if(input.length() == 7 && input.matches("[01]+")) 
      checkInput = true; 
     else 
      sendMessage(client, "invalid"); 
    } 
    sendMessage(client, input); 
} 

private void sendMessage(Socket client, String message) throws IOException 
{ 
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream())); 

    writer.write(message); 
    writer.flush(); 
    writer.close(); 
} 

private String getMessage(Socket client) throws IOException 
{ 
    String userInput; 
    BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream())); 
    userInput = reader.readLine(); 
    return userInput; 
} 

public static void main(String[] args) 
{ 
    int portNumber = 9987; 

    try { 
     Server socketServer = new Server(portNumber); 
     socketServer.start(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 

這是我的客戶端代碼:

package ClientSideDataTransmitter; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.Scanner; 

public class Client 
{ 
private String hostname; 
private int port; 
Socket socketClient; 

public Client(String hostname, int port) 
{ 
    this.hostname = hostname; 
    this.port = port; 
} 

public void connect() throws UnknownHostException, IOException 
{ 
    System.out.println("Attempting to connect to " + hostname + ":" + port); 
    socketClient = new Socket(hostname, port); 
    System.out.println("\nConnection Established."); 
} 

public void readResponse() throws IOException 
{ 
    String userInput; 
    BufferedReader reader = new BufferedReader(new InputStreamReader(socketClient.getInputStream())); 

    System.out.print("Response from server: "); 
    while ((userInput = reader.readLine()) != null) { 
     System.out.println(userInput); 
    } 
} 

public void sendData() throws IOException 
{ 
    Scanner sc = new Scanner(System.in); 
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socketClient.getOutputStream())); 
    System.out.println("Enter a 7-bits binary as message to server:\n"); 
    String input = sc.nextLine(); 
    writer.write(input); 
    writer.flush(); 
} 

public static void main(String arg[]) 
{ 
    Client client = new Client ("localhost", 9987); 
    try { 
     client.connect(); 
     client.readResponse(); 
     client.sendData(); 
     client.readResponse(); 
    } catch (UnknownHostException e) { 
     System.err.println("Host unknown. Cannot establish connection"); 
    } catch (IOException e) { 
     System.err.println("Cannot establish connection. Server may not be up." + e.getMessage()); 
    } 
} 
} 

我還沒有完成了代碼,請忽略小錯誤碼。

當我啓動服務器,然後客戶端,併發送輸入從客戶端到服務器。服務器似乎沒有從客戶端獲取數據,因爲我將該輸入從服務器發送回客戶端以將其打印出來,但它不打印任何內容。

我認爲服務器代碼中getMessage()方法可能存在問題,但我無法修復它。請幫我修復代碼。非常感謝!

服務器:

Server starting at port 9987 
Waiting for client... 

客戶:

Attempting to connect to localhost:9987 

Connection Established. 
Response from server: This is Hamming Code Checking. 
Enter a 7-bits binary as message to server: 

1234567 
Response from server: 

回答

0

在Shriram的建議之上,我建議您在這裏使用PrintWriter來避免關閉連接。使用起來也比較方便。這裏的工作示例:

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

public class Server { 
    private ServerSocket serverSocket; 
    private int port; 

    public Server(int port) { 
     this.port = port; 
    } 

    public void start() throws IOException { 
     System.out.println("Server starts at port:" + port); 
     serverSocket = new ServerSocket(port); 

     System.out.println("Waiting for client..."); 
     Socket client = serverSocket.accept(); 

     sendMessage(client, "This is Hamming Code Checking."); 

     boolean checkInput = false; 
     String input = null; 
     while (!checkInput) { 
      input = getMessage(client); 

      if (input.length() == 7 && input.matches("[01]+")) 
       checkInput = true; 
      else 
       sendMessage(client, "invalid"); 
     } 
     sendMessage(client, input); 
    } 

    private void sendMessage(Socket client, String message) throws IOException { 
     PrintWriter writer = new PrintWriter(client.getOutputStream(), true); 
     writer.println(message); 
    } 

    private String getMessage(Socket client) throws IOException { 
     String userInput; 
     BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream())); 
     userInput = reader.readLine(); 
     return userInput; 
    } 

    public static void main(String[] args) { 
     int portNumber = 9987; 

     try { 
      Server socketServer = new Server(portNumber); 
      socketServer.start(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

客戶:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.Scanner; 

public class Client { 
    private String hostname; 
    private int port; 
    Socket socketClient; 

    public Client(String hostname, int port) { 
     this.hostname = hostname; 
     this.port = port; 
    } 

    public void connect() throws UnknownHostException, IOException { 
     System.out.println("Attempting to connect to " + hostname + ":" + port); 
     socketClient = new Socket(hostname, port); 
     System.out.println("\nConnection Established."); 
    } 

    public void readResponse() throws IOException { 
     String userInput; 
     BufferedReader reader = new BufferedReader(new InputStreamReader(socketClient.getInputStream())); 

     System.out.print("Response from server: "); 
     userInput = reader.readLine(); 
     System.out.println(userInput); 
    } 

    public void sendData() throws IOException { 
     Scanner sc = new Scanner(System.in); 
     PrintWriter writer = new PrintWriter(socketClient.getOutputStream(), true); 
     System.out.println("Enter a 7-bits binary as message to server:\n"); 
     String input = sc.nextLine(); 
     writer.println(input); 
    } 

    public static void main(String arg[]) { 
     Client client = new Client("localhost", 9987); 
     try { 
      client.connect(); 
      client.readResponse(); 
      client.sendData(); 
      client.readResponse(); 
     } catch (UnknownHostException e) { 
      System.err.println("Host unknown. Cannot establish connection"); 
     } catch (IOException e) { 
      System.err.println("Cannot establish connection. Server may not be up." + e.getMessage()); 
     } 
    } 
} 
+0

代碼現在停止打印「這是漢明碼檢查」。客戶端上的sendData()方法不運行。下一步如何解決它? –

0

與您的代碼的問題是writer.close()sendMessage()將在內部關閉編寫的對象。您的服務器程序將被關閉,不再接受連接。

+0

我嘗試刪除sendMessage()中的writer.close(),並停止在客戶端建立連接? –

+0

這是因爲while((userInput = reader.readLine())!= null)行。因爲在讀取服務器的響應之後再連續等待循環。您在編程中沒有使用多線程。最好使用不同的線程來寫和讀。 – Shriram

0

你創建每封郵件的新BufferedReader丟失數據。您需要在套接字的生命週期中使用相同的一個。同上'PrintWriter`或任何你用來發送。

相關問題