我使用Socket的客户端向服务器端发送请求,那么服务器端能不能返回一个信息给客户端呢?比如返回给客户端一个字符串。
------解决方案--------------------------------------------------------
当然可以
- Java code
public class MyServer { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(8854); Socket client = server.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out = new PrintWriter(client.getOutputStream()); while(true) { String str = in.readLine(); System.out.println(str); out.println("has receive..."); out.flush(); if(str.equals("end")) break; } client.close(); }}