gpt4 book ai didi

java - 来自客户端的字符串在 Java 中的 Client/Socket 应用程序中的 Socket 控制台上打印出来

转载 作者:行者123 更新时间:2023-12-02 05:53:28 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的客户端/服务器套接字应用程序。我遇到的问题是如何制作以及应该将其放在哪里,这部分将在客户端请求输入,然后在服务器端打印出相同的输入。我尝试过,但没有成功。

目标:在客户端类的控制台中,当用户输入例如“我的名字是迈克”时,我想要的是当时在服务器的控制台上打印字符串“我的名字是迈克”换行。

主要

public class Main {
public static void main(String[] args) {
System.out.println("The server has been summoned.\n");
System.out.println("The server is waiting for client to come...");
try {
ServerSocket servertest = new ServerSocket(2014);
while (true) {
try {
Socket ser = servertest.accept();
new Server.ThreadSer(ser).start();
} catch (IOException e) {}
}
} catch (IOException e) {System.err.println(e);}
}}}

服务器

public class Server {

public static class ThreadSer extends Thread {
private Socket s;
public ThreadSer(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
String response = "This is the IP: " + s.getLocalAddress() + " that has come via port: "
+ s.getLocalPort() + "\r\n";
OutputStream out = s.getOutputStream();
out.write(response.getBytes());

} catch (IOException e) { System.err.println(e); }
}}}

客户端

public class Client {

public static void main(String[] args) throws UnknownHostException, IOException {
Socket socket = new Socket("localhost", 2014);
new OutputThread(socket.getInputStream()).start();
}

public static class OutputThread extends Thread {
private InputStream inputstream;
public OutputThread(InputStream inputstream) {
this.inputstream = inputstream;
}
@Override
public void run() {
BufferedReader input = new BufferedReader(new InputStreamReader(inputstream));
while (true) {
try {
String line = input.readLine();
System.out.println(line);
} catch (IOException exception) {
exception.printStackTrace();
break;
}}}}}

我的代码中遗漏了一些内容。

最佳答案

在您的客户端中,您不需要从服务器读取行,您需要输出流来将字符串发送到服务器:

示例:

在客户端

     public static class OutputThread extends Thread {
private InputStream inputstream;
Scanner scanner;
String message;
public OutputThread(OutputStream outputstream) {
this.inputstream = inputstream;
}
@Override
public void run() {
ObjectOutputStream output = new ObjectOutputStream(outputstream);
scanner = new Scanner(System.in);
while (true) {
try {
System.out.print("InputMessage: ");
message = scanner.nextLine();
System.out.println(message);
output.writeObject(message); //send the string to the server
output.flush();
} catch (IOException exception) {
exception.printStackTrace();
break;
}}}}}
  • 它的作用是请求输入消息,然后传递使用输出流向服务器发送消息。

在服务器中:

    public class Server {

public static class ThreadSer extends Thread {
private Socket s;
public ThreadSer(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
String response = "This is the IP: " + s.getLocalAddress() + " that has come via port: "
+ s.getLocalPort() + "\r\n";
ObjectInputStream input = new ObjectInputStream(s.getInputStream());
while(true){
Object object = input.readObject();
String command = ((String) object).trim(); //trim the string
System.out.println(command);
}
} catch (IOException e) { System.err.println(e); }
}}}
  • 它正在做的是循环并读取客户端的行使用输入流发送给它。然后将其打印到服务器的控制台。

关于java - 来自客户端的字符串在 Java 中的 Client/Socket 应用程序中的 Socket 控制台上打印出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23330472/

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