簡體   English   中英

使用套接字立即將數據從服務器發送到客戶端時出現問題?

[英]Problem in sending data from server to Client immidiately using sockets?

我正在嘗試使用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);
        }
    }
}

您可以參考下面的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();
            }
        }
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM