急!Java下 端口复用问题!

phoenixandlinda phoenixandlinda 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
各位,大写请帮我看看,我已经没折了,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();
}


}

}
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
phoenixandlinda
Java SE 62,621 成员 307,251 内容