[英]How do I listen to the client using the Java Socket?
我正在使用“ServerSocket”和“Socket”,我遇到的問題是:我使用 serverSocket 創建了一個服務器,我正在等待客戶端連接,當它連接時我會收到一些信息,這是我的問題,我如何繼續聽取客戶的意見並接受客戶的指示?
在下面的示例中,我正在創建一個服務器,當客戶端連接時,我將連接保存在“clientSocket”中。
@GET
@Path("/createServer")
public String conect() throws IOException {
serverSocket = new ServerSocket(3242);
clientSocket = serverSocket.accept();
...
...
}
從這一點開始我需要一直監聽這個clientSocket,當客戶端發送一些我需要捕獲的信息來執行一些動作時,該怎么做呢?
ServerSocket.accept()
給你一個java.net.Socket
。 文檔 在這里
從那里開始,您可以使用其輸入 stream ( Socket.getInputStream()
)讀取該套接字或寫入其 output stream (SocketOutputStream)( Socket.getOutputStream()
)
您的 sockets(客戶端和服務器)將一直存在,直到它們close
d 或垃圾收集,因此請記住,只要您需要它們,就對它們保持強烈的引用。
示例程序(簡單的回顯服務器。在客戶端中鍵入bye
退出):
服務器.java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Server {
private static volatile boolean stopped = false;
public static void main(String[] args) throws IOException, InterruptedException {
try (ServerSocket server = new ServerSocket(3001)) {
System.out.println("Server ready to accept connections on port " + server.getLocalPort());
final Socket client = server.accept();
System.out.println("Client connected using remote port " + client.getPort());
final Thread t = new Thread(() -> {
try {
try (InputStream clientIn = client.getInputStream()) {
try (OutputStream clientOut = client.getOutputStream()) {
echo(clientIn, clientOut);
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
stopped = true;
}
});
t.setDaemon(true);
t.start();
while (!stopped) {
Thread.sleep(10);
}
System.out.println("Program exit");
}
}
private static void echo(InputStream clientIn, OutputStream clientOut) throws IOException {
try (Scanner clientScan = new Scanner(clientIn, StandardCharsets.UTF_8)) {
try (OutputStreamWriter clientWriter = new OutputStreamWriter(clientOut, StandardCharsets.UTF_8)) {
while (!stopped) {
final String fromClient = clientScan.nextLine();
System.out.println("In: " + fromClient);
clientWriter.write(fromClient);
clientWriter.write(System.lineSeparator());
clientWriter.flush();
if ("bye".equalsIgnoreCase(fromClient.trim())) {
stopped = true;
}
}
System.out.println("Loop done");
}
}
}
}
客戶端.java
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private static volatile boolean stopped = false;
public static void main(String[] args) throws UnknownHostException, IOException {
System.out.println("Client launched");
try (Socket client = new Socket("localhost", 3001)) {
System.out.println("Connected on remote port " + client.getPort() + " from " + client.getLocalPort());
try (Scanner console = new Scanner(System.in)) {
try (OutputStreamWriter toServer = new OutputStreamWriter(client.getOutputStream())) {
final Thread t = new Thread(() -> printEverything(client));
t.setDaemon(true);
t.start();
while (!stopped) {
final String fromConsole = console.nextLine();
if (stopped)
break;
toServer.write(fromConsole);
toServer.write(System.lineSeparator());
toServer.flush();
}
}
}
}
System.out.println("Program exit");
}
private static void printEverything(Socket client) {
try (Scanner server = new Scanner(client.getInputStream())) {
while (!stopped) {
final String fromServer = server.nextLine();
System.out.println("Server said: " + fromServer);
if ("bye".equalsIgnoreCase(fromServer.trim())) {
stopped = true;
}
}
System.out.println("Loop done. Press enter to exit");
} catch (IOException e) {
e.printStackTrace();
stopped = true;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.