gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:36:30 24 4
gpt4 key购买 nike

我正在尝试使用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();
}
}
}

}

关于java - 使用套接字立即从服务器向客户端发送数据时出现问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56449529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com