Java NIO 时不能邦定指定IP和端口

解决SNMP4J客户端无法指定本地IP和端口的问题
本文探讨了在使用SNMP4J时遇到的无法通过Socket指定客户端本地IP和端口的问题。通过分析NIO编程限制,提出了解决方案,并展示了在Socket环境下指定IP和端口的实现方式。

在使用SNMP4J时,我想指定创建的客户端使用的本地IP和端口,因为在Socket时这是可以的,但是发现无法实现

因为SNMP4J底层的通信是使用NIO实现的,而NIO编程时貌似就不能显示的指定

例如在SNMP4J的DefaultTcpTransportMapping类里面,当作为客户端需要发送消息时,程序首先判断是否创建了这个客户端,如果没有在创建时看到这样的代码:

SocketChannel sc = null;
try {
	sc = SocketChannel.open();
	sc.configureBlocking(false);
	sc.connect(new InetSocketAddress(((TcpAddress) address).getInetAddress(),((TcpAddress) address).getPort()));
	s = sc.socket();
	entry = new SocketEntry((TcpAddress) address, s);
	entry.addMessage(message);
	sockets.put(address, entry);
	synchronized (pending) {
		pending.add(entry);
	}
	selector.wakeup();
	logger.debug("Trying to connect to " + address);
} catch (IOException iox) {
	logger.error(iox);
	throw iox;
}

 即使在SocketChannel中,他的Socket变量定义也是不能修改的:

/**
 * Retrieves a socket associated with this channel.
 *
 * <p> The returned object will not declare any public methods that are not
 * declared in the {@link java.net.Socket} class.  </p>
 *
 * @return  A socket associated with this channel
 */
public abstract Socket socket();

 

所以我直接判定Java NIO中,客户端是无法指定自己的IP和端口的!

那么有人在想为什么需要指定自己的IP和端口?具体需求我就不再说了,在计算机上虽然只有一块网卡,但是我们可以使用兼容的IP:

 

由于我的服务端程序以客户端IP来判断信息来源,现在我需要在我的电脑上做测试程序,需要同时邦定两个IP地址进行消息发送。

此时我就可以在高级设置里面设置兼容IP就可以,但是现在程序却无法选择。

 

在Socket里面可以这样写:

package com.xidian.nms.socket;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
public class SocketServer {
	public static void main(String[] args) throws Exception {
		// 创建非邦定式连接对象
		ServerSocket ss = new ServerSocket();
		// 需要邦定的本地IP和地址
		SocketAddress address = new InetSocketAddress("192.168.0.109", 2330);
		// 将连接对象邦定到地址
		ss.bind(address);
		System.out.println("服务已经启动");
		while (true) {
			// 接收请求
			Socket socketClient = ss.accept();
			// 客户端IP
			String ip = socketClient.getInetAddress().getHostAddress();
			// 客户端端口
			int port = socketClient.getPort();
			System.out.println("服务端收到请求:" + ip + "/" + port);
		}
	}
}

 

服务端很简单,你可以一行代码搞定,也可以显示的指定IP、端口,然后进行显示的服务连接操作:

package com.xidian.nms.socket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
public class SocketClient {
	public static void main(String[] args)  throws Exception{
		Socket socket = new Socket();
		// 需要邦定的本地IP
		InetAddress iaddThis = InetAddress.getByName("192.168.1.109");
		// 需要邦定的本地地址
		SocketAddress saddThis = new InetSocketAddress(iaddThis,2331);
		socket.bind(saddThis);
		// 连接的远程服务地址
		InetAddress iaddRe = InetAddress.getByName("192.168.0.109");
		SocketAddress saddRe = new InetSocketAddress(iaddRe,2330);
		// 显示连接
		socket.connect(saddRe);
//		Socket socket = new Socket("192.168.0.109", 2330);
	}
}

 

注释掉的内容是一行搞定连接的方式。

当然这是我发现的一个问题,不知道如何解决,希望对NIO了解的人指点一下。

 

自建博客地址:http://www.javacui.com/ ,内容与ITEYE同步!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值