public class BioClient {
public static void main(String[] args) {
OutputStream outputStream = null;
BufferedWriter bw = null;
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 9999);
outputStream = socket.getOutputStream();
PrintStream ps = new PrintStream(outputStream);
ps.println("hello , I am form china");
ps.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public class BioServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
BufferedReader br = null;
try {
serverSocket = new ServerSocket(9999);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
br = new BufferedReader(new InputStreamReader(inputStream));
String s = null;
if ((s = br.readLine()) != null){
System.out.println("服务器收到消息 ====" + s);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
服务器收到消息 ==== hello , I am form china
public class TwoBioClient {
public static void main(String[] args) {
OutputStream outputStream = null;
BufferedWriter bw = null;
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 9999);
outputStream = socket.getOutputStream();
PrintStream ps = new PrintStream(outputStream);
ps.println("hello , I am form china");
ps.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public class TwoBioServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
BufferedReader br = null;
try {
serverSocket = new ServerSocket(9999);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
br = new BufferedReader(new InputStreamReader(inputStream));
String s = null;
if ((s = br.readLine()) != null){
System.out.println("服务器收到消息 ====" + s);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
服务器收到消息 ==== hello , I am form china
服务器收到消息 ==== hello , I am form china
服务器收到消息 ==== hello , I am form china
在上述的案例中,一个服务端只能接收一个客户端的通信请求,那么如果服务端需要处理很多个客户端的消 息通信请求应该如何处理呢,此时我们就需要在服务端引入线程了,也就是说客户端每发起一个请求,服务端就创 建一个新的线程来处理这个客户端的请求,这样就实现了一个客户端一个线程的模型,
public class MultiBioClient {
public static void main(String[] args) {
OutputStream outputStream = null;
BufferedWriter bw = null;
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 9999);
outputStream = socket.getOutputStream();
PrintStream ps = new PrintStream(outputStream);
Scanner scanner = new Scanner(System.in);
while (true){
System.out.print("请说:");
String msg = scanner.nextLine();
ps.println(msg);
ps.flush();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public class MultiBioServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(9999);
while (true){
Socket socket = serverSocket.accept();
new MultiServerThreadReader(socket).start();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public class MultiServerThreadReader extends Thread{
private Socket socket;
public MultiServerThreadReader(Socket socket){
this.socket = socket;
}
@Override
public void run() {
try {
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String s = null;
while ((s = br.readLine()) != null){
System.out.println("服务器收到消息 ====" + s);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
===client1:
请说:
ppp
请说:
你在干嘛?
请说:
我是第一个client
请说:
===client2:
请说:
lll
请说:
还钱!!!
请说:
我是第二个client
请说:
===client3:
请说:
我是第三个client
请说
服务器收到消息 ==== lll
服务器收到消息 ==== ppp
服务器收到消息 ==== 你在干嘛?
服务器收到消息 ==== 还钱!!!
服务器收到消息 ==== 我是第二个client
服务器收到消息 ==== 我是第一个client
服务器收到消息 ==== 我是第三个client
public class AsyncBioClient {
public static void main(String[] args) {
OutputStream outputStream = null;
BufferedWriter bw = null;
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 9999);
outputStream = socket.getOutputStream();
PrintStream ps = new PrintStream(outputStream);
Scanner scanner = new Scanner(System.in);
while (true){
System.out.print("请说:");
String msg = scanner.nextLine();
ps.println(msg);
ps.flush();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public class AsyncBioServer {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(9999);
ExecutorService executorService = new ThreadPoolExecutor(3, 6, 10, TimeUnit.SECONDS, new LinkedBlockingDeque(2), //队列大小为2
new MyThreadFactory(),//这里用简单的线程工厂
new RejectedExecutionHandler() {
@Override
//错误策略,超过最大线程数后执行该方法
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println("ThreadName:" + Thread.currentThread().getName() + "执行拒绝策略:");
}
}
);
while (true) {
Socket socket = serverSocket.accept();
executorService.execute(new ServerRunnable(socket));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static class MyThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
return new Thread(r);
}
}
}
public class ServerRunnable implements Runnable{
private Socket socket;
public ServerRunnable(Socket socket){
this.socket = socket;
}
@Override
public void run() {
try {
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String s = null;
while ((s = br.readLine()) != null){
System.out.println("服务器收到消息 ====" + s);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
===client1:
请说:
ppp
请说:
你在干嘛?
请说:
我是第一个client
请说:
===client2:
请说:
lll
请说:
还钱!!!
请说:
我是第二个client
请说:
===client3:
请说:
我是第三个client
请说
服务器收到消息 ==== lll
服务器收到消息 ==== ppp
服务器收到消息 ==== 你在干嘛?
服务器收到消息 ==== 还钱!!!
服务器收到消息 ==== 我是第二个client
服务器收到消息 ==== 我是第一个client
服务器收到消息 ==== 我是第三个client
https://gitee.com/charlinchenlin/store-pos