接受并反馈

客户端

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket=new Socket("127.0.0.1",10000);

        Scanner scanner=new Scanner(System.in);
        String str=scanner.nextLine();
        socket.getOutputStream().write(str.getBytes());
        socket.shutdownOutput();

       InputStream is=socket.getInputStream();
        InputStreamReader isr=new InputStreamReader(is);
        int b;
        while((b=isr.read())!=-1){
            System.out.print((char)b);
        }

        socket.close();

    }
}

服务端

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss=new ServerSocket(10000);
        Socket socket = ss.accept();

        InputStream is=socket.getInputStream();
        InputStreamReader isr=new InputStreamReader(is);
        int b;
        while((b=isr.read())!=-1){
            System.out.print((char)b);
        }
       Scanner scanner=new Scanner(System.in);
        String str=scanner.nextLine();
        OutputStream os = socket.getOutputStream();
        os.write(str.getBytes());

        socket.close();
        ss.close();
    }
}

你可能感兴趣的:(java,网络,开发语言)