📄 tcpclient.java
字号:
/**
* Created at Nov 22, 2008
*/
package com.jdev.net.connector;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;
import com.jdev.net.data.SocketDataHelper;
import com.jdev.net.event.Notifier;
import com.jdev.net.queue.Request;
import com.jdev.util.Debug;
/**
* <p>Title: TcpClient</p>
* <p>Description: </p>
* @author Lawrence
* @version 1.0
*/
public class TcpClient implements Runnable {
/**
*
*/
public TcpClient() throws IOException {
// 获取事件触发器
notifier = Notifier.getNotifier();
// 创建无阻塞网络套接
selector = Selector.open();
}
private final static String module = TcpClient.class.getName();
private SocketDataHelper socketDataHandler;
protected Notifier notifier;
private final Selector selector;
private volatile SocketChannel channel;
private String url = "127.0.0.1";
private int port = 6188;
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
int num = 0;
try {
Thread.sleep(1000);
while (!Thread.interrupted()) {
// 监听
try {
Thread.sleep(1000);
// notifier.fireOnDebug("-->Start Tcp Running...");
if(!channel.isOpen()){
channel = SocketChannel.open();
channel.configureBlocking(false);
InetSocketAddress socketAddress = new InetSocketAddress(url, port);
channel.connect(socketAddress); //绑定socketAddress
Debug.logVerbose("connecting to server:" + url + ":" + port,
module);
channel.register(selector, SelectionKey.OP_CONNECT, new Connector(selector,channel));
continue;
}
num = selector.select();
// notifier.fireOnDebug("-->selected event...");
if (num > 0) {
Set<SelectionKey> selectedKeys = selector
.selectedKeys();
Iterator<SelectionKey> it = selectedKeys.iterator();
while (it.hasNext()) {
SelectionKey key = (SelectionKey) it.next();
it.remove();
dispatch(key);
selectedKeys.clear();
}
}
} catch (Exception e) {
notifier.fireOnError("-->Error occured in Client: "
+ e.getMessage());
continue;
}
}
} catch (Exception e) {
}
}
public void openSocketChannel(String url, int port) throws Exception {
try {
this.url = url;
this.port = port;
channel = SocketChannel.open();
channel.configureBlocking(false);
InetSocketAddress socketAddress = new InetSocketAddress(url, port);
channel.connect(socketAddress); //绑定socketAddress
Debug.logVerbose("connecting to server:" + url + ":" + port,
module);
channel.register(selector, SelectionKey.OP_CONNECT, new Connector(selector,channel));
} catch (IOException e) {
Debug.logError(e, module);
throw new Exception(e);
}
}
private void dispatch(SelectionKey key) throws Exception {
Runnable r = (Runnable) (key.attachment());
if (r != null) {
r.run();
}
}
public void close() {
if (channel != null) {
try {
SelectionKey key = channel.keyFor(selector);
key.cancel();
channel.close();
} catch (Exception ignored) {
}
}
}
public static void main(String[] args) throws Exception {
TcpClient t = new TcpClient();
t.openSocketChannel("127.0.0.1", 6188);
Thread s = new Thread(t);
s.start();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -