Java网络通信(一)——BIO

Socket&ServerSocket方式—-Blocking I/O

1对1模式(仅用于测试)

//Server端
public class LinyServer {
    public static void main(String[] args) {
        System.out.println("Server: started...");
        LinyServer svr = new LinyServer();
        try {
            svr.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Server: bye!");
    }

    public void start() throws IOException {
        ServerSocket ss = new ServerSocket(9998);
        Socket s = null;
        s = ss.accept();
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter out = new PrintWriter(s.getOutputStream());
        String line = null;
        while((line=in.readLine()) != null) {
            System.out.println("Receive: " + line);
            String res = line + "Svr";
            System.out.println("Send: " + res);
            out.println(res);
            out.flush();
        }
    }
}
// Client端
public static void main(String[] args) throws Exception {
        Socket s = new Socket("127.0.0.1", 9998);
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        PrintWriter out = new PrintWriter(s.getOutputStream(), true);

        Scanner scanner = new Scanner(System.in);
        String keyIn = "";
        while(scanner.hasNext()) {
            keyIn = scanner.next();
            out.println(keyIn);
            out.flush();
            System.out.println("Send: " + keyIn);

            String read = in.readLine();
            System.out.println("Receive: " + read);
        }
    }

1对多模式

//服务端,来一个连接开一个线程处理
//仅供演示,实际根据需求考虑用线程池
public class LinyServer {
    public static void main(String[] args) {
        System.out.println("Server: started...");
        LinyServer svr = new LinyServer();
        try {
            svr.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Server: bye!");
    }

    public void start() throws IOException {
        ServerSocket ss = new ServerSocket(9998);
        while(true) {
            Socket s = ss.accept();
            new LinyThread(s).start();
        }
    }   
}

public class LinyThread extends Thread {
    private Socket mSocket;
    public LinyThread(Socket s) {
        super();
        mSocket = s;
    }

    @Override
    public void run() {
        System.out.println("Started: " + Thread.currentThread());
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
            PrintWriter out = new PrintWriter(mSocket.getOutputStream());
            String line = null;
            while((line=in.readLine()) != null) {
                System.out.println(Thread.currentThread() + "Rceive: " + line);
                String res = line + "--> from server" + Thread.currentThread();
                System.out.println(Thread.currentThread() + "Send: " + res);
                out.println(res);
                out.flush();
            }
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

}
//客户端,模拟了2个用户(不同的Socket)跟服务端进行交互
public class LinyClient {
    public static void main(String[] args) throws Exception {
        test();
    }

    public static void test() throws Exception {
        int i = 0;
        Socket s1 = new Socket("127.0.0.1", 9998);
        BufferedReader in1 = new BufferedReader(new InputStreamReader(s1.getInputStream()));
        PrintWriter out1 = new PrintWriter(s1.getOutputStream(), true);

        Socket s2 = new Socket("127.0.0.1", 9998);
        BufferedReader in2 = new BufferedReader(new InputStreamReader(s2.getInputStream()));
        PrintWriter out2 = new PrintWriter(s2.getOutputStream(), true);

        String keyIn = "";
        BufferedReader in;
        PrintWriter out;
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()) {
            keyIn = scanner.next();
            if(i%2 == 0) {
                in = in1;
                out = out1;
            } else {
                in = in2;
                out = out2;
            }
            out.println(keyIn);
            out.flush();
            System.out.println("Send: " + keyIn);
            String read = in.readLine();
            System.out.println("Receive: " + read);
            i++;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值