您的位置:首页 > 理论基础 > 计算机网络

几种java网络编程服务器端的代码

2013-10-07 00:00 369 查看
摘要: 所有代码来自于Java网络编程精解,我是怕丢了,在这上面留个备用,详细的可以看看书

1.最开始的用io实现的

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();
}
}

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();}
}
}
}

3用通道了

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类同上

4.以上都是同步通信方式,接下来这个是异步通信方式了,书上还有个同步和异步一起用的,在这不例出来了。

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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java