服務端的EIT造形+Socket代碼
[ Go Back ]
===============================
By 高煥堂 (寫代碼)
本文的代码摘自网页:
http://bbs.51cto.com/thread-1085282-1.html
本文将之改为EIT造形架构。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
importjava.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* 使用Socket通信传输文件
*
*
*/
public class Server_E {
private final static Logger logger =Logger.getLogger(ServerTest7.class.getName());
public void init() {
Selector selector = null;
ServerSocketChannel serverSocketChannel =null;
try {
//使用Selector
selector = Selector.open();
//建立Channel
serverSocketChannel =ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
//绑定到10000端口
serverSocketChannel.socket().setReuseAddress(true);
serverSocketChannel.socket().bind(newInetSocketAddress(10000));
//向Selector注册Channel及我们有兴趣的事件
serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);
//不断的轮询
while (selector.select() > 0) {
Iterator
//轮询
while (it.hasNext()) {
SelectionKey readyKey = it.next();
it.remove();
//执行事件
doit((ServerSocketChannel) readyKey.channel());
}
}
} catch (ClosedChannelException ex) { logger.log(Level.SEVERE, null, ex);
} catch (IOException ex) { logger.log(Level.SEVERE, null, ex);
} finally {
try { selector.close();
} catch(Exception ex) {}
try { serverSocketChannel.close();
} catch(Exception ex) {}
}
}
public abstract doit(ServerSocketChannelserverSocketChannel);
}
//-------------------------------------------------------------------------------------
public class Server_T extends Servet_E{
public void doit(final ServerSocketChannel serverSocketChannel)throws IOException {
SocketChannel socketChannel = null;
try {
socketChannel =serverSocketChannel.accept();
//接收文件
receiveFile(socketChannel, newFile ("E:/socketserverdemo/src/iisant/jframe/socket/server/test7/server_receive.txt"));
//发送文件
sendFile(socketChannel, newFile("E:/socketserverdemo/src/iisant/jframe/socket/server/test7/test_server.txt"));
} finally {
try { socketChannel.close();
} catch(Exception ex) {}
}
}
//--------------------------------------------
/**
* 接收文件
* @param socketChannel
* @param file
*
*/
private void receiveFile(SocketChannel socketChannel, File file) throws IOException {
FileOutputStream fos = null;
FileChannel channel = null;
try {
fos = new FileOutputStream(file);
channel = fos.getChannel();
ByteBuffer buffer =ByteBuffer.allocateDirect(1024);
int size = 0;
while ((size = socketChannel.read(buffer))!= -1) {
buffer.flip();
if (size > 0) { buffer.limit(size);
channel.write(buffer);
buffer.clear();
}
}
} finally {
try { channel.close();
} catch(Exception ex) {}
try { fos.close();
} catch(Exception ex) {}
}
}
//-------------------------------------
/**
* 发送文件
* @param socketChannel
* @param file
*
*/
private void sendFile(SocketChannelsocketChannel, File file) throws IOException {
FileInputStream fis = null;
FileChannel channel = null;
try {
fis = new FileInputStream(file);
channel = fis.getChannel();
ByteBuffer buffer =ByteBuffer.allocateDirect(1024);
int size = 0;
while ((size = channel.read(buffer)) != -1){
buffer.rewind();
buffer.limit(size);
socketChannel.write(buffer);
buffer.clear();
}
socketChannel.socket().shutdownOutput();
} finally {
try { channel.close();
} catch(Exception ex) {}
try { fis.close();
} catch(Exception ex) {}
}
}
}
//--------------- End ------------------------------