netty学习-简单Socket(一)

package com.xin;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author xinGuiYuan
 * @create 2019-01-09 22:48
 * @since 1.0.0
 */
public class ServerBoot {
    private static final int PORT = 8000;

    public static void main(String[] args){
        Server server = new Server(PORT);
        server.start();
    }
}

==================================================

package com.xin;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author xinGuiYuan
 * @create 2019-01-09 22:53
 * @since 1.0.0
 */
public class Server {
    private ServerSocket serverSocket;

    public Server(int port){
        try {
            this.serverSocket = new ServerSocket(port);
            System.out.println("服务端启动成功,端口:" + port);
        } catch (IOException e) {
            System.out.println("服务端启动失败。");
        }
    }

    //创建新线程避免主线程阻塞
    public void start() {
        new Thread(() -> doStart()).start();
    }

    private void doStart() {
        while (true) {
            try {
                Socket client = serverSocket.accept();//accept阻塞的方法
                new ClientHandler(client).start();
            } catch (IOException e) {
                System.out.println("服务端异常");
            }
        }
    }
}

==================================================

package com.xin;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author xinGuiYuan
 * @create 2019-01-09 23:06
 * @since 1.0.0
 */
public class ClientHandler {

    public static final int MAX_DATA_LEN = 1024;
    private final Socket socket;
    public ClientHandler(Socket socket){
        this.socket = socket;
    }

    //避免读写阻塞
    public void start() {
        System.out.println("新客户端接入");
        new Thread(() -> {
            doStart();
        }).start();
    }

    private void doStart() {
        try {
            InputStream inputStream = socket.getInputStream();
            while (true) {
                byte[] data = new byte[MAX_DATA_LEN];
                int len;
                while ((len = inputStream.read(data)) != -1) {
                    String message = new String(data, 0, len);
                    System.out.println("客户端传来消息:" + message);
                    socket.getOutputStream().write(data);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

==================================================

package com.xin;

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

/**
 * 〈一句话功能简述〉<br>
 * 〈客户端〉
 *
 * @author xinGuiYuan
 * @create 2019-01-09 23:39
 * @since 1.0.0
 */
public class Client {
    private static final String HOST = "127.0.0.1";
    private static final int PORT = 8000;
    private static final int SLEEP_TIME = 5000;

    public static void main(String[] args) throws IOException {
        final Socket socket = new Socket(HOST, PORT);
        new Thread(()-> {
            System.out.println("客户端启动成功!");
            while (true) {
                try {
                    String message = "hello world";
                    System.out.println("客户端发送数据:" + message);
                    socket.getOutputStream().write(message.getBytes());
                } catch (IOException e) {
                    System.out.println("写数据出错");
                }
                sleep();
            }
        }).start();
    }

    private static void sleep() {
        try {
            Thread.sleep(SLEEP_TIME);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值