java socket实战文档格式.docx
- 文档编号:21329134
- 上传时间:2023-01-29
- 格式:DOCX
- 页数:26
- 大小:19.27KB
java socket实战文档格式.docx
《java socket实战文档格式.docx》由会员分享,可在线阅读,更多相关《java socket实战文档格式.docx(26页珍藏版)》请在冰豆网上搜索。
publicUser(){
}
publicUser(Stringname,Stringpassword){
this.name=name;
this.password=password;
publicStringgetName(){
returnname;
publicvoidsetName(Stringname){
publicStringgetPassword(){
returnpassword;
publicvoidsetPassword(Stringpassword){
}
MyUserService.java
[java]
importjava.util.List;
publicinterfaceMyUserService{
List<
User>
list(intsize);
UserfindByName(Stringname);
voidtest();
MyUserServiceImpl.java
importjava.util.ArrayList;
publicclassMyUserServiceImplimplementsMyUserService{
@Override
publicList<
list(intsize){
users=newArrayList<
();
for(inti=0;
i<
size;
i++){
users.add(newUser("
user_"
+i,"
password_"
+i));
returnusers;
publicUserfindByName(Stringname){
returnnewUser(name,null);
publicvoidtest(){
//donothing
2.服务器端类,主要有三个类MyServer,MyServerSimpleImpl和MyServerNIOImpl。
MyServer是服务器端接口类,用来启动Socketserver;
MyServerSimpleImpl和MyServerNIOImpl是两个实现类,其中MyServerSimpleImpl是使用简单的Socket实现的,MyServerNIOImpl是使用javanio包里的类实现的,这个实现会有更好的性能。
MyServer.java
publicinterfaceMyServer{
publicvoidstartup()throwsException;
publicvoidshutdown()throwsException;
MyServerSimpleImpl.java
importcom.googlecode.garbagecan.test.socket.IOUtil;
importjava.io.ObjectInputStream;
importjava.io.ObjectOutputStream;
importjava.lang.reflect.Method;
import.ServerSocket;
import.Socket;
publicclassMyServerSimpleImplimplementsMyServer{
privateintport;
publicMyServerSimpleImpl(intport){
this.port=port;
publicvoidstartup()throwsException{
newThread(newRunnable(){
publicvoidrun(){
try{
ServerSocketserver=newServerSocket(port);
while(true){
Socketsocket=server.accept();
invoke(socket);
}catch(Exceptionex){
ex.printStackTrace();
}).start();
publicvoidshutdown()throwsException{
//Implementme
privatevoidinvoke(finalSocketsocket){
ObjectInputStreamois=null;
ObjectOutputStreamoos=null;
ois=newObjectInputStream(socket.getInputStream());
oos=newObjectOutputStream(socket.getOutputStream());
Objectobj=ois.readObject();
MyRequestrequest=(MyRequest)obj;
MyResponseresponse=execute(request);
oos.writeObject(response);
oos.flush();
}finally{
IOUtil.closeQuietly(ois);
IOUtil.closeQuietly(oos);
IOUtil.closeQuietly(socket);
privateMyResponseexecute(MyRequestrequest)throwsException{
Classclazz=request.getRequestClass();
StringmethodName=request.getRequestMethod();
Class<
?
>
[]parameterTypes=request.getRequestParameterTypes();
Methodmethod=clazz.getDeclaredMethod(methodName,parameterTypes);
Object[]parameterValues=request.getRequestParameterValues();
finalObjectobj=method.invoke(clazz.newInstance(),parameterValues);
returnnewMyGenericResponse(obj);
MyServerNIOImpl.java
importcom.googlecode.garbagecan.test.socket.SerializableUtil;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
import.InetSocketAddress;
importjava.nio.ByteBuffer;
importjava.nio.channels.SelectionKey;
importjava.nio.channels.Selector;
importjava.nio.channels.ServerSocketChannel;
importjava.nio.channels.SocketChannel;
importjava.util.Iterator;
importjava.util.logging.Level;
importjava.util.logging.Logger;
publicclassMyServerNIOImplimplementsMyServer{
privatefinalstaticLoggerlogger=Logger.getLogger(MyServerNIOImpl.class.getName());
publicMyServerNIOImpl(intport){
Selectorselector=null;
ServerSocketChannelserverSocketChannel=null;
selector=Selector.open();
serverSocketChannel=ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().setReuseAddress(true);
serverSocketChannel.socket().bind(newInetSocketAddress(port));
serverSocketChannel.register(selector,SelectionKey.OP_ACCEPT);
while(selector.select()>
0){
Iterator<
SelectionKey>
it=selector.selectedKeys().iterator();
while(it.hasNext()){
SelectionKeyreadyKey=it.next();
it.remove();
invoke((ServerSocketChannel)readyKey.channel());
}catch(Exceptionex){
logger.log(Level.SEVERE,ex.getMessage(),ex);
selector.close();
}catch(Exceptionex){}
serverSocketChannel.close();
privatevoidinvoke(ServerSocketChannelserverSocketChannel)throwsException{
SocketChannelsocketChannel=null;
socketChannel=serverSocketChannel.accept();
MyRequestmyRequest=receiveData(socketChannel);
MyResponsemyResponse=execute(myRequest);
sendData(socketChannel,myResponse);
socketChannel.close();
privateMyRequestreceiveData(SocketChannelsocketChannel)throwsIOException{
MyRequestmyRequest=null;
ByteArrayOutputStreambaos=newByteArrayOutputStream();
ByteBufferbuffer=ByteBuffer.allocate(1024);
byte[]bytes;
intsize=0;
while((size=socketChannel.read(buffer))>
=0){
buffer.flip();
bytes=newbyte[size];
buffer.get(bytes);
baos.write(bytes);
buffer.clear();
bytes=baos.toByteArray();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java socket实战 socket 实战