我正在尝试在多个客户端和单个服务器之间创建一个短信应用程序。唯一的问题是,客户端没有从服务器接收数据。这是客户端类的代码。
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(),这意味着它不会收到任何数据。但是,我可以确认服务器确实从客户端接收数据,但不是相反。任何人都可以看到原因,因为我无法理解。 谢谢。
答案 0 :(得分:0)
我只需要在ClientServer类中启动线程。客户端正在接收数据。只是负责读取数据的线程从未启动过。