从套接字读取时获取空字符串 [英] Getting Null String when reading from a socket

查看:95
本文介绍了从套接字读取时获取空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里是一个使用Sockets在Java中编写客户端 - 服务器系统,但是我不能读取从服务器发送到客户端的数据。客户代码:

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


//通过套接字4444建立到All Care服务器应用程序的连接(调整localhost以反映服务器
//正在运行的IP地址)
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(无法连接到所有Care Server应用程序);
}
}

public void closeClientSocket()
{
try
{
clientSocket.close
}
catch(IOException e)
{
System.out.println(无法关闭与所有Care Server应用程序的连接);
}
}

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

返回输入;
}
catch(IOException e)
{
System.out.println(无法从服务器读取消息);
}

返回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);

//无限循环继续监听客户端的连接请求
while(监听)
{
new ClientConnection(serverSocket.accept())。start );

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

serverSocket.close();
}
catch(IOException e)
{
System.out.println(错误无法创建套接字连接到端口);
}
}
}

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

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

//连接到服务器后运行的线程已被接受
@Override
public void run()
{
try
{
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

sendMessagetoClient(out,CONNECTION SUCCESS);

//检查从客户端发送到服务器的登录凭据

//如果有效,则返回其加密密码,否则输出登录错误消息

//等待用户输入,然后根据他们的请求进行各种进程

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

//向客户端发送消息
void sendMessagetoClient(PrintWriter out,String message)
{
out。 write(message);
}

//监听来自客户端的消息
String getMessageFromClient(BufferedReader in)
{
try
{
String input = in.readLine();
return input;
}
catch(IOException e)
{
System.out.println(无法从客户端读取消息);
}

返回No Data;
}

这里是代码行im使用查看数据是否正在发送。

  System.out.println(clientSocket.getMessageFromServer());在您的 sendMessageToClient()中,


解决方案

方法,您需要刷新:

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






PrintWriter ,使用 autoflush 的构造函数:

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

当你写,而不是 out.write code>使用 printf() println()


I am trying to write a client-server system using Sockets in java, however I cannot seem to read data sent from the server to the client.

Here is the code for the client:

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

And here is the Server code:

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

And here is the line of code im using to see if the data is being sent.

System.out.println(clientSocket.getMessageFromServer());

解决方案

In your sendMessageToClient() method, you need to flush:

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


Or, when you create the PrintWriter, use the constructor with autoflush:

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

And when you write, instead of out.write(message) use printf() or println().

这篇关于从套接字读取时获取空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆