java 实现代理服务器 无法注册通道!!

天空之蓝钻 2010-06-01 03:59:17
我想注册两个通道,一个接通浏览器,一个联通外部网站,但是对应外部网站的通道就是注册不上去:
InetSocketAddress socketAddress = new InetSocketAddress(toAddressIp,toPort);
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(socketAddress);
channel.register(selector, SelectionKey.OP_WRITE,new AttachFile(attachFile.getProxyVector(), AttachFile.WEB_TYPE));

全部代码如下:

package main;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

import util.AddressTurn;
import bean.AttachFile;
import bean.ProxyVector;

public class MainProxy {
private Selector selector;
public MainProxy() {

}

public void startProxy() throws IOException {
ServerSocketChannel serverSocketChannel;
serverSocketChannel = ServerSocketChannel.open();// 开启一个服务端通道
serverSocketChannel.configureBlocking(false);// 设为非阻塞
serverSocketChannel.socket().bind(new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(),8210));// 绑定端口8999
this.selector = Selector.open();// 开启选择器
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
selector.select(3000);
Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> iterator = keys.iterator();
while (iterator.hasNext()) {
SelectionKey keysss = (SelectionKey) iterator.next();
try {
this.keyHandle(keysss);
} catch (Exception exception) {
keysss.channel().close();
} finally {
iterator.remove();
}
}
}
}

public void keyHandle(SelectionKey key) throws IOException {
if (key.isAcceptable()) {// 初始化
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ, new AttachFile(new ProxyVector(), AttachFile.CLIENT_TYPE));
}
if (key.isConnectable()) {// 链接
SocketChannel channel = (SocketChannel) key.channel();
if (channel.finishConnect()) {
key.interestOps(SelectionKey.OP_WRITE);
}else{
}
}
if (key.isReadable()) {// 读
AttachFile attachFile = (AttachFile) key.attachment();// 取出载体
SocketChannel socketChannel = (SocketChannel) key.channel();//取出管道
if (attachFile != null) {
if(attachFile.getChannelType()==AttachFile.CLIENT_TYPE){//读客户消息
int count = socketChannel.read(attachFile.getProxyVector().getClientBuffer());
byte[] message = new byte[count];
attachFile.getProxyVector().getClientBuffer().flip();
attachFile.getProxyVector().getClientBuffer().get(message,0,count);
// attachFile.getProxyVector().getClientBuffer().rewind();
if(message[0]==0x05&&count==3){//客户端请求
attachFile.getProxyVector().getClientBuffer().clear();
attachFile.getProxyVector().getClientBuffer().put(new byte[] { 0x05,0x00 });
}
else if(message[0] == 0x05&&count>3){//客户端应答
String toAddressIp = AddressTurn.bytes2IPV4(message, 4);//站点ip
int toPort = AddressTurn.bytes2Port(new byte[]{message[8],message[9]});//站点端口

InetSocketAddress socketAddress = new InetSocketAddress(toAddressIp,toPort);
SocketChannel channel = SocketChannel.open();
channel.configureBlocking(false);
channel.connect(socketAddress);
channel.register(selector, SelectionKey.OP_WRITE,new AttachFile(attachFile.getProxyVector(), AttachFile.WEB_TYPE));


byte[] returnMessage =new byte[10];
returnMessage[0] = (byte)0x05;
returnMessage[1] = (byte)0x00;
returnMessage[2] = (byte)0x00;
returnMessage[3] = (byte)0x01;
//ip
returnMessage[4] = (byte)socketChannel.socket().getInetAddress().getAddress()[0];
returnMessage[5] = (byte)socketChannel.socket().getInetAddress().getAddress()[1];
returnMessage[6] = (byte)socketChannel.socket().getInetAddress().getAddress()[2];
returnMessage[7] = (byte)socketChannel.socket().getInetAddress().getAddress()[3];
//port
returnMessage[8] = (byte)(socketChannel.socket().getLocalPort() >> 8);
returnMessage[9] = (byte) (socketChannel.socket().getLocalPort());

attachFile.getProxyVector().getClientBuffer().clear();
attachFile.getProxyVector().getClientBuffer().put(returnMessage);
}
else{
attachFile.getProxyVector().getWebBuffer().clear();
attachFile.getProxyVector().getWebBuffer().put(message);
}
}
else if(attachFile.getChannelType()==AttachFile.WEB_TYPE){//读网站信息
int count = socketChannel.read(attachFile.getProxyVector().getWebBuffer());
byte[] message = new byte[count];
attachFile.getProxyVector().getWebBuffer().get(message,0,count);
attachFile.getProxyVector().getWebBuffer().clear();
attachFile.getProxyVector().getClientBuffer().put(message);
}

}
key.interestOps(SelectionKey.OP_WRITE);
}
if (key.isWritable()) {// 写
AttachFile attachFile = (AttachFile) key.attachment();// 取出载体
SocketChannel socketChannel = (SocketChannel) key.channel();//取出管道
if (attachFile != null) {
if(attachFile.getChannelType()==AttachFile.CLIENT_TYPE){//写客户消息
attachFile.getProxyVector().getClientBuffer().flip();
socketChannel.write(attachFile.getProxyVector().getClientBuffer());
attachFile.getProxyVector().getClientBuffer().clear();//清空客户消息
}
else if(attachFile.getChannelType()==AttachFile.WEB_TYPE){//网络端
attachFile.getProxyVector().getWebBuffer().flip();
socketChannel.write(attachFile.getProxyVector().getWebBuffer());
attachFile.getProxyVector().getWebBuffer().clear();//清空客户消息
}
}
key.interestOps(SelectionKey.OP_READ);
}

}

public static void main(String[] args) throws IOException {
MainProxy mainProxy = new MainProxy();
System.out.println("服务器开启……");
mainProxy.startProxy();
}
}
...全文
90 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧