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");
}
}