////////////
//读取server端发回的数据,并显示
public void Rec() throws IOException{
int count;
r_buff.clear();
count=sc.read(r_buff);
byte[] temp = new byte[r_buff.limit()];
r_buff.get(temp);
System.out.println("reply is " + count +" long, and content is: " + new String(temp));
}
public static void main(String args[]){
if(args.length < 1){//输入需有主机名或IP地址
try{
System.err.println("Enter host name: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
host = br.readLine();
}catch(IOException ioe){
System.err.println("sth. is wrong with br.readline() ");
}
}
else if(args.length == 1){
host = args[0];
}
else if(args.length > 1){
host = args[0];
port = Integer.parseInt(args[1]);
}
new AsyncClient();
}
}
在当前目录下运行:
javac AsynClient.java
后,若无编译出错,确认AsyncServer已经运行的情况下,接下来可运行:
java AsynClient hostname 或 java AsynClient hostname ×××(端口号)
并按提示进行操作即可。
总结
总的来说,用nio进行网络编程还是很有新意的,服务器端软件能在一个线程中维护与众多客户端的通信连接。笔者在本文中试图用一个典型的回射例子说明如何用nio建立最基本的C/S应用。希望大家能试着用用它。
另外,笔者在实践中也发现nio在应用中存在的一些难题,比如如何应用SocketChannel的继承类,以及如何在socketchannel之上应用SSL(Secure Socket Layer)等等,因而希望这篇文章只是抛砖引玉,引起大家对nio作进一步的讨论。
在当前目录下运行:
javac AsynServer.java
后,若无编译出错,接下来可运行:
java AsynServer 或 java AsynServer ×××(端口号)
上述服务程序在运行时,可指定其侦听端口,否则程序会取8848为默认端口。
2.客户端的简单示例:
////////////////////////
//AsyncClient.java
// by zztudou@163.com
////////////////////////
import java.nio.channels.SocketChannel;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Selector;
import java.nio.channels.SelectionKey;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
class AsyncClient{
private SocketChannel sc;
private final int MAX_LENGTH = 1024;
private ByteBuffer r_buff = ByteBuffer.allocate(MAX_LENGTH);
private ByteBuffer w_buff = ByteBuffer.allocate(MAX_LENGTH);
private static String host ;
private static int port = 8848;
public AsyncClient(){
try {
InetSocketAddress addr = new InetSocketAddress(host,port);
//生成一个socketchannel
sc = SocketChannel.open();
//连接到server
sc.connect(addr);
while(!sc.finishConnect())
;
System.out.println("connection has been established!...");
while(true){
//回射消息
String echo;
try{
System.err.println("Enter msg you'd like to send: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//输入回射消息
echo = br.readLine();
//把回射消息放入w_buff中
w_buff.clear();
w_buff.put(echo.getBytes());
w_buff.flip();
}catch(IOException ioe){
System.err.println("sth. is wrong with br.readline() ");
}
//发送消息
while(w_buff.hasRemaining())
sc.write(w_buff);
w_buff.clear();
//进入接收状态
Rec();
//间隔1秒
Thread.currentThread().sleep(1000);
}
}catch(IOException ioe){
ioe.printStackTrace();
}
catch(InterruptedException ie){
ie.printStackTrace();
}
}
////////////
// 读取server端发回的数据,并显示
public void Rec() throws IOException{
int count;
r_buff.clear();
while((count=sc.read(r_buff))>0)
;
r_buff.flip();
byte[] temp = new byte[r_buff.limit()];
System.out.println("reply is : " + new String(temp));
}
public static void main(String args[]){
if(args.length < 1){//输入需有主机名或IP地址
try{
System.err.println("Enter host name: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
host = br.readLine();
}catch(IOException ioe){
System.err.println("sth. is wrong with br.readline() ");
}
}
else if(args.length == 1){
host = args[0];
}
else if(args.length > 1){
host = args[0];
port = Integer.parseInt(args[1]);
}
new AsyncClient();
}
}
在当前目录下运行:
javac AsynClient.java
后,若无编译出错,确认AsyncServer已经运行的情况下,接下来可运行:
java AsynClient hostname 或 java AsynClient hostname ×××(端口号)
并按提示进行操作即可。
总结
总的来说,用nio进行网络编程还是很有新意的,服务器端软件能在一个线程中维护与众多客户端的通信连接。笔者在本文中试图用一个典型的回射例子说明如何用nio建立最基本的C/S应用。希望大家能试着用用它。
另外,笔者在实践中也发现nio在应用中存在的一些难题,比如如何应用SocketChannel的继承类,以及如何在socketchannel之上应用SSL(Secure Socket Layer)等等,因而希望这篇文章只是抛砖引玉,引起大家对nio作进一步的讨论。
