各位,大写请帮我看看,我已经没折了,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();
}
}}
LZ显然不是这个意思哇
一个socket bind一个东西 在connect另外一个东西 不可以哇
就是你写的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();
}
} }