继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

readLine方法需要\n或\r读取内容

car
关注TA
已关注
手记 83
粉丝 56
获赞 363
import java.net.*;
import java.io.*;

public class GreetingServer {

    public static void main(String[] args) throws IOException {
        int port = 6066;
        ServerSocket serverSocket = new ServerSocket(port);
        Socket socket = serverSocket.accept();
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        OutputStream os = socket.getOutputStream();
        String info;
        while ((info = br.readLine()) != null) {
            System.out.println("客户端说:" + info);
            info=info+"\n";
            os.write(info.getBytes("utf-8"));

        }
        PrintWriter pw = new PrintWriter(os);
        pw.write("欢迎您\n");
        pw.flush();
        pw.close();
        isr.close();
        is.close();
        br.close();
        socket.close();
        serverSocket.close();
    }
}
import java.net.*;
import java.io.*;

public class GreetingClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost", 6066);
        OutputStream os = socket.getOutputStream();
        PrintWriter pw = new PrintWriter(os);
        pw.write("用户名:admin;密码123\n");
        pw.flush();
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        String info;
        while ((info = br.readLine()) != null) {
            System.out.println("服务端说:" + info);
        }

        pw.close();
        br.close();
        isr.close();
        is.close();
        socket.close();

    }
}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP