`
ninghq
  • 浏览: 12700 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

简单BioDemo示例(socket通信)

    博客分类:
  • java
阅读更多

思路:服务端接到请求,分发给多线程任务,返回时间给客户端

 

Bioserver服务端

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

public class BioServer {

    /**
     * 端口号
     */
    private static final int PORT = 8080;

    public static void main(String[] args) throws IOException {

        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();
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(server!=null){
                System.out.println("the time server close");
                server.close();
            }
        }
    }
}

 

多线程时间任务

import java.io.*;
import java.net.Socket;
import java.util.Date;

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 body = null;

            while ((body = in.readLine())!=null && body.length()!=0){
                System.out.println("the time server receive msg :" + body);
                out.println(new Date().toString());
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {

            if(in!=null){
                try {
                    in.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(this.socket != null){
                try{
                    this.socket.close();

                }catch (Exception e){
                    e.printStackTrace();
                }
            }

        }
    }
}

 

BioCenlit客户端

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

public class BioClient {

    private static final int PORT = 8080;
    private static final String HOST = "127.0.0.1";

    public static void main(String[] args){
        Socket socket = null;
        BufferedReader in = null;
        PrintWriter out = null;

        try {
            socket = new Socket(HOST, PORT);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            out.println("I am Client");
            String resp = in.readLine();
            System.out.println("当前服务器时间是:"+resp);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(in != null){
                try {
                    in.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

 

分享到:
评论

相关推荐

    基于java NIO的socket通信demo

    在这个“基于java NIO的socket通信demo”中,我们将探讨如何使用NIO进行服务器和客户端的Socket通信,并解决通信过程中的字符集乱码问题。 首先,我们来看`NioServer.java`。这个文件中包含了一个基于NIO的服务器端...

    spring-boot示例项目

    spring boot demo 是一个Spring Boot、Spring Cloud的项目示例,根据市场主流的后端技术,共集成了30+个demo,未来将持续更新。该项目包含helloworld(快速入门)、web(ssh项目快速搭建)、aop(切面编程)、data-redis...

    code4netty:BIO、PIO(Pseudo-Asynchronous)、NIO示例,以及基于Netty的实现各种功能的测试demo。基于Netty4.1.22

    - **BIO示例**:展示了如何使用传统的Socket API进行网络通信。 - **PIO模拟**:可能通过自定义线程池和轮询机制来实现类似效果。 - **NIO示例**:演示了如何使用Java NIO进行非阻塞I/O操作。 - **Netty基础功能**:...

    Mina2.0自学手册

    这本手册通过提供三个具体的示例(demo),让学习者在实践中掌握mina的应用。 mina框架在处理网络通信方面具有以下几个特点: 1. 它提供了一种简便的方法来处理事件驱动的协议,例如HTTP或FTP,它封装了底层的...

    Java springboot 整合mina 框架,nio通讯基础教程,mina框架基础教程.zip

    最后,“MinaDemo”可能是Mina框架的一个示例项目,你可以通过运行和分析这个项目,进一步掌握Mina的实战应用。 总结来说,本教程将引导你从理论到实践,掌握Java NIO的基本原理,理解Mina框架的使用,以及如何在...

    nio-demo:NIO编辑演示

    在"nio-demo"项目中,可能包含了使用NIO进行Socket编程的示例。NIO的`ServerSocketChannel`和`SocketChannel`可以替代传统的`ServerSocket`和`Socket`。通过`ServerSocketChannel.bind()`绑定监听地址和端口,`...

Global site tag (gtag.js) - Google Analytics