導航:首頁 > IDC知識 > javanio伺服器

javanio伺服器

發布時間:2020-11-11 18:48:51

1、java nio 伺服器架構有哪些

1,一個小的線程池負責dispatch NIO事件。
2,注冊事件,即操作selecter時,要使用一個同步鎖(即Architecture of a Highly Scalable NIO-Based Server一文中的guard對象),即對同一個selector的操作是互斥的。
3,這個小的線程池不處理邏輯業務,大小可以是Runtime.getRuntime().availableProcessors() + 1,即你系統有效CPU個數+1。這是因為我們假設有一個線程專門處理accept事件,
而其他線程處理read/write操作。

2、java nio並發訪問問題,我現在利用nio框架制伺服器的並發訪問,SelectionKey多線程

其實不要太迷信nio。這個東西如果你使用不當,效率還不如io高。
而且,使用了nio會造成和以前的代碼的不兼容。
當然,你是高手,那就例外了。

3、java nio socket伺服器如何啟動

socket是基於通訊層的消息處理的協議,既不是serlvet的http協議方式,也不是app桌面程序方式。
socket的通訊是基於協議的具體工作方式得了解協議,JAVA通過對其封裝,使用起來已經很簡單了,只要有一個client和一個server,通過往埠相互發送消息而實現。
有對JAVA的SOCKET封裝的組件,像比較有名的mina。

4、Java NIO 當終端關閉後,伺服器如何知道具體

終端斷開,伺服器需要捕獲異常,將其key刪除(key.cancel();),然後關閉伺服器端對應的socketchannel就好了。

5、java NIO編寫伺服器和客戶端可以讀寫Object?求給個例子

客戶端發送:
len+type+command+userid+":"+content+";"
例: 32 001 001 10001 : helloworld!;

其中32為len為整數 001為type為short,001為command為short,
10001為userid為八位元組long, content為變長字元串。「;」和":"分別一位元組。

type: 判斷type可知道客戶端的請求類型,如是聊天還是游戲
command: 判斷command類型可知道客戶端的請求具體命令,如聊天,表情等

所以,整個數據包頭長度為: 4+2+2+8+1+content長+1
content的長度可以算出來;
byte[] stringBytes=content.getBytes();
len= StringBytes.length();

數據包的頭部長度為: int headLen=4+2+2+8+1;
數據包中數據體的長度為: int datalen= len+1;
整個數據包的長度為: headLen+datalen;

客戶端發送時壓成 :
byte[] toSend=new byte[headLen+datalen];
分別用System.arrayCopy命令將內容考入soSend中
copy datalen 到toSend的前兩個位元組
copy type 到toSend 占兩個位元組
copy command
copy userid
copy ":"
copy content.getBytes()
copy ";"
//行了, tosend中己經有壓縮編碼好的消息了
outputStream.write(toSend);//發送到服務端
outputStream.flash();//別忘了flash
////////////// 客戶端發送完畢

服務端收取
byte[] datas=new byte[len];
inputStream.read(datas);
現在datas中己包含客戶端發過來的byte[]
//////////// 下面開始解析
1。從 datas中拿兩個位元組出來得到len
2。再拿兩個位元組出來,得到 type
3。得到command
4。得到userid
5。拿一個位元組 得到:或者;,如果沒有:只有;證明客戶端就發了個空消息
6。根據len的長度拿len個位元組,得到String str=new String(指定長度拿來的byte[],指定編碼);
7。 解析str,丟棄; 細分str的具體內容
//////////// 解析結束

另外一種辦法,很偷懶,效率低一丁點:
全部用String 不用計算長度,最簡單, 各數據項之間用","分割
String toString="len,type,command,userid: targetUserid,chatContent;"
這種形式最簡單,效率會低點,但你初學可以直接用,以後再改進。
具體如下:
StringBuffer buffer =new StringBuffer();
buffer.append(len);
buffer.append(",");
buffer.append(type);
buffer.append(",");
buffer.append(command);
buffer.append(,);
buffer.append(userid);
buffer.append(":");
buffer.append(對方帳號);
buffer.append(",");
buffer.append(內容);
buffer.append(";");
String result=buffer.toString();
outputstream.write(result.getBytes("utf-8"));
flash()

///服務端收到以後,是純字元串
先按";"split得到數個消息包
再按":"得到消息體得消息頭
再按","解析具體內容

6、Java NIO與IO的區別和比較

J2SE1.4以上版本中發布了全新的I/O類庫。本文將通過一些實例來簡單介紹NIO庫提供的一些新特性:非阻塞I/O,字元轉換,緩沖以及通道。
一. 介紹NIO
NIO包(java.nio.*)引入了四個關鍵的抽象數據類型,它們共同解決傳統的I/O類中的一些問題。
1. Buffer:它是包含數據且用於讀寫的線形表結構。其中還提供了一個特殊類用於內存映射文件的I/O操作。
2. Charset:它提供Unicode字元串影射到位元組序列以及逆影射的操作。
3. Channels:包含socket,file和pipe三種管道,它實際上是雙向交流的通道。
4. Selector:它將多元非同步I/O操作集中到一個或多個線程中(它可以被看成是Unix中select()函數或Win32中WaitForSingleEvent()函數的面向對象版本)。
二. 回顧傳統
在介紹NIO之前,有必要了解傳統的I/O操作的方式。以網路應用為例,傳統方式需要監聽一個ServerSocket,接受請求的連接為其提供服務(服務通常包括了處理請求並發送響應)圖一是伺服器的生命周期圖,其中標有粗黑線條的部分表明會發生I/O阻塞。

圖一

可以分析創建伺服器的每個具體步驟。首先創建ServerSocket
ServerSocket server=new ServerSocket(10000);
然後接受新的連接請求
Socket newConnection=server.accept();
對於accept方法的調用將造成阻塞,直到ServerSocket接受到一個連接請求為止。一旦連接請求被接受,伺服器可以讀客戶socket中的請求。
InputStream in = newConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
BufferedReader buffer = new BufferedReader(reader);
Request request = new Request();
while(!request.isComplete()) {
String line = buffer.readLine();
request.addLine(line);
}
這樣的操作有兩個問題,首先BufferedReader類的readLine()方法在其緩沖區未滿時會造成線程阻塞,只有一定數據填滿了緩沖區或者客戶關閉了套接字,方法才會返回。其次,它回產生大量的垃圾,BufferedReader創建了緩沖區來從客戶套接字讀入數據,但是同樣創建了一些字元串存儲這些數據。雖然BufferedReader內部提供了StringBuffer處理這一問題,但是所有的String很快變成了垃圾需要回收。
同樣的問題在發送響應代碼中也存在
Response response = request.generateResponse();
OutputStream out = newConnection.getOutputStream();
InputStream in = response.getInputStream();
int ch;
while(-1 != (ch = in.read())) {
out.write(ch);
}
newConnection.close();
類似的,讀寫操作被阻塞而且向流中一次寫入一個字元會造成效率低下,所以應該使用緩沖區,但是一旦使用緩沖,流又會產生更多的垃圾。
傳統的解決方法
通常在Java中處理阻塞I/O要用到線程(大量的線程)。一般是實現一個線程池用來處理請求,如圖二

圖二
線程使得伺服器可以處理多個連接,但是它們也同樣引發了許多問題。每個線程擁有自己的棧空間並且佔用一些CPU時間,耗費很大,而且很多時間是浪費在阻塞的I/O操作上,沒有有效的利用CPU。
三. 新I/O
1. Buffer
傳統的I/O不斷的浪費對象資源(通常是String)。新I/O通過使用Buffer讀寫數據避免了資源浪費。Buffer對象是線性的,有序的數據集合,它根據其類別只包含唯一的數據類型。
java.nio.Buffer 類描述
java.nio.ByteBuffer 包含位元組類型。 可以從ReadableByteChannel中讀在 WritableByteChannel中寫
java.nio.MappedByteBuffer 包含位元組類型,直接在內存某一區域映射
java.nio.CharBuffer 包含字元類型,不能寫入通道
java.nio.DoubleBuffer 包含double類型,不能寫入通道
java.nio.FloatBuffer 包含float類型
java.nio.IntBuffer 包含int類型
java.nio.LongBuffer 包含long類型
java.nio.ShortBuffer 包含short類型
可以通過調用allocate(int capacity)方法或者allocateDirect(int capacity)方法分配一個Buffer。特別的,你可以創建MappedBytesBuffer通過調用FileChannel.map(int mode,long position,int size)。直接(direct)buffer在內存中分配一段連續的塊並使用本地訪問方法讀寫數據。非直接(nondirect)buffer通過使用Java中的數組訪問代碼讀寫數據。有時候必須使用非直接緩沖例如使用任何的wrap方法(如ByteBuffer.wrap(byte[]))在Java數組基礎上創建buffer。
2. 字元編碼
向ByteBuffer中存放數據涉及到兩個問題:位元組的順序和字元轉換。ByteBuffer內部通過ByteOrder類處理了位元組順序問題,但是並沒有處理字元轉換。事實上,ByteBuffer沒有提供方法讀寫String。
Java.nio.charset.Charset處理了字元轉換問題。它通過構造CharsetEncoder和CharsetDecoder將字元序列轉換成位元組和逆轉換。
3. 通道(Channel)
你可能注意到現有的java.io類中沒有一個能夠讀寫Buffer類型,所以NIO中提供了Channel類來讀寫Buffer。通道可以認為是一種連接,可以是到特定設備,程序或者是網路的連接。通道的類等級結構圖如下

圖三
圖中ReadableByteChannel和WritableByteChannel分別用於讀寫。
GatheringByteChannel可以從使用一次將多個Buffer中的數據寫入通道,相反的,ScatteringByteChannel則可以一次將數據從通道讀入多個Buffer中。你還可以設置通道使其為阻塞或非阻塞I/O操作服務。
為了使通道能夠同傳統I/O類相容,Channel類提供了靜態方法創建Stream或Reader
4. Selector
在過去的阻塞I/O中,我們一般知道什麼時候可以向stream中讀或寫,因為方法調用直到stream准備好時返回。但是使用非阻塞通道,我們需要一些方法來知道什麼時候通道准備好了。在NIO包中,設計Selector就是為了這個目的。SelectableChannel可以注冊特定的事件,而不是在事件發生時通知應用,通道跟蹤事件。然後,當應用調用Selector上的任意一個selection方法時,它查看注冊了的通道看是否有任何感興趣的事件發生。圖四是selector和兩個已注冊的通道的例子

圖四
並不是所有的通道都支持所有的操作。SelectionKey類定義了所有可能的操作位,將要用兩次。首先,當應用調用SelectableChannel.register(Selector sel,int op)方法注冊通道時,它將所需操作作為第二個參數傳遞到方法中。然後,一旦SelectionKey被選中了,SelectionKey的readyOps()方法返回所有通道支持操作的數位的和。SelectableChannel的validOps方法返回每個通道允許的操作。注冊通道不支持的操作將引發IllegalArgumentException異常。下表列出了SelectableChannel子類所支持的操作。

ServerSocketChannel OP_ACCEPT
SocketChannel OP_CONNECT, OP_READ, OP_WRITE
DatagramChannel OP_READ, OP_WRITE
Pipe.SourceChannel OP_READ
Pipe.SinkChannel OP_WRITE
四. 舉例說明
1. 簡單網頁內容下載
這個例子非常簡單,類SocketChannelReader使用SocketChannel來下載特定網頁的HTML內容。
package examples.nio;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.net.InetSocketAddress;
import java.io.IOException;
public class SocketChannelReader{

private Charset charset=Charset.forName("UTF-8");//創建UTF-8字元集
private SocketChannel channel;
public void getHTMLContent(){
try{
connect();
sendRequest();
readResponse();
}catch(IOException e){
System.err.println(e.toString());
}finally{
if(channel!=null){
try{
channel.close();
}catch(IOException e){}
}
}
}
private void connect()throws IOException{//連接到CSDN
InetSocketAddress socketAddress=
new InetSocketAddress("http://www.csdn.net",80/);
channel=SocketChannel.open(socketAddress);
//使用工廠方法open創建一個channel並將它連接到指定地址上
//相當與SocketChannel.open().connect(socketAddress);調用
}
private void sendRequest()throws IOException{
channel.write(charset.encode("GET "
+"/document"
+"\r\n\r\n"));//發送GET請求到CSDN的文檔中心
//使用channel.write方法,它需要CharByte類型的參數,使用
//Charset.encode(String)方法轉換字元串。
}
private void readResponse()throws IOException{//讀取應答
ByteBuffer buffer=ByteBuffer.allocate(1024);//創建1024位元組的緩沖
while(channel.read(buffer)!=-1){
buffer.flip();//flip方法在讀緩沖區位元組操作之前調用。
System.out.println(charset.decode(buffer));
//使用Charset.decode方法將位元組轉換為字元串
buffer.clear();//清空緩沖
}
}
public static void main(String [] args){
new SocketChannelReader().getHTMLContent();
}
2. 簡單的加法伺服器和客戶機
伺服器代碼
package examples.nio;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.net.InetSocketAddress;
import java.io.IOException;
/**
* SumServer.java
*
*
* Created: Thu Nov 06 11:41:52 2003
*
* @author starchu1981
* @version 1.0
*/
public class SumServer {
private ByteBuffer _buffer=ByteBuffer.allocate(8);
private IntBuffer _intBuffer=_buffer.asIntBuffer();
private SocketChannel _clientChannel=null;
private ServerSocketChannel _serverChannel=null;
public void start(){
try{
openChannel();
waitForConnection();
}catch(IOException e){
System.err.println(e.toString());
}
}
private void openChannel()throws IOException{
_serverChannel=ServerSocketChannel.open();
_serverChannel.socket().bind(new InetSocketAddress(10000));
System.out.println("伺服器通道已經打開");
}
private void waitForConnection()throws IOException{
while(true){
_clientChannel=_serverChannel.accept();
if(_clientChannel!=null){
System.out.println("新的連接加入");
processRequest();
_clientChannel.close();
}
}
}
private void processRequest()throws IOException{
_buffer.clear();
_clientChannel.read(_buffer);
int result=_intBuffer.get(0)+_intBuffer.get(1);
_buffer.flip();
_buffer.clear();
_intBuffer.put(0,result);
_clientChannel.write(_buffer);
}
public static void main(String [] args){
new SumServer().start();
}
} // SumServer
客戶代碼
package examples.nio;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.SocketChannel;
import java.net.InetSocketAddress;
import java.io.IOException;
/**
* SumClient.java
*
*
* Created: Thu Nov 06 11:26:06 2003
*
* @author starchu1981
* @version 1.0
*/
public class SumClient {
private ByteBuffer _buffer=ByteBuffer.allocate(8);
private IntBuffer _intBuffer;
private SocketChannel _channel;
public SumClient() {
_intBuffer=_buffer.asIntBuffer();
} // SumClient constructor

public int getSum(int first,int second){
int result=0;
try{
_channel=connect();
sendSumRequest(first,second);
result=receiveResponse();
}catch(IOException e){System.err.println(e.toString());
}finally{
if(_channel!=null){
try{
_channel.close();
}catch(IOException e){}
}
}
return result;
}
private SocketChannel connect()throws IOException{
InetSocketAddress socketAddress=
new InetSocketAddress("localhost",10000);
return SocketChannel.open(socketAddress);
}

private void sendSumRequest(int first,int second)throws IOException{
_buffer.clear();
_intBuffer.put(0,first);
_intBuffer.put(1,second);
_channel.write(_buffer);
System.out.println("發送加法請求 "+first+"+"+second);
}

private int receiveResponse()throws IOException{
_buffer.clear();
_channel.read(_buffer);
return _intBuffer.get(0);
}
public static void main(String [] args){
SumClient sumClient=new SumClient();
System.out.println("加法結果為 :"+sumClient.getSum(100,324));
}
} // SumClient
3. 非阻塞的加法伺服器
首先在openChannel方法中加入語句
_serverChannel.configureBlocking(false);//設置成為非阻塞模式
重寫WaitForConnection方法的代碼如下,使用非阻塞方式
private void waitForConnection()throws IOException{
Selector acceptSelector = SelectorProvider.provider().openSelector();
/*在伺服器套接字上注冊selector並設置為接受accept方法的通知。
這就告訴Selector,套接字想要在accept操作發生時被放在ready表
上,因此,允許多元非阻塞I/O發生。*/
SelectionKey acceptKey = ssc.register(acceptSelector,
SelectionKey.OP_ACCEPT);
int keysAdded = 0;

7、JAVA用nio寫CS程序,伺服器端接收到的信息不完整,求助!SocketChannel,Bytebuffer

建議你吧文本的長度加進去。

發送之前先發送長度,接收端根據實際長度來接收,這樣確保不會錯。

8、java nio socket 怎麼讓客戶端知道伺服器端將數據發送完畢

首先,TCP是流協議,數據沒有邊界。在整個通信過程中,連接從SYN-FIN之間發送的數據,接收方是無法判斷發送方是否發送完全的。這樣,數據包的定義就如應用層來處理了,典型的封包方式有:1,按數據長度。2,根據特殊字元。另外,TCP的傳輸與應用層的write還是不同步的,應用層的write是將應用數據發送到socket的send_q中,雖然write調用返回,但是應用數據還是本方socket的send_q中,並沒有發送到對方。同樣,即使在真正的傳輸中,本方的數據也不一定全部發送到接收方,這收到接收方當前的狀態影響,比如說應用進行是否立即讀取了數據。這里就存在滑動窗口之類的機制了。這種情況就會產生應用數據的粘包和拆包的問題。好了,先說這么多。

9、求java nio網路編程的小例子,要求客戶端一直與伺服器保持連接

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.*;
import java.io.*;

public class chatClient extends Frame {

/**
* @param args
*/
TextField tfTxT=new TextField();
TextArea taContent=new TextArea();
Socket s=null;
DataOutputStream dos=null;
DataInputStream dis=null;
private boolean bConnected =false;

public static void main(String[] args) {
new chatClient().lunachFrame();
}

private class RecvThread implements Runnable{

public void run() {
try{
while(bConnected){
String str=dis.readUTF();
taContent.setText(taContent.getText()+str+'\n');
}
}catch(IOException e){
e.printStackTrace();
}
}

}

public void lunachFrame(){
this.setLocation(400, 300);
this.setSize(300,300);
//this.setLayout(new FlowLayout());
this.add(tfTxT,"South");
this.add(taContent,"North");
pack();
tfTxT.addActionListener(new TFListener());
this.addWindowListener(new WindowClose());
this.setVisible(true);
connect();
new Thread(new RecvThread()).start();
}

public void connect(){
try {
s= new Socket("127.0.0.1",8888);
dos =new DataOutputStream(s.getOutputStream());
dis =new DataInputStream(s.getInputStream());
System.out.println("connected!");
bConnected=true;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void disconnect(){
try {
dos.close();
s.close();
} catch (Exception e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}

class WindowClose extends WindowAdapter{

@Override
public void windowClosing(WindowEvent e) {
// TODO 自動生成方法存根
System.exit(0);
disconnect();
}

}

private class TFListener implements ActionListener{

public void actionPerformed(ActionEvent e) {
String str=tfTxT.getText().trim();//trim去掉兩邊空格
//taContent.setText(str);
tfTxT.setText("");
try {
dos.writeUTF(str);
dos.flush();
//dos.close();

} catch (IOException e1) {
e1.printStackTrace();

}
}
}
}

======================================
import java.io.IOException;
import java.net.*;
import java.io.*;
import java.util.*;

public class ChatServer {
List<Client> clients=new ArrayList<Client>();
Client c=null;

public static void main(String[] args){
new ChatServer().start();
}

public void start(){
boolean started=false;
ServerSocket ss=null;
DataInputStream dis=null;
try{
ss=new ServerSocket(8888);
started =true;
}catch(Exception e)
{
e.printStackTrace();
}
try{
while(started){
Socket s=ss.accept();
c=new Client(s);//啟動線程,實行run()方法
System.out.println("a client connected!");
new Thread(c).start();//啟動start方法,循環.start是Thread中的方法與這上面的start無關
clients.add(c);
//dis.close();
}
} catch (Exception e) {

//e.printStackTrace();
}
finally{
try {
ss.close();
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}
}

class Client implements Runnable{

private Socket s;
private DataInputStream dis =null;
private boolean bConnected =false;
private DataOutputStream dos=null;

public Client(Socket s){
this.s=s;
try {
dis=new DataInputStream(s.getInputStream());
dos =new DataOutputStream(s.getOutputStream());
bConnected =true;
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
}

public void send(String str)throws Exception{
dos.writeUTF(str);

}

public void run() {
try{
while(bConnected){
String str = dis.readUTF();
System.out.println(str);
for(int i=0;i<clients.size();i++){
c=clients.get(i);
c.send(str);
}
/*for(Iterator<Client> it=clients.iterator();it.hasNext();){
Client c=it.next();
c.send(str);
}*/
}
}catch(SocketException e){
clients.remove(this);
System.out.println("客戶下線了");
}
catch(EOFException e){
System.out.println("Client closed");
}
catch (Exception e){

//e.printStackTrace();
}
finally{
try {
if(dis !=null) dis.close();
if(dos !=null) dos.close();
if(s!=null) s.close();
} catch (Exception e1) {
// TODO 自動生成 catch 塊
//e1.printStackTrace();
}
}
}
}

}

第一個是客戶端,
第二個是server端

10、在java nio網路通信中,一台客戶端使用一個埠號,對應一台伺服器同一埠號,也就是說同一so

埠可以復用的,這個在java培訓教材的網路裡面有的。不過現在都有現成框架的,沒人從頭寫

與javanio伺服器相關的知識