bio
作者:互联网
package com.luban.bio;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class BioClient {
public static void main(String[] args) {
Socket socket=null;
OutputStream outputStream=null;
try {
socket =new Socket("127.0.0.1",9999);
new Thread(new BioClientHandler(socket)).start(); //循环读
outputStream=socket.getOutputStream();
Scanner scanner=new Scanner(System.in);
System.out.print("请输入要发送的消息:");
while (true){
String s = scanner.nextLine();
if(s.trim().equals("by")){
break;
}
outputStream.write(s.getBytes());
outputStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.luban.bio;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class BioClientHandler implements Runnable {
private Socket socket;
public BioClientHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
InputStream inputStream=null;
try {
inputStream = socket.getInputStream();
int count=0;
byte[] bytes=new byte[1024];
while ((count=inputStream.read(bytes))!=-1){
System.out.println("\n收到服务器消息: "+new String(bytes,0,count,"utf-8"));
System.out.print("请输入要发送的消息:");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.luban.bio;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class BioServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket=new ServerSocket(9999);
TimeServerHandlerExecutorPool timeServerHandlerExecutorPool=new TimeServerHandlerExecutorPool(50,1000);
while (true){
Socket socket = serverSocket.accept(); //阻塞
System.out.println("客户端"+socket.getRemoteSocketAddress().toString()+"来连接了");
// new Thread(new BioServerHandler(socket)).start();
timeServerHandlerExecutorPool.execute(new BioServerHandler(socket));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.luban.bio;
import jdk.internal.util.xml.impl.Input;
import java.io.*;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class BioServerHandler implements Runnable {
//负责客户端通信
private Socket socket;
public BioServerHandler(Socket socket){
this.socket=socket;
}
@Override
public void run() {
InputStream inputStream=null;
OutputStream outputStream=null;
try {
inputStream = socket.getInputStream();
outputStream=socket.getOutputStream();
int count=0;
String content=null;
byte[] bytes=new byte[1024];
while ((count=inputStream.read(bytes))!=-1){
String line=new String(bytes,0,count,"utf-8");
System.out.println(line);
content=line.trim().equalsIgnoreCase("SJ")?new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()): "你发的啥?";
outputStream.write(content.getBytes());
outputStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.luban.bio;
import java.util.concurrent.*;
public class TimeServerHandlerExecutorPool implements Executor{
private ExecutorService executorService;
public TimeServerHandlerExecutorPool(int maxPoolSize, int queueSize) {
/**
* @param corePoolSize 核心线程数量
* @param maximumPoolSize 线程创建最大数量
* @param keepAliveTime 当创建到了线程池最大数量时 多长时间线程没有处理任务,则线程销毁
* @param unit keepAliveTime时间单位
* @param workQueue 此线程池使用什么队列
*/
this.executorService = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
maxPoolSize,120L, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(queueSize));
}
public TimeServerHandlerExecutorPool(int corePoolSize,int maxPoolSize, int queueSize) {
/**
* @param corePoolSize 核心线程数量
* @param maximumPoolSize 线程创建最大数量
* @param keepAliveTime 当创建到了线程池最大数量时 多长时间线程没有处理任务,则线程销毁
* @param unit keepAliveTime时间单位
* @param workQueue 此线程池使用什么队列
*/
this.executorService = new ThreadPoolExecutor(corePoolSize,
maxPoolSize,120L, TimeUnit.SECONDS,new LinkedBlockingDeque<>(queueSize));
}
@Override
public void execute(Runnable command) {
executorService.execute(command);
}
public static void main(String[] args) {
TimeServerHandlerExecutorPool timeServerHandlerExecutorPool=new TimeServerHandlerExecutorPool(0,20);
while (true){
timeServerHandlerExecutorPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("aaaaaa");
}
});
}
}
}
标签:bio,java,socket,new,import,null,public 来源: https://www.cnblogs.com/mfk11/p/16251991.html