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;
}
}

在 ServerClient 中调用 send() 方法,但客户端永远不会超过 in.nextLine(),这意味着它没有接收任何数据。但是,我可以确认服务器确实从客户端接收数据,但不是相反。谁能明白为什么,因为我确实看不到。谢谢你。

4

1 回答 1

0

我只需要在 ClientServer 类中启动线程。客户端正在接收数据。只是负责读取该数据的线程从未启动过。

于 2017-03-03T15:57:50.967 回答