gpt4 book ai didi

java - 套接字编程 - BindException - 地址已被使用

转载 作者:行者123 更新时间:2023-11-29 04:31:53 25 4
gpt4 key购买 nike

我理解为什么会发生绑定(bind)错误,但我已按照我能找到的任何解决方案进行操作,但无法使其正常工作。

堆栈跟踪

java.net.BindException: Address already in use: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at socket.ClientWorker.run(ClientWorker.java:30)
at java.lang.Thread.run(Unknown Source)
Picked up JAVA_TOOL_OPTIONS: -Djava.vendor="Sun Microsystems Inc"

服务器代码

@Override
public void run() {
try {
ServerSocket listener = new ServerSocket(port);
while (running) {
Socket socket = listener.accept();
new Thread(new ServerWorker(socket, airport)).start();
}
listener.close();
} catch (IOException e) {e.printStackTrace();}
}
  • 每次服务器获得传入连接时,它都会创建一个新线程来完成工作。

服务器 worker

@Override
public void run() {
try {
// Get message
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String number;
number = br.readLine();

// Add new arrival event
String[] splitMessage = number.split(" ");
String mode = splitMessage[2];
int time = Integer.valueOf(splitMessage[0]);
int airportID = Integer.valueOf(splitMessage[1]);

if(mode.equals("STANDARD")){
Event newEvent = new Event(EventType.ARRIVAL,time,airport);
airport.scheduleEvent(airportID,newEvent);
}

if(mode.equals("NULL")){
Event newEvent = new Event(EventType.NULL,time,airport);
airport.scheduleEvent(airportID,newEvent);
}

// Close
socket.close();
}
catch (IOException e){e.printStackTrace();}
}

客户端

/*-- Send a message to a different airport --*/
public void sendMessage(int port, String message){
new Thread(new ClientWorker(port, message)).start();
}
  • 客户端(机场对象)在想要与不同的机场通信时调用此消息。创建一个新线程来处理工作。

客户 worker

@Override
public void run() {
try {
Socket s = new Socket("localhost", port);
OutputStream os = s.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String sendMessage = message + "\n";
bw.write(sendMessage);
bw.flush();
s.close();
}
catch (UnknownHostException e){e.printStackTrace(); System.exit(0);}
catch (IOException e){e.printStackTrace(); System.exit(0);}
}
  • 错误追溯到这里——'Socket s = new Socket("localhost", port);'

注意事项

  • 所有工作都在同一台机器上完成。
  • 每秒可以发送数百条消息。
  • 所有客户端并行运行。
  • 客户端可能必须(非​​常)快速地连续发送多条消息。

我以为我是在为每条消息建立一个新连接,但我一定是误会了什么。

最佳答案

您正在使用 Socket socket = listener.accept(); 您需要在使用到服务器代码后关闭 socket。`

关于java - 套接字编程 - BindException - 地址已被使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43477375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com