在服务器-客户端套接字通信方面需要一些帮助

时间:2019-12-24 15:51:33

标签: java sockets client-server

我正在尝试在Java中的套接字上进行练习,并使客户端与服务器通信,我不明白为什么服务器卡在读取中,并且好像它没有从服务器接收消息。客户,实际上是客户发送的。 服务器正确查看了客户端,并且接受成功。怎么做?

服务器代码

ServerSocket server = null;
Socket client = null;
while(true) {
    try{
        server = new ServerSocket();
        server.bind(new InetSocketAddress("localhost", 2000));
        client = server.accept();
        break;
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }   
}

BufferedReader reader = null;
BufferedWriter writer = null;
try {
    reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
    writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

try {
    String message = null;
    while((message = reader.readLine()) != null) {
        System.out.println("Client send: " + message);
        String answer = "ok"; 
        writer.write(answer);
        writer.flush();

    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

try {
    writer.close();
    reader.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

客户代码

Socket socket = new Socket();
try {
    SocketAddress address = new InetSocketAddress("127.0.0.1",2000);
    socket.connect(address);
} catch (IOException e) {
    e.printStackTrace();
}

BufferedReader reader = null;
BufferedWriter writer = null;
try {       
    reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
try {
    String string = "hello";
    writer.write(string);
    writer.flush();
    System.out.print("sent");
    String message;
    while((message = reader.readLine()) != null) {
        System.out.println("Server send: " + message);
        break;
    }
}catch (SocketException e) {
    // TODO: handle exception
    System.out.println(e);

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

try {
    writer.close();
    reader.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

2 个答案:

答案 0 :(得分:0)

我的MRE:

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

public class Foo {
    public static void main(String[] args) {
        new Thread(() -> ServerFun.someServerMethod()).start();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {}
        new Thread(() -> ClientFun.someClientMethod()).start();
    }
}
class ClientFun {
    public static void someClientMethod() {
        Socket socket = new Socket();
        try {
            SocketAddress address = new InetSocketAddress("LocalHost", 2000);
            System.out.println("client socket address: " + address);
            socket.connect(address);
        } catch (IOException e) {
            e.printStackTrace();
        }

        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String string = "hello";
            writer.write(string);
            writer.newLine();
            writer.flush();
            System.out.print("sent");
            String message;
            while ((message = reader.readLine()) != null) {
                System.out.println("Server send: " + message);
                break;
            }
        } catch (SocketException e) {
            System.out.println(e);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            writer.close();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class ServerFun {
    public static void someServerMethod() {
        ServerSocket server = null;
        Socket client = null;
        // !!while (true) {  // not needed for a single client
            try {
                server = new ServerSocket();
                server.bind(new InetSocketAddress("localhost", 2000));
                System.out.println("server socket: " + server.getInetAddress());
                client = server.accept();
                // !! break;
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(-1);
            }
        // !! }
        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String message = null;
            while ((message = reader.readLine()) != null) {
                System.out.println("Client send: " + message);
                String answer = "ok";
                writer.write(answer);
                writer.newLine();
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            writer.close();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

您需要发送新行以使readLine方法起作用。例如,

writer.write(answer);
writer.newLine();  // add this **********
writer.flush();

这对于客户端和服务器都必须到位。或者,使用PrintWriter和out.println(answer);

我还必须使用“ localhost”来使客户端正常工作。

希望您在解决此问题时看到了MRE的实用程序。下次,我希望您能为我们创建一个,而不是强迫我们创建一个。

答案 1 :(得分:-3)

它是因为股票就像输入。如果要在服务器侦听客户端时执行其他操作,则等到有数据到达它时,才应使用多重编程。 在一个红色的Lestin里给客户,而在另一个上做其他事情