简体   繁体   中英

Client Receives no data from Server

I am trying to make a texting application between multiple clients, and a single server. The only problem is, the client receives no data from the server. Here is the code for the client side classes.

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

And is the code for the server classes.

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

The send() method is called in ServerClient, but the client never advances past in.nextLine(), meaning it is not recieving any data. I can confirm, however, that the server does recieve data from the client, but not the other way around. Can anyone see why, because I certianly can't. Thank you.

I simply needed to start the thread in the ClientServer class. The client was receiving data. It's just that the thread responsible for reading that data was never started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM