Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 客户端未从服务器接收任何数据_Java_Sockets_Client_No Data - Fatal编程技术网

Java 客户端未从服务器接收任何数据

Java 客户端未从服务器接收任何数据,java,sockets,client,no-data,Java,Sockets,Client,No Data,我正在尝试在多个客户端和单个服务器之间创建一个文本应用程序。唯一的问题是,客户端没有从服务器接收数据。下面是客户端类的代码 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

我正在尝试在多个客户端和单个服务器之间创建一个文本应用程序。唯一的问题是,客户端没有从服务器接收数据。下面是客户端类的代码

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

包服务器;
导入java.io.IOException;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.util.ArrayList;
公共类服务器实现可运行{
私有布尔运行;
私人ArrayList客户;
专用int端口;
私有服务器套接字服务器套接字;
专用服务器(){
端口=9999;
运行=真;
clients=newarraylist();
布尔值=假;
当(!工作){
试一试{
serverSocket=新的serverSocket(端口);
工作=真实;
}捕获(IOE异常){
端口++;
}
}
System.out.println(“端口:”+端口);
螺纹=新螺纹(此);
thread.start();
}
公共静态void main(字符串[]args){
新服务器();
}
@凌驾
公开募捐{
(跑步时){
试一试{
Socket=serverSocket.accept();
添加(新的服务器客户端(socket,this));
}
捕获(IOE异常){
e、 printStackTrace();
}
}
}
收到公共无效消息(int clientNumber,字符串消息){
对于(int i=0;i
在ServerClient中调用send()方法,但客户端从未在.nextLine()中前进,这意味着它没有接收任何数据。但是,我可以确认服务器确实从客户机接收数据,但不是相反。有人知道为什么吗,因为我确实不知道。
谢谢。

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

isConnected()
不是有效的循环条件。它永远不会变成假的。您应该在每次调用
readLine()
时测试其结果是否为null。谢谢您让我知道。我改变了它,但它没有解决原来的问题。
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;
}
}