package com.tcf.io.model.server.bio;
import java.net.ServerSocket;
import java.net.Socket;
/***
* TODO TCF 阻塞IO-服务器
* @author Hasee
*
*/
public class ServerBIO {
private static byte[] byteArray=new byte[1024];
@SuppressWarnings("resource")
public static void main(String[] args)
{
try
{
//TODO TCF 打开Socket服务
ServerSocket serverSocket=new ServerSocket(8080);
//TODO TCF 获取客户端发送的消息
System.out.println("开始接收客户端发送的消息...");
Socket socket=serverSocket.accept();
//TODO TCF 读取客户端发送的消息
int readResult=socket.getInputStream().read(byteArray);
if(readResult>0)
{
System.out.println("服务器接收到的消息:"+new String(byteArray));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
package com.tcf.io.model.server.bio;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/***
* TODO TCF 伪异步阻塞IO模型(BIO)-服务器
* TODO TCF 基于多线程实现批量IO操作,一个客户端只能建立一次TCP
* @author Hasee
*
*/
public class ServerBIODisguiser {
//TODO TCF 读取字节数
private static byte[] byteArray=new byte[1024];
//TODO TCF 线程池
private static ExecutorService executorService=Executors.newCachedThreadPool();
@SuppressWarnings("resource")
public static void main(String[] args)
{
try
{
//TODO TCF 建立连接
final ServerSocket serverSocket=new ServerSocket(8080);
System.out.println("开始接收客户端发送的消息...");
while(true)
{
//TODO TCF 接收客户端发送的消息
final Socket socket=serverSocket.accept();
executorService.execute(new Runnable() {
@Override
public void run()
{
try
{
int readResult=socket.getInputStream().read(byteArray);
if(readResult>0)
{
System.out.println("接收到客户端发送的消息:"+new String(byteArray));
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
package com.tcf.io.model.server.nio;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
/****
* TODO TCF 非阻塞IO模型-服务器
* TODO TCF 单线程进行批量IO操作,不阻塞
* @author Hasee
*
*/
public class ServerNIO {
//TODO TCF 输入流装载的字节缓冲区ByteBuffer
private static ByteBuffer byteBuffer=ByteBuffer.allocate(512);
public static void main(String[] args)
{
try
{
ServerSocketChannel serverSocketChannel=ServerSocketChannel.open();
//TODO TCF 绑定服务器端口号
serverSocketChannel.bind(new InetSocketAddress(8080));
//TODO TCF 设置为非阻塞IO模型NIO
serverSocketChannel.configureBlocking(false);
//TODO TCF 接收客户端发送过来的消息
System.out.println("开始接收客户端发送的消息...");
SocketChannel socketChannel=serverSocketChannel.accept();
if(socketChannel!=null)
{
//TODO TCF 接收到客户端发送的消息,直接输出
int readResult=socketChannel.read(byteBuffer);
if(readResult>0)
{
//TODO TCF 字节缓冲区转字节
byte[] byteArray=byteBuffer.array();
if(byteArray!=null && byteArray.length>0)
{
System.out.println("接收到客户端发送过来的消息:"+new String(byteArray));
}
}
}
else
{
//TODO TCF 没有接收到客户端发送的消息,直接输出未接收
System.out.println("未接收到客户端发送过来的消息");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
package com.tcf.io.model.server.nio;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;
/***
* TODO TCF 基于NIO三要素:选择器Selector,自定义集合作为选择器,实现伪非阻塞IO模型
* @author Hasee
*
*/
public class ServerNIODisguiser {
//TODO TCF 输入流存放的字节缓冲区ByteBuffer
private static ByteBuffer byteBuffer=ByteBuffer.allocate(512);
//TODO TCF 选择器Selector
private static List
public static void main(String[] args)
{
try
{
//TODO TCF 创建NIO非阻塞IO服务
ServerSocketChannel serverSocketChannel=ServerSocketChannel.open();
//TODO TCF 绑定运行端口
serverSocketChannel.bind(new InetSocketAddress(8080));
//TODO TCF 指定处理IO方式为非阻塞IO(NIO)
serverSocketChannel.configureBlocking(false);
//TODO TCF 获取接收到的客户端发送的消息
System.out.println("开始接收客户端发送的消息...");
while(true)
{
SocketChannel socketChannel=serverSocketChannel.accept();
if(socketChannel!=null)
{
//TODO TCF 接收到客户端发送的消息,存入选择器
// socketChannel.configureBlocking(false);
selector.add(socketChannel);
//TODO TCF 遍历选择器并输出接收到的消息
for(SocketChannel channel:selector)
{
int readResult=channel.read(byteBuffer);
if(readResult>0)
{
byteBuffer.flip();
//TODO TCF 字节缓冲区转字节
byte[] byteArray=byteBuffer.array();
if(byteArray!=null && byteArray.length>0)
{
System.out.println("接收到客户端发送的消息:"+new String(byteArray));
}
}
}
}
//TODO TCF 没有接收到消息则直接跳过,因为是非阻塞IO处理,不论是否接收到客户端发送的消息都不影响程序执行...
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
package com.tcf.io.model.server.nio;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
/***
* TODO TCF 基于JDK原生API实现非阻塞IO-NIO
* @author Hasee
*
*/
public class ServerNIOPro {
//TODO TCF 选择器Selector,存放客户端发送的处理IO请求
private Selector selector;
//TODO TCF 初始化非阻塞IO模型服务器
public void init(Integer port)
{
try
{
ServerSocketChannel serverSocketChannel=ServerSocketChannel.open();
//TODO TCF 绑定运行端口
serverSocketChannel.bind(new InetSocketAddress(port.intValue()));
//TODO TCF 设置处理IO的方式为非阻塞IO-NIO
serverSocketChannel.configureBlocking(false);
//TODO TCF 非阻塞IO选择器初始化
this.selector=Selector.open();
//TODO TCF 当前IO处理通道存入选择器
serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//TODO TCF 监听器,监听选择器中的每一个IO处理请求
public void listen()
{
System.out.println("开始接收客户端发送的消息...");
try
{
while(true)
{
//TODO TCF 选择器中通道元素数量
int result=this.selector.select();
if(result>0)
{
//TODO TCF 获取选择器中的通道
Iterator
while(iterator.hasNext())
{
SelectionKey selectionKey=iterator.next();
//TODO TCF 删除当前已经处理的通道
iterator.remove();
//TODO TCF 如果通道事件类型是Accept则需要获取客户端发送的通道并重新加入选择器,返回ServerSocketChannel
//TODO TCF 如果通道事件类型是Read则直接获取通道数据输出,返回SocketChannel
if(selectionKey.isAcceptable())
{
//TODO TCF 接收到客户端发送的请求事件(accept),获取客户端发送的消息通道(SocketChannel),重新存入选择器
ServerSocketChannel serverSocketChannel=(ServerSocketChannel) selectionKey.channel();
//TODO TCF 获取接收到客户端发送的消息
SocketChannel socketChannel=serverSocketChannel.accept();
if(socketChannel!=null)
{
//TODO TCF 设置为非阻塞IO处理模式并重新加入选择器
socketChannel.configureBlocking(false);
//TODO TCF 通道设置只读权限
socketChannel.register(selector,SelectionKey.OP_READ);
}
}
else if (selectionKey.isReadable())
{
//TODO TCF 如果通道是只读权限,直接获取通道数据并输出
SocketChannel socketChannel=(SocketChannel)selectionKey.channel();
//TODO TCF 获取接收到的消息,存放字节缓冲区
ByteBuffer byteBuffer=ByteBuffer.allocate(512);
int readResult=socketChannel.read(byteBuffer);
if(readResult>0)
{
byteBuffer.flip();
//TODO TCF 字节缓冲区中的字节
byte[] byteArray=byteBuffer.array();
if(byteArray!=null && byteArray.length>0)
{
System.out.println("接收到客户端发送的消息:"+new String(byteArray));
//TODO TCF 响应客户端信息
ByteBuffer responseByteBuffer=ByteBuffer.wrap(byteArray);
socketChannel.write(responseByteBuffer);
}
}
}
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ServerNIOPro serverNIOPro=new ServerNIOPro();
serverNIOPro.init(8080);
serverNIOPro.listen();
}
}
package com.tcf.io.model.client;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Scanner;
import org.apache.commons.lang.StringUtils;
/***
* TODO TCF 基于Socket通信-TCP协议客户端
* @author Hasee
*
*/
public class SocketTCPClient1 {
//TODO TCF 输入流存放的字节数组
private static byte[] byteArray=new byte[1024];
public static void main(String[] args)
{
Socket socket=new Socket();
SocketAddress address=new InetSocketAddress(8080);
try
{
//TODO TCF 建立连接
socket.connect(address);
//TODO TCF 发送消息
while(true)
{
Scanner input=new Scanner(System.in);
String message=input.next();
if(StringUtils.isNotEmpty(message))
{
socket.getOutputStream().write(message.getBytes());
System.out.println("消息发送成功...");
//TODO TCF 接收到服务器返回的响应数据
int responseResult=socket.getInputStream().read(byteArray);
if(responseResult>0)
{
System.out.println("接收到返回的响应数据:"+new String(byteArray));
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}