Android client

 

package com.test.shb;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;

public class NTclient {
 private class UserInfo {
  String UserName;
  int UserID;

  public void setUserName(String str) {
   this.UserName = str;
  }

  public void setUserId(int ID) {
   this.UserID = ID;
  }
 }

 // 定义检测SocketChannel的Selector对象
 private Selector selector = null;
 // 定义处理编码和解码的字符集
 private Charset charset = Charset.forName("UTF-8");
 // 客户端SocketChannel
 private SocketChannel sc = null;

 public void init(String ipAddr) throws IOException {
  selector = Selector.open();
  System.out.println("ip:");
  System.out.println(ipAddr.toString());
  InetSocketAddress isa = new InetSocketAddress("192.168.0.100", 9696);
  // 调用open静态方法创建连接到指定主机的SocketChannel
  sc = SocketChannel.open(isa);
  // 设置该sc以非阻塞方式工作
  sc.configureBlocking(false);
  // 将SocketChannel对象注册到指定Selector
  sc.register(selector, SelectionKey.OP_READ);
  // 启动读取服务器端数据的线程
  new ClientThread().start();
  // 创建键盘输入流
  // Scanner scan = new Scanner(System.in);
  // while (scan.hasNextLine()) {
  // 读取键盘输入
  // String line = scan.nextLine();
  // 将键盘输入的内容输出到SocketChannel中
  // sc.write(charset.encode(line));
  // }
  String userinfo = "Name:suping ID:123456";
  sc.write(charset.encode(userinfo));
  sendImage(sc);
 }

 public void sendImage(SocketChannel sc) {
  try {
   System.out.println("test 0");
   // Obtain a channel
   WritableByteChannel channel = Channels.newChannel(sc.socket()
     .getOutputStream());
   // new FileOutputStream("D:1.jpg").getChannel();
   File file = new File("D:1.jpg");
   InputStream inputStream = new BufferedInputStream(
     new FileInputStream(file));
   System.out.println("test 1");
   // Create a direct ByteBuffer;
   // see also Creating a ByteBuffer
   ByteBuffer buf = ByteBuffer.allocateDirect(10);
   System.out.println("test 2");
   byte[] bytes = new byte[1024];
   int count = 0;
   int index = 0;
   System.out.println("test 3");
   // Continue writing bytes until there are no more
   while (count >= 0) {
    System.out.println("test 4");
    if (index == count) {
     count = inputStream.read(bytes);
     index = 0;
    }
    // Fill ByteBuffer
    while (index < count && buf.hasRemaining()) {
     System.out.println("test 5");
     buf.put(bytes[index++]);
    }

    // Set the limit to the current position and the position to 0
    // making the new bytes visible for write()
    buf.flip();

    // Write the bytes to the channel
    int numWritten = channel.write(buf);

    // Check if all bytes were written
    if (buf.hasRemaining()) {
     // If not all bytes were written, move the unwritten bytes
     // to the beginning and set position just after the last
     // unwritten byte; also set limit to the capacity
     buf.compact();
    } else {
     // Set the position to 0 and the limit to capacity
     buf.clear();
    }
   }
   System.out.println("test 6");
   // Close the file
   channel.close();
  } catch (Exception e) {
  }
 }

 // 定义读取服务器数据的线程
 private class ClientThread extends Thread {
  public void run() {
   System.out.println("1");
   try {
    System.out.println("2");
    while (selector.select() > 0) {
     System.out.println("3");
     // 遍历每个有可用IO操作Channel对应的SelectionKey
     for (SelectionKey sk : selector.selectedKeys()) {
      // 删除正在处理的SelectionKey
      selector.selectedKeys().remove(sk);
      // 如果该SelectionKey对应的Channel中有可读的数据
      if (sk.isReadable()) {
       // 使用NIO读取Channel中的数据
       SocketChannel sc = (SocketChannel) sk.channel();
       ByteBuffer buff = ByteBuffer.allocate(1024);
       String content = "";
       while (sc.read(buff) > 0) {
        sc.read(buff);
        buff.flip();
        content += charset.decode(buff);
       }
       // 打印输的内容
       System.out.println("聊天信息:" + content);
       // 为下一次读取作准备
       sk.interestOps(SelectionKey.OP_READ);
      }
     }
    }
   } catch (IOException ex) {
    ex.printStackTrace();
   }
  }
 }

 public static void main(String[] args) throws IOException {
  new NTclient().init("192.168.0.100");
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值