急!Java下 端口复用问题!
各位,大写请帮我看看,我已经没折了,C++下直接使用Winsock Api可以实现地址复用,但是Java下老是无法实现地址复用。
开发环境:
OS: Win XP Sp3
Jdk: 1.6
以下是测试问题的代码。
//=======================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author huzhigang
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//监听3333端口
ServerSocket server=null;
Socket client = null;
try {
server = new ServerSocket();
server.setReuseAddress(true);
server.bind(new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(),3333));
while(true){
client=new Socket();
client.setReuseAddress(true);
//绑定到3333端口
client.bind(new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(),3333));
//连接9999端口
client.connect(new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(),9999));
client.close();
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
开发环境:
OS: Win XP Sp3
Jdk: 1.6
以下是测试问题的代码。
//=======================
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author huzhigang
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//监听3333端口
ServerSocket server=null;
Socket client = null;
try {
server = new ServerSocket();
server.setReuseAddress(true);
server.bind(new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(),3333));
while(true){
client=new Socket();
client.setReuseAddress(true);
//绑定到3333端口
client.bind(new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(),3333));
//连接9999端口
client.connect(new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(),9999));
client.close();
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
...全文
相关推荐
Socket端口复用
然而,需要注意的是,端口复用也可能带来潜在的安全问题,因此在实际应用中需谨慎使用。
reDuh 端口复用工具
reDuh是一款基于Java与PHP开发的端口复用工具,包含客户端和服务端组件。客户端采用Java编写,通过Ant构建,服务端由PHP实现支持TCP/UDP套接字通信,可用于网络隧道和流量转发。项目具
java web开发中常遇到的问题
1. java.lang.IllegalArgumentException: node to traverse cannot be null!
每个JAVA初学者都应该搞懂的问题!
### 每个JAVA初学者都应该搞懂的问题详解#### 问题一:我声明了什么!在Java中,当我们执行 `String s = "HelloWorld!"
你必须知道的261个Java语言问题 梁建全.pdf
尽管书中不可能包含Java编程中的所有问题,但它所涉及的都是大多数Java程序员在学习和工作中经常遇到的问题,具有很强的代表性。


Java SE
