阻塞式Io

public class Client { public static void main(String[] args) throws Exception{ client(); }服務器

public static void client()throws Exception{網絡

//1獲取網絡通道
	
   SocketChannel socket= SocketChannel.open(new InetSocketAddress("127.0.0.1",8096));

   //3 獲取本地的文件通道
   FileChannel fileChannel=FileChannel.open(Paths.get("d:/test/aa.txt"), StandardOpenOption.READ);

   //2 獲取緩衝區
   ByteBuffer buf=ByteBuffer.allocate(1024);

   //4 讀取本地文件,發送到服務器

   while (fileChannel.read(buf)!=-1){
       buf.flip();
       socket.write(buf);
       buf.clear();
   }
    //5 關閉通道
   fileChannel.close();
   socket.close();

} }socket

public class Server {code

public static void main(String[] args)throws Exception {

     server();
}

public static void server()throws Exception{server

//1 創建網絡通道 ,這個是靜態方法,打開服務器端套接字通道
   ServerSocketChannel socketChannel =ServerSocketChannel.open();

   //4 獲取本地文件通道
   FileChannel fileChannel=FileChannel.open(
           Paths.get("d:/test/a_a_a.txt"),
           StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE);

   //2 綁定鏈接  InetSocketAddresss是SocketAddress(是個抽象類)的子類
   socketChannel.bind(new InetSocketAddress(8096));

   //3接受到此通道套接字的鏈接。 接受客戶端鏈接的通道,要用套接字通道來接收
   SocketChannel sclient = socketChannel.accept();

   //4創建緩衝區
   ByteBuffer buffer=ByteBuffer.allocate(1024);

   while (sclient.read(buffer)!=-1){
       buffer.flip();
       fileChannel.write(buffer);
       buffer.clear();
   }

   fileChannel.close();
   sclient.close();
   socketChannel.close();

} }xml

//下面的是有交互的,仍是阻塞式的ip

public class Client1 { public static void main(String[] args) throws Exception{ client(); } public static void client() throws Exception{get

//1創建網絡套接字經過
	
   SocketChannel socketChannel =SocketChannel.open(new InetSocketAddress("127.0.0.1",8989));

   //2獲取文件傳輸通道
   
   FileChannel fileChannel =FileChannel.open(Paths.get("d:/test/3.xml"),StandardOpenOption.READ);

   //3獲取緩衝區
   ByteBuffer buffer=ByteBuffer.allocate(1024);

   //4進行文件傳輸
   while (fileChannel.read(buffer)!=-1){
       buffer.flip();
       socketChannel.write(buffer);
       buffer.clear();
   }
   //關閉向外寫出的流
    socketChannel.shutdownOutput();
   //5接收服務器端的信息

   while (socketChannel.read(buffer)!=-1){
       buffer.flip();
       System.out.println(new String(buffer.array(),0,buffer.limit()));
       buffer.clear();
   }
   //6關閉通道
   socketChannel.close();
   fileChannel.close();

}it

} public class Server1 {io

public static void main(String[] args) throws Exception{
   server();
}

public static void server()throws Exception{

    //1打開服務器端套接字通道
    ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
    //2 綁定鏈接
    serverSocketChannel.bind(new InetSocketAddress(8989));
   //3接受套接字通訊通道
    SocketChannel socketChannel=serverSocketChannel.accept();

    //4 打開本地文件通道
    FileChannel fileChannel = FileChannel.open(Paths.get("d:/test/3_copy_copy.xml"),
            StandardOpenOption.WRITE,StandardOpenOption.CREATE);

    //5創建個緩衝器
    ByteBuffer buffer =ByteBuffer.allocate(1024);

    //6將內容寫入到本地中
    while (socketChannel.read(buffer)!=-1){
        buffer.flip();
        fileChannel.write(buffer);
        buffer.clear();
    }

    //7給客戶端回條消息
    buffer.put("消息收到了".getBytes());
    buffer.flip();
    socketChannel.write(buffer);

    //8關閉通道
    socketChannel.close();
    serverSocketChannel.close();
    fileChannel.close();


}

}

相關文章
相關標籤/搜索