基于java BIO实现一个时间查询的demo

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于java BIO实现一个时间查询的demo相关的知识,希望对你有一定的参考价值。


服务端核心代码:

server = new ServerSocket(port);
System.out.println("The time server is start in port:"+port);
Socket socket = null;
while (true)
socket = server.accept();
new Thread(new TimeServerHandler(socket)).start();

用ServerSocket类来监听port端口打开服务,然后接收到请求之后,就创建新的线程来处理请求,处理类的核心代码如下

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("The time server receive order: " + body);
currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)? new java.util.Date(
System.currentTimeMillis()).toString():"BAO ORDER";
out.println(currentTime);

客户端核心代码

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("Send order 2 server succeed.");
String resp = in.readLine();
System.out.println("Now is : " + resp);

客户端使用socker来创建一个socket连接,绑定端口号和ip地址。
完整代码请看

//
import com.sun.security.ntlm.Server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class TimeServer
public static void main(String[] args) throws IOException
/**
* @Author: chaojilaji
* @Description:
* @Date: 16:21 2018/7/21 * @param args laodiao
*/
int port = 8080;
if (args !=null && args.length>0)
try
port = Integer.valueOf(args[0]);
catch (NumberFormatException e)
// e.printStackTrace();



ServerSocket server = null;
try
server = new ServerSocket(port);
System.out.println("The time server is start in port:"+port);
Socket socket = null;
while (true)
socket = server.accept();
new Thread(new TimeServerHandler(socket)).start();


finally
if (server != null)
System.out.println("The time server close");
server.close();
server = null;




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

public class TimeServerHandler implements Runnable
private Socket socket;
public TimeServerHandler(Socket socket)
this.socket = socket;

@Override
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("The time server receive order: " + body);
currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)? new java.util.Date(
System.currentTimeMillis()).toString():"BAO ORDER";
out.println(currentTime);

catch (Exception e)
if (in!=null)
try
in.close();
catch (IOException el)
el.printStackTrace();


if (out!=null)
out.close();
out = null;

if(this.socket!=null)
try
this.socket.close();
catch (IOException el)
el.printStackTrace();

this.socket = null;




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

public class TimeClient
public static void main(String[] args)
int port = 8080;
if (args!=null && args.length>0)
try
port = Integer.valueOf(args[0]);
catch (NumberFormatException el)
//


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("Send order 2 server succeed.");
String resp = in.readLine();
System.out.println("Now is : " + resp);
catch (Exception e)

finally
if (out!=null)
out.close();
out = null;

if (in!=null)
try
in.close();
catch (IOException e)
e.printStackTrace();


if (socket!=null)
try
socket.close();
catch (IOException e)
e.printStackTrace();

socket = null;



先运行服务端程序,在执行客户端请求即可看到效果。


以上是关于基于java BIO实现一个时间查询的demo的主要内容,如果未能解决你的问题,请参考以下文章

Java网络编程系列之基于BIO的多人聊天室设计与实现

利用CountDownLatch实现基于netty的"BIO"

BIO,NIO,AIO详解

java的nio之:java的bio流下实现的socket服务器

自己动手,基于netty实现单机版的RPC

NO.72 TCP/IP+BIO : 基于线程池的实现方式