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 Server - Fatal编程技术网

Java 从套接字读取时获取空字符串

Java 从套接字读取时获取空字符串,java,sockets,client-server,Java,Sockets,Client Server,我试图用java编写一个使用套接字的客户机-服务器系统,但是我似乎无法读取从服务器发送到客户机的数据 以下是客户端的代码: public class ClientSocket { Socket clientSocket = null; PrintWriter out = null; BufferedReader in = null; // establish a connection to All Care's server application thro

我试图用java编写一个使用套接字的客户机-服务器系统,但是我似乎无法读取从服务器发送到客户机的数据

以下是客户端的代码:

public class ClientSocket 
{
    Socket clientSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;


    // establish a connection to All Care's server application through socket 4444 (adjust localhost to reflect the IP address that the server
    // is being run from)
    public ClientSocket()
    {
        try
        {
            clientSocket = new Socket("localhost", 4445);

            out = new PrintWriter(clientSocket.getOutputStream());
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        }
        catch (IOException e)
        {
            System.out.println("Could not connect to All Care Server Application");
        }
    }

    public void closeClientSocket()
    {   
        try
        {
            clientSocket.close();
        }
        catch (IOException e)
        {
            System.out.println("Could not close connection to All Care Server Application");
        }
    }

    public String getMessageFromServer()
    {
        try
        {
            String input = in.readLine();

            return input;
        }
        catch (IOException e)
        {
            System.out.println("Could not read message from server");
        }

        return "No Data";
    }

    public void sendMessageToServer(String message)
    {
        out.write(message);
    }
}
以下是服务器代码:

public class ArFileServer {

    public static void main(String[] args) 
    {
        ServerSocket serverSocket = null;
        boolean listening = true;

        try
        {
            serverSocket = new ServerSocket(4445);

            // infinite loop to continually listen for connection requests made by clients
            while (listening)
            {
                new ClientConnection(serverSocket.accept()).start();

                if (serverSocket != null)
                {
                    System.out.println("Connection to client established");
                }
            }

            serverSocket.close();
        }
        catch (IOException e)
        {
            System.out.println("Error could not create socket connection to port");
        }
    }
}

public class ClientConnection extends Thread
{
    private Socket socket = null;

    public ClientConnection(Socket socket) 
    {
        super("ClientConnection");
        this.socket = socket; 
    }

    // the thread that runs after a connection to the server has been accepted
    @Override
    public void run()
    {
        try
        {
            PrintWriter out = new PrintWriter(socket.getOutputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            sendMessagetoClient(out, "CONNECTION SUCCESS");

            // check login credentials sent from client to the server

            // if valid send back their encrypted password, otherwise output a login error message

            // wait for user input and then do various processes based on their requests

            in.close();
            out.close();
            socket.close();
        }
        catch (IOException e)
        {
            System.out.println("Client socket connection error");
        }
    }

    // sends a message to the client
    void sendMessagetoClient(PrintWriter out, String message)
    {
        out.write(message);
    } 

    // listens for a message from the client
    String getMessageFromClient(BufferedReader in)
    {      
        try
        {
            String input = in.readLine();
            return input;
        }
        catch (IOException e)
        {
            System.out.println("Could not read message from client");
        }

        return "No Data";
    }
这是我用来查看数据是否被发送的代码行

System.out.println(clientSocket.getMessageFromServer());
在clint代码中,您没有连接到服务器套接字。 用于clint插座连接

在clint代码中,您没有连接到服务器套接字。 用于clint插座连接


sendMessageToClient()
方法中,需要刷新:

void sendMessagetoClient(PrintWriter out, String message)
{
    out.write(message);
    out.flush();
} 

或者,当您创建
PrintWriter
时,将构造函数与
autoflush
一起使用:

PrintWriter out = new PrintWriter(socket.getOutputStream(),true);

当你写的时候,不要写出来。写(消息)使用
printf()
println()

在你的
sendMessageToClient()方法中,你需要刷新:

void sendMessagetoClient(PrintWriter out, String message)
{
    out.write(message);
    out.flush();
} 

或者,当您创建
PrintWriter
时,将构造函数与
autoflush
一起使用:

PrintWriter out = new PrintWriter(socket.getOutputStream(),true);

当你写的时候,不要写出来。写(消息)
使用
printf()
println()

这里有几个问题

  • 你在读台词,但不是在写台词

  • 您没有检查readLine()的结果是否为null,这意味着对等方已经关闭了连接,这意味着您必须执行同样的操作

  • 在编写之后,您没有刷新
    PrintWriter

  • 你把事情按错误的顺序结束了。必须关闭连接到套接字的输出写入程序/流。这样做会刷新它,然后关闭输入流/读取器和套接字。按错误的顺序操作会失去冲水功能。关闭输出后,不需要关闭其他两个

  • 您使用的是
    PrintWriter,
    ,它通过网络接收异常,您需要了解通信中的异常和错误,并且也不检查错误。使用
    BufferedWriter.


  • 这里有几个问题

  • 你在读台词,但不是在写台词

  • 您没有检查readLine()的结果是否为null,这意味着对等方已经关闭了连接,这意味着您必须执行同样的操作

  • 在编写之后,您没有刷新
    PrintWriter

  • 你把事情按错误的顺序结束了。必须关闭连接到套接字的输出写入程序/流。这样做会刷新它,然后关闭输入流/读取器和套接字。按错误的顺序操作会失去冲水功能。关闭输出后,不需要关闭其他两个

  • 您使用的是
    PrintWriter,
    ,它通过网络接收异常,您需要了解通信中的异常和错误,并且也不检查错误。使用
    BufferedWriter.


  • 请参见
    ClientSocket()
    constructor:
    ClientSocket=newsocket(“localhost”,4445)中的行是的,我确实连接,我甚至让服务器在连接被接受时打印。要清楚ClientConnection是服务器代码(即当客户端连接被接受时服务器运行的线程),请参阅
    ClientSocket()
    构造函数中的行:
    ClientSocket=new Socket(“localhost”,4445)是的,我确实连接,我甚至在连接被接受时让服务器打印。为了澄清ClientConnection是服务器代码(即当客户端连接被接受时服务器运行的线程)@MatthewPigram是的,我编辑了答案,将其作为可能的修复。但是调用
    out.write()
    不会自动刷新,您需要调用
    printf()
    println()
    来自动刷新。啊,非常感谢,我确实尝试过使用println,想知道为什么它会工作,这是因为我没有自动刷新!Ty@MatthewPigram是的,我编辑了答案,将其作为可能的修复。但是调用
    out.write()
    不会自动刷新,您需要调用
    printf()
    println()
    来自动刷新。啊,非常感谢,我确实尝试过使用println,想知道为什么它会工作,这是因为我没有自动刷新!泰