代码实战
ServerBoot启动类
package com.example.demo.server;
/**
* @author MySelf
* @create 2018/9/29
* @desc 服务启动类
**/
public class ServerBoot {
private static final int PORT = 8000;
/**
* 启动Server类线程
* @param args
*/
public static void main(String[] args) {
Server server = new Server(PORT);
server.start();
}
}
Server监听端口服务类
package com.example.demo.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author MySelf
* @create 2018/9/29
* @desc Server监听端口主服务
**/
public class Server {
private ServerSocket serverSocket;
/**
* 监听端口
* @param port
*/
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(new Runnable() {
@Override
public void run() {
doStart();
}
}).start();
}
/**
* 启动客户端接受
*/
private void doStart() {
while (true){
try {
Socket client = serverSocket.accept();
new ClientHandler(client).start();
}catch (IOException e){
System.out.println("服务端异常");
}
}
}
}
ClientHandler客户端接入监听类
package com.example.demo.server;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
/**
* @author MySelf
* @create 2018/9/29
* @desc 客户端接入监听类
**/
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(new Runnable() {
@Override
public void run() {
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();
}
}
}
Client客户端启动类
package com.example.demo.client;
import java.io.IOException;
import java.net.Socket;
/**
* @author MySelf
* @create 2018/9/29
* @desc 客户端启动类
**/
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(new Runnable() {
@Override
public void run() {
System.out.println("客户端启动成功!");
while (true){
try {
String message = "hello world";
System.out.println("客户端发送数据:"+message);
socket.getOutputStream().write(message.getBytes());
}catch (Exception e){
System.out.println("写数据出错!");
}
sleep();
}
}
}).start();
}
private static void sleep() {
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
相关实战
GitHub项目:基于Netty4与SpringBoot,聊天室WebSocket(文字图片)、Iot物联网-TCP/IP协议单片机通信,异步存储聊天数据
如果本文对你有所帮助,欢迎关注技术公众号。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。