1.最開始的用io實現的 java
import java.io.*;
import java.net.*;
public class EchoServer{
private int port = 8000;
private ServerSocket serverSocket;
public EchoServer() throws IOException{
serverSocket = new ServerSocket(port);
System.out.println("服務器啓動");
}
public String echo(String msg){
return "echo:" + msg;
}
private PrintWriter getWriter(Socket socket)throws IOException{
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket)throws IOException{
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public void service(){
while(true){
Socket socket = null;
try{
socket = serverSocket.accept();
System.out.println("New connection accepted" + socket.getInetAddress() +":" + socket.getPort());
BufferedReader br = getReader(socket);
PrintWriter pw = getWriter(socket);
String msg = null;
while((msg = br.readLine())!= null){
System.out.println(msg);
pw.println(echo(msg));
if(msg.equals("bye")){
break;
}
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(socket != null)
socket.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws IOException{
new EchoServer().service();
}
} 服務器
2.服務端用線程池,書上還有個本身實現線程池的,能夠看看 異步
mport java.io.*;
import java.net.*;
import java.util.concurrent.*;
public class EchoServer3{
private int port = 8000;
private ServerSocket serverSocket;
private ExecutorService executorService;
private final int POOL_SIZE = 4;
public EchoServer3() throws IOException{
serverSocket = new ServerSocket(port);
//建立線程池
executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_SIZE);
System.out.println("服務器啓動");
}
public void service(){
while(true){
Socket socket = null;
try{
socket = serverSocket.accept();
executorService.execute(new Handler(socket));//把與客戶通訊的任務交給線程池
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void main(String[] args)throws Exception{
new EchoServer3().service();
}
} socket
class Handler implements Runnable{
private SocketChannel socketChannel;
public Handler(SocketChannel socketChannel){
this.socketChannel = socketChannel;
}
public void run(){
handle(socketChannel);
}
private PrintWriter getWriter(Socket socket) throws IOException{
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut,true);
}
private BufferedReader getReader(Socket socket)throws IOException{
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
public String echo(String msg){
return "echo:" + msg;
}
public void handle(SocketChannel socketChannel){
try{
Socket socket = socketChannel.socket();
System.out.println("接收到客戶鏈接,來自:" + socket.getInetAddress() + ":" + socket.getPort());
BufferedReader br = getReader(socket);
PrintWriter pw = getWriter(socket);
String msg = null;
while((msg = br.readLine()) != null){
System.out.println(msg);
pw.println(echo(msg));
if(msg.equals("bye"))
break;
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(socketChannel != null)
socketChannel.close();
}catch(IOException e){e.printStackTrace();}
}
}
} this
3用通道了 .net
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
public class EchoServer{
private int port = 8000;
private ServerSocketChannel serverSocketChannel = null;
private ExecutorService executorService;
private static final int POOL_MULTIPLE = 4;
public EchoServer() throws IOException{
executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()*POOL_MULTIPLE);
//建立一個ServerSocketChannel對象
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().setReuseAddress(true);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
System.out.println("服務器啓動");
}
public void service(){
while(true){
SocketChannel socketChannel = null;
try{
socketChannel = serverSocketChannel.accept();
executorService.execute(new Handler(socketChannel));
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void main(String[] args)throws Exception{
new EchoServer().service();
}
} 線程
Handler類同上 code
4.以上都是同步通訊方式,接下來這個是異步通訊方式了,書上還有個同步和異步一塊兒用的,在這不例出來了。 server
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.*;
public class EchoServer3{
private Selector selector;
private int port = 8000;
private ServerSocketChannel serverSocketChannel = null;
private Charset charset = Charset.forName("GBK");
public EchoServer3() throws IOException{
selector = Selector.open();
//executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * POOL_MULTIPLE);
//建立一個ServerSocketChannel對象
serverSocketChannel = ServerSocketChannel.open();
//使得在同一個主機上關閉了服務器程序,緊接着再啓動該服務器程序時,能夠順利綁定相同的端口
serverSocketChannel.socket().setReuseAddress(true);
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
System.out.println("服務器啓動");
}
public void service()throws IOException{
serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);
while(selector.select() > 0){
Set readyKeys = selector.selectedKeys();
Iterator it = readyKeys.iterator();
while(it.hasNext()){
SelectionKey key = null;
try{
key = (SelectionKey)it.next();
it.remove();
if(key.isAcceptable()){
ServerSocketChannel ssc = (ServerSocketChannel)key.channel();
SocketChannel socketChannel = (SocketChannel)ssc.accept();
System.out.println("接收到客戶鏈接,來自:"+ socketChannel.socket().getInetAddress() + ":"
+ socketChannel.socket().getPort());
socketChannel.configureBlocking(false);
ByteBuffer buffer = ByteBuffer.allocate(1024);
socketChannel.register(selector,SelectionKey.OP_READ|SelectionKey.OP_WRITE,buffer);
}
if(key.isReadable()){
receive(key);
}
if(key.isWritable()){
send(key);
}
}catch(IOException e){
e.printStackTrace();
try{
if(key != null){
key.cancel();
key.channel().close();
}
}catch(Exception ex){e.printStackTrace();}
}
}
}
}
public void send(SelectionKey key)throws IOException{
ByteBuffer buffer = (ByteBuffer)key.attachment();
SocketChannel socketChannel = (SocketChannel)key.channel();
buffer.flip();
String data = decode(buffer);
if(data.indexOf("\r\n") == -1) return ;
String outputData = data.substring(0,data.indexOf("\n")+1);
System.out.print(outputData);
//ByteBuffer outputBuffer = data;
ByteBuffer outputReader = encode("ehco:" + outputData);
while(outputReader.hasRemaining())
socketChannel.write(outputReader);
ByteBuffer temp = encode(outputData);
buffer.position(temp.limit());
buffer.compact();
if(outputData.equals("bye\r\n")){
key.cancel();
socketChannel.close();
System.out.println("關閉與客戶的鏈接");
}
}
public void receive(SelectionKey key) throws IOException{
//得到與SelectionKey關聯的附件
ByteBuffer buffer =(ByteBuffer)key.attachment();
//得到與SelectionKey關聯的SocketChannel
SocketChannel socketChannel = (SocketChannel)key.channel();
//建立一個ByteBuffer,用於存放讀到的數據
ByteBuffer readBuff = ByteBuffer.allocate(32);
socketChannel.read(readBuff);
readBuff.flip();
buffer.limit(buffer.capacity());
buffer.put(readBuff);
}
public String decode(ByteBuffer buffer){
CharBuffer charBuffer = charset.decode(buffer);
return charBuffer.toString();
}
public ByteBuffer encode(String str){
return charset.encode(str);
}
public static void main(String[] args) throws Exception{
new EchoServer3().service();
}
} 對象