客戶端的<<EIT造形+Socket代碼>>_第1张图片

客户(Client)端的范例代码

Go Back ]

EE                                             EE

By 高煥堂 (寫代碼)

本文的代码摘自网页:

http://bbs.51cto.com/thread-1085282-1.html

本文将之改为EIT造形架构。


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* 使用Socket通信传输文件
* @author Administrator
*
*/

public class Client_E {
      private final static Logger logger = Logger.getLogger(ClientTest7.class.getName());
      public void init() throws Exception {
                new Thread(new MyRunnable()).start();
      }

      private final class MyRunnableimplements Runnable {
           public void run() {
                 SocketChannel socketChannel = null;
                 try {
                        socketChannel = SocketChannel.open();
                        SocketAddress socketAddress = new InetSocketAddress("localhost", 10000);
                        socketChannel.connect(socketAddress);

                        onSendFile(socketChannel);

                        onReceiveFile(socketChannel);

                      }

                } catch (Exception ex) {  logger.log(Level.SEVERE, null, ex);
                } finally {
                             try {  socketChannel.close();
                             } catch(Exception ex) {}
               }
     }

    public abstract onSendFile(SocketChannel socketChannel);

    public abstract onReceiveFile(SocketChannel socketChannel);

}

//-----------------------------------------------------------------------

public class Client_T extends Client_E {

public abstract onSendFile(SocketChannel socketChannel)

      sendFile(socketChannel, new File("E:/socketclientdemo/src/iisant/jframe/socket/client/test7/test7_client.txt"));
         } catch (Exception ex) {  logger.log(Level.SEVERE, null, ex);
         } finally {
               try {  socketChannel.close();
               } catch(Exception ex) {}
        }
    }    

//-----------------------------------------------

public abstract onReceiveFile(SocketChannel socketChannel);

            receiveFile(socketChannel, new File("E:/socketclientdemo/src/iisant/jframe/socket/client/test7/client_receive.txt"));
            } catch (Exception ex) {  logger.log(Level.SEVERE, null, ex);
            } finally {
                   try {  socketChannel.close();
                   } catch(Exception ex) {}
            }

    }      

    //---------------------------------------------------
    * 发送文件
    * @param socketChannel
   * @param file
   * http://home.51cto.com/index.php?

      s=/space/2305405" target="_blank">@throws IOException
    */
 private void sendFile
(SocketChannel socketChannel, File file) throws IOException {
      FileInputStream fis = null;
      FileChannel channel = null;
      try {
          fis = new FileInputStream(file);
          channel = fis.getChannel();
          ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
          int size = 0;
          while ((size = channel.read(buffer)) != -1) {
                buffer.rewind();
                buffer.limit(size);
                socketChannel.write(buffer);
                buffer.clear();
           }
          socketChannel.socket().shutdownOutput();
      } finally {
            try { channel.close();
            } catch(Exception ex) {}
            try {   fis.close();
            } catch(Exception ex) {}
      }
   }

  //--------------------------------------------------
  /**
  * 接收文件
  * @param socketChannel
  * @param file
 
* http://home.51cto.com/index.php? s=/space/2305405" target="_blank">@throws IOException

  */
private void receiveFile(SocketChannel socketChannel, File file) throws IOException {
    FileOutputStream fos = null;
    FileChannel channel = null;
    try {
          fos = new FileOutputStream(file);
          channel = fos.getChannel();
          ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
          int size = 0;
          while ((size = socketChannel.read(buffer)) != -1) {
                 buffer.flip();
                 if (size > 0) {
                        buffer.limit(size);
                        channel.write(buffer);
                        buffer.clear();
                 }
            }
            } finally {
                    try { channel.close();
                    } catch(Exception ex) {}
            try {   fos.close();
            } catch(Exception ex) {}
        }
     }
   }
}

//----------- End  -----------------------------------