简体   繁体   English

无法从套接字读取

[英]Cannot read from Socket

everybody! 大家! I'm trying to make my first Java apllication using Sockets. 我正在尝试使用套接字制作我的第一个Java应用程序。 And i'm having a trouble to read from Socket. 而且我在读取Socket时遇到麻烦。 Here's a source code for a ServerThread: 这是ServerThread的源代码:

public class ServerThread extends Thread{
private final int SERVER_PORT = 444;
private ServerSocket serv;
public ServerThread(){

    try {
        serv = new ServerSocket(SERVER_PORT);
    } catch (IOException ex) {
        Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
public void run(){
    while(true){
        try {
            Socket client = serv.accept();
            client.setSoTimeout(0);
            client.setTcpNoDelay(true);
            client.setSendBufferSize(65536);
            if(client.isConnected()){
                new Thread(new MsgSrv(client)).start();
                System.out.println("Client connected");
            }
        } catch (IOException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
} }   

And Here is for a class, which handles client connections: 这是一个用于处理客户端连接的类:

public class MsgSrv extends Thread{
private BufferedWriter out;
private BufferedReader in;
private boolean isFinished = false;
private String buff;
private Socket sock;
public MsgSrv(Socket sc){
    try {
        sock = sc;
        in = new BufferedReader(new InputStreamReader(sc.getInputStream()));
        out = new BufferedWriter(new OutputStreamWriter(sc.getOutputStream()));
    } catch (IOException ex) {
        Logger.getLogger(MsgSrv.class.getName()).log(Level.SEVERE, null, ex);
    }

}
public void run(){
    while(!isFinished){
        try {
            buff=in.readLine();
            System.out.println(buff);
            out.write("Hello");
        } catch (IOException ex) {
            Logger.getLogger(MsgSrv.class.getName()).log(Level.SEVERE, null, ex);
            isFinished=true;
        }
    }
}}

Client Application sends a string to a server, and trying to read an answer. 客户端应用程序将字符串发送到服务器,并尝试读取答案。 But nothing happens. 但是什么也没发生。 in.readLine returns nothing, and waiting for an answer from a server forever. in.readLine返回任何内容,并且永远等待服务器的答复。 Can you help me to solve a problem? 你能帮我解决一个问题吗? To read data i've already used DataInputStream, BufferedReader classes. 要读取数据,我已经使用了DataInputStream,BufferedReader类。 Source for a client app: 客户端应用程序的来源:

public class JavaApplication4 {

/**
 * @param args the command line arguments
 */
public static Socket connectSock;
public static PrintWriter out;
public static DataInputStream in;
public static void main(String[] args) {
    try {

        connectSock = new Socket("127.0.0.1", 444);
        if(connectSock.isConnected()){
            in = new DataInputStream(connectSock.getInputStream());
            out = new PrintWriter(connectSock.getOutputStream(), true);
            out.println("!hello!");
            out.flush();
            String s;
            while(true){
                s = in.readUTF();
                System.out.println(s);
            }

               }else
        {
            System.out.println("bad socket");
            System.exit(0);
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(JavaApplication4.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(JavaApplication4.class.getName()).log(Level.SEVERE, null, ex);
    }

}}

Like you use the PrintWriter object to sent the String use the Scanner object to read the String. 就像您使用PrintWriter对象发送String一样,使用Scanner对象读取String。 Like this. 像这样。

input = server.getInputStream();
Scanner scanner = new Scanner(input);

while (scanner.hasNextLine()) {
     s = (String) scanner.nextLine();
}

edit: 编辑:

If you change you client like this and make sure you have a break line at the end of you String ("\\n") when you use scanner.nextLine(); 如果您这样更改客户,并确保在使用scanner.nextLine();时在字符串末尾有一个换行符(“ \\ n” scanner.nextLine();

if(connectSock.isConnected()){           
        Scanner scanner = new Scanner(connectSock.getInputStream());
        out = new PrintWriter(connectSock.getOutputStream(), true);
        out.println("!hello!");
        out.flush();
        String s;
        while (scanner.hasNextLine()) {
            s = (String) scanner.nextLine();

        }

           }else
    {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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