📄 nonblockingserver.java__.htm
字号:
<html><head><title>src.zip - NonBlockingServer.java</title></head><body>
<script language="JavaScript">
function openwin(url)
{
window.open(url, 'ProgramSourceCode');
}
</script>
<textarea cols=105 rows=17>文件头:
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.util.*;
import java.nio.charset.*;
import java.lang.*;
public class NonBlockingServer
{
public Selector sel = null;
public ServerSocketChannel server = null;
public SocketChannel socket = null;
public int port = 4900;
String result = null;
public NonBlockingServer()
{
System.out.println("Inside default ctor");
}
public NonBlockingServer(int port)
{
System.out.println("Inside the other ctor");
port = port;
}
public void initializeOperations() throws IOException,UnknownHostException
{
System.out.println("Inside initialization");
sel = Selector.open();
server = ServerSocketChannel.open();
server.configureBlocking(false);
InetAddress ia = InetAddress.getLocalHost();
InetSocketAddress isa = new InetSocketAddress(ia,port);
server.socket().bind(isa);
}
public void start</textarea><BR>
<table width=756 border=0>
<tr><td width=1 bgcolor=#B0B0B0></td><td>
<script type="text/javascript"><!--
google_ad_client = "pub-8055710228382273";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="0099118804";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "000000";
google_color_text = "333333";
google_color_url = "666666";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
<td width=15 bgcolor=#E4E0D8></td></tr></table>
<textarea cols=105 rows=12>文件尾:
= ByteBuffer.wrap((charBuf.toString()).getBytes());
int nBytes = socket.write(buffer);
System.out.println("nBytes = "+nBytes);
result = null;
}
catch(Exception e)
{
e.printStackTrace();
}
}
public String readMessage(SelectionKey key)
{
int nBytes = 0;
socket = (SocketChannel)key.channel();
ByteBuffer buf = ByteBuffer.allocate(1024);
try
{
nBytes = socket.read(buf);
buf.flip();
Charset charset = Charset.forName("us-ascii");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(buf);
result = charBuffer.toString();
}
catch(IOException e)
{
e.printStackTrace();
}
return result;
}
public static void main(String args[])
{
NonBlockingServer nb = new NonBlockingServer();
try
{
nb.startServer();
}
catch (IOException e)
{
e.printStackTrace();
System.exit(-1);
}
}
}
</textarea><BR>
<a href=http://www.pudn.com><small><font color=#A0A0A0>程序员联合开发网</font></small></a> <a href="/downloads81\sourcecode\java\applet\game/detail312386.html"><small><font color=#A0A0A0>返回src.zip</font></small></a>
<a href="javascript:openwin('/view_src.asp?id=312386&f=NonBlockingServer.java');"><small><font color=#A00000>在线阅读整个文件</font></small></a>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -