同步阻塞通信-BIO编程

Posted Lance_xu_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了同步阻塞通信-BIO编程相关的知识,希望对你有一定的参考价值。

  1. 同步阻塞通信模型是传统一请求一应答,即开启一个ServerSocket负责监听socket的连接。
  2. 服务器实现
package com.haidong.bio;

import java.net.ServerSocket;
import java.net.Socket;

/**
 * TimeServer
 *
 * @author Lance
 * @date 2017/05/31
 */
public class TimeServer 
    public static void main(String[] args) throws Exception 
        int port = 8080;
        if(args.length > 0 && args != null)
            try 
                port = Integer.valueOf(port);
             catch (NumberFormatException e) 
                //采用默认值
                e.printStackTrace();
            
        
        ServerSocket server = null;
        try 
            server = new ServerSocket(port);
            System.out.println("时间服务器开启:" + port);
            Socket socket = null;
            while (true)
                //监听
                socket = server.accept();
                new Thread(new TimeServerHandler(socket)).start();
            
         finally 
            if(server != null)
                System.out.println("关闭服务器");
                server.close();
                server = null;
            
        
    

3.业务处理线程

package com.haidong.bio;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Date;

/**
 * TimeServerHandler
 *
 * @author Lance
 * @date 2017/05/31
 */
public class TimeServerHandler implements Runnable 
    private Socket socket;

    public TimeServerHandler(Socket socket) 
        this.socket = socket;
    

    public void run() 
        BufferedReader in = null;
        PrintWriter out = null;
        try 
            in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            out = new PrintWriter(this.socket.getOutputStream(), true);
            String currentTime = null;
            String body = null;
            while (true)
                //读取
                body = in.readLine();
                if (body == null)
                    break;
                
                System.out.println("服务器接收到客户端内容:" + body);
                currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER";
                out.println(currentTime);
            
         catch (IOException e) 
            e.printStackTrace();
            if(in != null)
                try 
                    in.close();
                 catch (IOException e1) 
                    e1.printStackTrace();
                
                in = null;
            
            if(out != null)
                out.close();
                out = null;
            
            if(this.socket != null)
                try 
                    socket.close();
                 catch (IOException e1) 
                    e1.printStackTrace();
                
                socket = null;
            
        
    

4.客户端实现

package com.haidong.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * TimeClient
 *
 * @author Lance
 * @date 2017/05/31
 */
public class TimeClient 
    public static void main(String[] args) 
        int port = 8080;
        if(args.length > 0 && args != null)
            try 
                port = Integer.valueOf(port);
             finally 
                //默认值
            
        
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;
        try 
            socket = new Socket("127.0.0.1", port);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            out.println("QUERY TIME ORDER");
            System.out.println("查询时间success");
            String resp = in.readLine();
            System.out.println("当前时间" + resp);
         catch (IOException e) 
            e.printStackTrace();
         finally 
          if(out != null)
              out.close();
              out = null;
          
          if(in != null)
              try 
                  in.close();
               catch (IOException e) 
                  e.printStackTrace();
              
              in = null;
          
          if(socket != null)
              try 
                  socket.close();
               catch (IOException e) 
                  e.printStackTrace();
              
              socket = null;
          
        
    

5.运行服务端和客户端结果

以上是关于同步阻塞通信-BIO编程的主要内容,如果未能解决你的问题,请参考以下文章

Java IO编程全解——传统的BIO编程

BIO,NIO区别

java IO总结(BIONIOAIO)

高级Java工程师必备 ----- 深入分析 Java IO BIO

java之BIO简介

Java 网络IO编程总结