客户端 - 服务器消息交换 - Java中的套接字

时间:2017-02-27 11:49:45

标签: java sockets

我试图用回声回答客户端消息,但我不知道如何做。我的客户发送了一条消息,但随后我撤消了文件并且程序没有继续(陷入困境

DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());

服务器)。我想这是关闭输入流,但是当我这样做时,关闭套接字,我无法重用它。我已经设法用readUTF和socket.shutDownInput来做这件事,但我想要一个最简单的方法 - 比如close或flush,我没有得到。

客户端

public class SocketClient {

public static void main( String[] args ) throws IOException {
    // Check arguments
    if (args.length < 3) {
        System.err.println("Argument(s) missing!");
        System.err.printf("Usage: java %s host port file%n", SocketClient.class.getName());
        return;
    }

    String host = args[0];
    // Convert port from String to int
    int port = Integer.parseInt(args[1]);
    // Concatenate arguments using a string builder
    StringBuilder sb = new StringBuilder();
    for (int i = 2; i < args.length; i++) {
        sb.append(args[i]);
        if (i < args.length-1) {
            sb.append(" ");
        }
    }
    String text = sb.toString();

    // Create client socket
    Socket socket = new Socket(host, port);
    System.out.printf("Connected to server %s on port %d %n", host, port);

    // Create stream to send data to server
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());

    // Send text to server as bytes
    out.writeBytes(text);
    out.writeBytes("\n"); // devia meter readline a null...

    System.out.println("Sent text: " + text);

    System.out.println(socket.getInetAddress().getHostAddress() + " " + socket.getPort());

    //out.close();
    //socket.shutdownOutput(); trials...
    ////////////////////////////////////////////

    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    System.out.println("here");
    // Receive data until client closes the connection
    String response;
    while ((response = in.readLine()) != null)
        System.out.printf("Received message with content: '%s'%n", response); // here is where client doesn't proceed - he doesn't get the echo

服务器

public class SocketServer {

public static void main( String[] args ) throws IOException {
// Check arguments
if (args.length < 1) {
System.err.println("Argument(s) missing!");
System.err.printf("Usage: java %s port%n", SocketServer.class.getName());
        return;
    }

    // Convert port from String to int
    int port = Integer.parseInt(args[0]);

    // Create server socket
    ServerSocket serverSocket = new ServerSocket(port);
    System.out.printf("Server accepting connections on port %d %n", port);

    // wait for and then accept client connection
    // a socket is created to handle the created connection
    Socket clientSocket = serverSocket.accept();
    System.out.printf("Connected to client %s on port %d %n",
        clientSocket.getInetAddress().getHostAddress(), clientSocket.getPort());

    // Create stream to receive data from client
    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    // Receive data until client closes the connection
    String response;
    while ((response = in.readLine()) != null)
        System.out.printf("Received message with content: '%s'%n", response);

    /////////////////////////////////////////////


    DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream()); // he doesn't reach here

    System.out.println("here-serv");

    System.out.println(clientSocket.getInetAddress().getHostAddress() + " " + clientSocket.getPort());
    // Send text to server as bytes
    out.writeBytes("echo");

我实际上相信这个问题与服务器的readline有关,它没有得到null - 但是客户端发送了一个&#34; \ n&#34;!你能找到我的代码错误吗?感谢

0 个答案:

没有答案