gpt4 book ai didi

java.net.绑定(bind)异常 : Address already in use: Cannot bind

转载 作者:行者123 更新时间:2023-11-29 04:39:20 28 4
gpt4 key购买 nike

对不起,我已经搜索过了,但似乎所有的答案都不能解决我的问题。尝试创建 ServerSocket 以回复多个客户端消息时出现此错误。

我的服务器代码:

package Server;

import java.net.*;
import java.io.*;

public class Server {
public final static int defaultPort = 7;

public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(defaultPort);
int i = 0;
while (true) {
try {
System.out.println("Server is running on port "
+ defaultPort);
Socket s = ss.accept();
System.out.println("Client " + i + " connected");
RequestProcessing rp = new RequestProcessing(s, i);
i++;
rp.start();
} catch (IOException e) {
System.out.println("Connection Error: " + e);
}
}
} catch (IOException e) {
System.err.println("Create Socket Error: " + e);
} finally {

}
}
}

class RequestProcessing extends Thread {
Socket channel;
int soHieuClient;

public RequestProcessing(Socket s, int i) {
channel = s;
clientNo = i;
}

public void run() {
try {
byte[] buffer = new byte[6000];
DatagramSocket ds = new DatagramSocket(7);
while (true) {
DatagramPacket incoming = new DatagramPacket(buffer,
buffer.length);
ds.receive(incoming);
String theString = new String(incoming.getData(), 0,
incoming.getLength());
System.out.println("Client " + clientNo
+ " sent: " + theString);
if ("quit".equals(theString)) {
System.out.println("Client " + clientNo
+ " disconnected");
ds.close();
break;
}
theString = theString.toUpperCase();
DatagramPacket outsending = new DatagramPacket(
theString.getBytes(), incoming.getLength(),
incoming.getAddress(), incoming.getPort());
System.out.println("Server reply to Client "
+ clientNo + ": " + theString);
ds.send(outsending);
}
} catch (IOException e) {
System.err.println(e);
}

}
}

和我的客户代码:

package Client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;

public class Client extends Object {
public final static int serverPort = 7;

public static void main(String[] args) {
try {

DatagramSocket ds = new DatagramSocket();
InetAddress server = InetAddress.getByName("192.168.109.128");
Socket s = new Socket("192.168.109.128", 7);
String theString = "";
do {
System.out.print("Enter message: ");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
theString = br.readLine();
byte[] data = theString.getBytes();
DatagramPacket dp = new DatagramPacket(data, data.length,
server, serverPort);

ds.send(dp);
System.out.println("Sent to server server: " + theString);

byte[] buffer = new byte[6000];

DatagramPacket incoming = new DatagramPacket(buffer,
buffer.length);
ds.receive(incoming);
System.out.print("Server reply: ");
System.out.println(new String(incoming.getData(), 0, incoming
.getLength()));

} while (!"quit".equals(theString));
s.close();
} catch (IOException e) {
System.err.println(e);
}
}
}

与第一个客户端连接,它工作顺利。但是从第二个客户端,它抛出 java.net.BindException: Address already in use: Cannot bind。第二个Client也可以收发消息,但是Client No还是0。

Server is running on port 7
Client 0 connected
Server is running on port 7
Client 0 sent: msg 0
Server reply to Client 0: MSG 0
Client 1 connected
Server is running on port 7
java.net.BindException: Address already in use: Cannot bind
Client 0 sent: msg 1 <<-- this one is sent from client 1 but Client No is 0
Server reply to Client 0: MSG 1

最佳答案

因此,在 RequestProcessing.run 中,您决定忽略在构造函数中接收到的套接字,并在与您正在监听的端口相同的端口上打开一个 DatagramSocket。你期望它会发生什么?

class RequestProcessing extends Thread {
Socket channel;
int soHieuClient;

public RequestProcessing(Socket s, int i) {
// *****************
// The processor should be using this socket to communicate
// with a connected client *using TCP Streams*
channel = s;
clientNo = i;
}

public void run() {
try {
byte[] buffer = new byte[6000];
// *****************************
// But, instead of using the this.channel, your code
// decides to ignore the TCP socket,
// then open another UDP *"server-side like"* socket.
// First time it's OK, but the second thread attempting
// to open another DatagramSocket on the same port will fail.
// It's like attempting to open two TCP ServerSockets on the
// same port
DatagramSocket ds = new DatagramSocket(7);

[额外]

您需要决定要使用的协议(protocol):如果您使用 ServerSocket/Socket 对,那么您可能需要 TCP 通信,所以没有 DatagramSockets.

如果您需要 UDP 通信,ServerSocket/Socket 与您的方法无关,您需要使用 DatagramSocket。构造它:

  1. > with a port on the serverside - 并且只做一次。
  2. > without any port for the client side然后用服务器地址和端口限定每个 DatagramPackets

请参阅 Oracle 站点上的教程 Datagram client/server configurations .

关于java.net.绑定(bind)异常 : Address already in use: Cannot bind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39927511/

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