java - ServerSocket 无法读取客户端的输入

标签 java sockets

我是套接字编程的新手,我只是尝试一些东西。我想做的是让客户端读取文本文件,将该文件中的行保存在 ArrayList 中,然后将这些行发送到服务器。这是我的代码。连接成功建立,但是当服务器尝试从他的BufferedReader中读取时,却什么也得不到:

服务器:

import java.io.*;
import java.net.*;

    public class Server extends Thread{
        ServerSocket sock = null;
        Socket client_sock = null;
        PrintWriter out = null;
        BufferedReader in = null;


        Server(int port){
            try{
            sock = new ServerSocket(port);
        }catch(IOException e){
            System.out.println("Couldn't create server socket");
            e.printStackTrace();
        }
        }

        public void run(){
            while (true){
                try{
                    client_sock = sock.accept();
                    System.out.println("Successfully connected to client" + client_sock);
                    System.out.println(client_sock.getOutputStream());
                    System.out.println(client_sock.getInputStream());
                    out = new PrintWriter(client_sock.getOutputStream(),true);
                    //System.out.println(out);
                    in = new BufferedReader(new InputStreamReader(client_sock.getInputStream()));
                    //System.out.println(in);
                    System.out.println("Trying to read line sent from client:");
                    String l;
                    try{    
                        l = in.readLine();
                        System.out.println(l);
                    }catch(IOException e){
                        System.out.println("Couldn't read line from client");
                        e.printStackTrace();}

                }catch(IOException e){
                    e.printStackTrace();
                    break;
                }
            }



        }

        public static void main(String[] args){
            //Thread t = new Server(Integer.parseInt(args[0]));
            //t.start();
            Server serv = new Server(10239);
            System.out.println(serv.sock);
            serv.run();

        }


    }

客户:

import java.net.*;
import java.io.*;
import java.util.*;

public class Client {
    Socket sock = null;
    OutputStream toServer = null;
    PrintWriter out = null;
    InputStream fromServer = null;
    BufferedInputStream in = null;
    Client(int port){
        try{
            sock = new Socket("localhost",port);
            //System.out.println(sock.getPort());
            //System.out.println(sock.getOutputStream());
            //System.out.println(sock.getInputStream());
            //toServer = sock.getOutputStream();
            //System.out.println(sock.getOutputStream());
            out = new PrintWriter(sock.getOutputStream());
            //System.out.println(out);
            //fromServer = sock.getInputStream();
            in = new BufferedInputStream(sock.getInputStream());
            //System.out.print(in);
        }catch(UnknownHostException ue){
            System.out.println("Host not known");
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        Client client = new Client(Integer.parseInt(args[0]));
        File f = new File("/Users/--------/Desktop/socket_test.txt");
        BufferedReader f_in = null;
        try{    
            f_in = new BufferedReader(new FileReader(f));
        }catch(IOException e){
            System.out.println("Cannot create FileReader for test file");
        }

        String line;
        ArrayList<String> text = new ArrayList<String>();
        try{
            while ((line = f_in.readLine()) != null){
                text.add(line);
            }
        }catch(IOException e){
            e.printStackTrace();
        }

        //System.out.println("first line of file");
        //System.out.println(text.get(0));


        for (String l : text){
            System.out.println("Sent the following line:");
            System.out.println(l);
            client.out.println(l);
        }

    }
}   

这是我为客户端获得的输出:

Sent the following line:
Similar to the previous constructor 
Sent the following line:
the InetAddress parameter specifies 
Sent the following line:
the local IP address to bind to. 
Sent the following line:
The InetAddress is used for servers that 
Sent the following line:
may have multiple IP addresses
Sent the following line:
allowing the server to specify which of 
Sent the following line:
its IP addresses to accept client requests on

这对于服务器来说:

ServerSocket[addr=0.0.0.0/0.0.0.0,localport=10239]
Successfully connected to clientSocket[addr=/127.0.0.1,port=58285,localport=10239]
Trying to read line sent from client:
null

我找不到这不起作用的原因,有人可以帮助我吗?

最佳答案

尝试在每行之后刷新流:

client.out.println(l);
client.out.flush();

关于java - ServerSocket 无法读取客户端的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34091415/

相关文章:

java - 对方付费电话要 map 吗?

java - 在 Grails 2.4 中使用 HTTPClient

c - 将数据写入以 2 帧发送的套接字

java - 使用 SqlRowSet 获取 Clob Java Spring

java - 如何从我的 Facebook 应用程序发送电子邮件

java - 是否有提供 LDAP 样式解析的独立 Java 库?

java - 客户端/服务器套接字 Java(模式 MVC)未向客户端发送正确的信息

Java,通过套接字发送大文件消耗太多CPU周期并且速度慢

c++ - IPv4 和 IPv6 之间的混合模式通信

Linux UDP 套接字 : why select()?