2017-03-03 136 views
0

我正在嘗試在多個客戶端和單個服務器之間創建一個發短信應用程序。唯一的問題是,客戶端沒有收到來自服務器的數據。這裏是客戶端類的代碼。客戶端無法從服務器接收數據

package client; 

import java.io.IOException; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.ArrayList; 
import java.util.Scanner; 

public class Client implements Runnable{ 
private ClientServer server; 
private Scanner in; 
private Thread thread; 
private boolean running; 
private ArrayList<String> messages; 

private Client(){ 
    in = new Scanner(System.in); 
    running = true; 
    thread = new Thread(this); 
    thread.start(); 
    messages = new ArrayList<>(); 
} 
public static void main(String[] args) { 
    new Client(); 
} 

@Override 
public void run() { 
    boolean worked = false; 
    while (!worked){ 
     System.out.println("Type in the server id."); 
     String input = in.nextLine(); 
     try { 
      int number = Integer.parseInt(input); 
      try{ 
       server = new ClientServer(new Socket(InetAddress.getLocalHost().getHostAddress(), number), this); 
       worked = true; 
      } 
      catch (UnknownHostException e){ 
       System.out.println("Could not connect. Unknown Host."); 
      } 
      catch (IOException e){ 
       System.out.println("Could not connect. Invalid id."); 
      } 
     } 
     catch (NumberFormatException e){ 
      System.out.println("That was not a number."); 
     } 
    } 

    System.out.println("Username: "); 
    server.send(in.nextLine()); 
    while (running){ 
     for (String message : messages){ 
      System.out.println(message); 
     } 
     messages.clear(); 
     server.send(in.nextLine()); 
    } 
} 

public void print(String message){ 
    System.out.println("Working"); 
    messages.add(message); 
} 
} 

package client; 

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

public class ClientServer implements Runnable{ 
    private PrintStream out; 
    private BufferedReader in; 
    private Socket server; 
    private Thread thread; 
    private Client client; 
     public ClientServer(Socket server, Client client){ 
     this.server = server; 
     this.client = client; 
     thread = new Thread(this); 
     try { 
      out = new PrintStream(server.getOutputStream()); 
      in = new BufferedReader(new InputStreamReader(server.getInputStream())); 
     } 
     catch (IOException e){ 
     e.printStackTrace(); 
    } 
} 

@Override 
public void run() { 
    while (server.isConnected()){ 
     try { 
      client.print(in.readLine()); 
     } catch (IOException e){ 
      e.printStackTrace(); 
     } 
    } 
} 

public void send(String message){ 
    out.println(message); 
} 
} 

而且是服務器類的代碼。

package server; 

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

public class ServerClient implements Runnable { 
    private Socket client; 
    private PrintStream out; 
    private BufferedReader in; 
    private Server server; 
    private String clientName; 
    private Thread clientThread; 

    public ServerClient(Socket client, Server server){ 
     this.client = client; 
     this.server = server; 
     try { 
      out = new PrintStream(client.getOutputStream()); 
      in = new BufferedReader(new InputStreamReader(client.getInputStream())); 
    } catch (IOException e){ 
     e.printStackTrace(); 
    } 
    clientThread = new Thread(this); 
    clientThread.start(); 
} 

@Override 
public void run() { 
    try { 
     clientName = in.readLine(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    out.println("test"); 
    boolean running = true; 
    while (client.isConnected() && running) { 
     try { 
      server.messageReceived(server.getClients().indexOf(this), clientName + ": " + in.readLine()); 
     } catch (IOException e){ 
      e.printStackTrace(); 
     } 
    } 
    try { 
     client.close(); 
     server.getClients().remove(this); 
     clientThread.join(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (InterruptedException e){ 
     e.printStackTrace(); 
    } 
} 

public void send(String message) { 
    out.println(message); 
    System.out.println("Working"); 
} 
} 

package server; 

import java.io.IOException; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.ArrayList; 

public class Server implements Runnable { 
private boolean running; 
private ArrayList<ServerClient> clients; 
private int port; 
private ServerSocket serverSocket; 

private Server(){ 
    port = 9999; 
    running = true; 
    clients = new ArrayList<>(); 
    boolean worked = false; 
    while (!worked) { 
     try { 
      serverSocket = new ServerSocket(port); 
      worked = true; 
     } catch (IOException e) { 
      port++; 
     } 
    } 
    System.out.println("Port: " + port); 
    Thread thread = new Thread(this); 
    thread.start(); 
} 

public static void main(String[] args) { 
    new Server(); 
} 

@Override 
public void run() { 
    while(running){ 
     try { 
      Socket socket = serverSocket.accept(); 
      clients.add(new ServerClient(socket, this)); 
     } 
     catch (IOException e){ 
      e.printStackTrace(); 
     } 
    } 
} 

public void messageReceived(int clientNumber, String message){ 
    for (int i = 0; i < clients.size(); i++) { 
     if(i != clientNumber) clients.get(i).send(message); 
    } 
} 

public ArrayList<ServerClient> getClients() { 
    return clients; 
} 
} 

的send()方法被調用ServerClient,但客戶端永遠前進經過in.nextLine(),這意味着它不recieving任何數據。但是,我可以確認服務器確實從客戶端接收數據,但不是相反。任何人都可以看到爲什麼,因爲我certianly不能。 謝謝。

+0

'isConnected()'不是有效的循環條件。它永遠不會變成錯誤的。您應該在每次調用readLine()時將結果測試爲null。 – EJP

+0

謝謝你讓我知道。我改變了它,但它沒有解決原來的問題。 –

回答

0

我只需要在ClientServer類中啓動線程。客戶端正在接收數據。只是負責讀取數據的線程從未開始。

相關問題