急!Java下 端口复用问题!

phoenixandlinda 2009-02-18 10:45:50
各位,大写请帮我看看,我已经没折了,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();
}


}

}
...全文
605 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
phoenixandlinda 2009-05-05
  • 打赏
  • 举报
回复
coldanimal 说的对!
paullbm 2009-02-19
  • 打赏
  • 举报
回复
如果建立的是TCP连接的话,那么其端口是无法复用的;
一头头 2009-02-18
  • 打赏
  • 举报
回复
按照LZ说的哇 端口的复用 2个socket复用一个port
就是你写的setReuseAddress
不过需要建立另外的一个socket哇 你写的一个socket连接2个port 显然是不可以的哇

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;
Socket client2 = null;
try {
server = new ServerSocket();
server.setReuseAddress(true);
server.bind(new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(),3333));
while(true){
client=new Socket();
client2 = new Socket();
client.setReuseAddress(true);
//绑定到3333端口
client.bind(new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(),3333));
//连接9999端口
client2.setReuseAddress(true);
client2.bind(new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(),3333));
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();
}


}

}
一头头 2009-02-18
  • 打赏
  • 举报
回复
端口复用应该是 2个socket搞一个port
LZ显然不是这个意思哇
一个socket bind一个东西 在connect另外一个东西 不可以哇
beihang2010 2009-02-18
  • 打赏
  • 举报
回复
不懂也
lzheng2001 2009-02-18
  • 打赏
  • 举报
回复
什么叫地址复用?

62,635

社区成员

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

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