使用套接字立即将数据从服务器发送到客户端时出现问题?

时间:2019-06-04 18:36:04

标签: java serversocket

我正在尝试使用ServerSocket制作服务器以便在android应用程序中使用,但是我遇到一个问题,当我仅使用socket.close()时,数据会发送到客户端。我希望在写入之后,应该在关闭与客户端的连接之前发送数据。

class Server {
    //initialize socket and input stream 
    private Socket socket = null;
    private ServerSocket server = null;
    private static DataOutputStream outClient = null;
    private static Scanner scan = new Scanner(System.in);
    static boolean bool = true;

    // constructor with port 
    public Server(int port) {

        // starts server and waits for a connection 
        try {
            server = new ServerSocket(port);
            System.out.println("Server started");

            System.out.println("Waiting for a client ...");

            socket = server.accept();
            System.out.println("Client accepted");

            // takes input from the client socket 
            outClient = new DataOutputStream(socket.getOutputStream());

            work();

            System.out.println("Closing connection");

            // close connection 
            socket.close();
            in.close();
        } catch (IOException i) {
            System.out.println(i);
        }
    }

    static void work() {

        System.out.println("Enter Data: ");
        try {
            outClient.writeUTF(scan.next());
            outClient.flush();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以参考下面的SocketServer和SocketCLient代码。

服务器代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer {
    private int port;
    private ServerSocket serverSocket;
    private Socket socketConnection;

    public SocketServer(int port) {
        this.port = port;
    }

    public void startSocketConnection() {
        try {
            serverSocket = new ServerSocket(this.port);
            // Blocking call
            socketConnection = serverSocket.accept();
            System.out.println("Client accepted");

            // takes input from the console
            BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
            // takes input from the client socket
            BufferedReader incomingStream = new BufferedReader(
                    new InputStreamReader(socketConnection.getInputStream()));
            PrintStream outgoingStream = new PrintStream(socketConnection.getOutputStream());

            // Reading incoming stream
            System.out.println(incomingStream.readLine());
            System.out.println("Enter String");
            // writing user input to outgoing stream
            outgoingStream.println(userInput.readLine());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socketConnection.close();
                serverSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

客户代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

class SocketClient {
    private Socket socketConnection;
    private int port;
    private String IPAddress;

    public SocketClient(String IPAddress, int port) {
        this.IPAddress = IPAddress;
        this.port = port;
    }

    public void startClientConnection() {
        try {
            socketConnection = new Socket(this.IPAddress, this.port);

            BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
            // takes input from the server socket
            BufferedReader incomingStream = new BufferedReader(
                    new InputStreamReader(socketConnection.getInputStream()));
            PrintStream outgoingStream = new PrintStream(socketConnection.getOutputStream());

            System.out.print("Enter string");
            outgoingStream.println(userInput.readLine());
            System.out.println(incomingStream.readLine());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socketConnection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}