gpt4 book ai didi

java - UDP 网络中的绑定(bind)错误

转载 作者:行者123 更新时间:2023-12-01 09:13:51 25 4
gpt4 key购买 nike

在我的服务器客户端程序中,通过 UDP,我的服务器接收消息,将该消息显示到控制台并从客户端显示,然后再次将其发送到客户端。

这是我的服务器,它从命令行输入其名称

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

class Stest implements Runnable {
private String str[];
DatagramSocket sock;
DatagramPacket pack;
String Sname;

Stest(String str[]) throws SocketException, UnsupportedEncodingException, IOException {
this.str = str;
this.Sname = str[0];
System.out.println(Sname);
new Thread(this).start();
}

public void run() {
while (true) {
byte[] data = new byte[1024];
try {
sock = new DatagramSocket(5050);
} catch (SocketException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
pack = new DatagramPacket(data, data.length);
try {
sock.receive(pack);
} catch (IOException e) {
e.printStackTrace();
}
String s = null;
try {
s = new String(pack.getData(), "UTF-8");
System.out.println(s);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
DatagramPacket dp = null;
try {
dp = new DatagramPacket(s.getBytes("UTF-8"), s.getBytes("UTF-8").length);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
dp.setPort(1);
dp.setAddress(pack.getAddress());
try {
sock.send(dp);
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

public class test{
public static void main(String[] args) {
try {
Stest stest= new Stest(args);

} catch (IOException ex) {
Logger.getLogger(test.class
.getName()).log(Level.SEVERE, null, ex);
}
}
}

这是我的客户端,它获取其名称、监听端口、服务器 IP 地址和服务器名称,并向服务器发送消息,并再次从服务器接收该消息,并将该消息显示到控制台。

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

class UDPClient {
String username;
String listening_port;
String serverAddress;
String servername;
public UDPClient(String username, String listening_port, String serverAddress, String servername) {
this.username = username;
this.listening_port = listening_port;
this.serverAddress = serverAddress;
this.servername = servername;
}
}
class ClientListen extends UDPClient implements Runnable {
Thread t;
DatagramSocket clientlistensocket;
DatagramPacket clientlistenpacket;
byte[] data;
public ClientListen(String username, String listening_port, String serverAddress, String servername) {
super(username, listening_port, serverAddress, servername);
try {
clientlistensocket = new DatagramSocket(Integer.parseInt(listening_port));

} catch (Exception e) {
System.out.println(e + " ClientListen");
}
t = new Thread(this, "ClientListen");
t.start();
}

public void go() {
String messag = new String(clientlistenpacket.getData());
System.out.println(messag);
}

void processing() {
data=new byte[1024];

clientlistenpacket = new DatagramPacket(data,data.length);

try {
clientlistensocket.receive(clientlistenpacket);
go();
} catch (Exception e) {
System.out.println(e + " ClientlistenProcessing");
}
}
public void run() {
while (true) {
try {
processing();
} catch (Exception e) {
System.out.println(e + "Processing+run");
}
}
}

}
class ClientSpeak extends UDPClient {

DatagramPacket clientspeakpacket;
DatagramSocket clientspeaksocket;
InetAddress ipAddress;
Thread t;

public ClientSpeak(String username, String listening_port, String serverAddress, String servername) {
super(username, listening_port, serverAddress, servername);

try {

clientspeaksocket = new DatagramSocket();

} catch (Exception e) {
System.out.println(e + "ClientSpeak");
}
try {

String firstmessage = "Via:" + servername + "To:" + servername + "From:" + username + "Port:" + listening_port;
byte[] firstdata = firstmessage.getBytes();
ipAddress = InetAddress.getByName(serverAddress);

clientspeakpacket = new DatagramPacket(firstdata, firstdata.length, ipAddress, 5050);
clientspeaksocket.send(clientspeakpacket);
} catch (Exception e) {
System.out.println(e + "Clientspeak");
}
}


}

public class testC {

public static void main(String[] args) {
if (args.length == 4) {
String username = args[0].trim();
String listening_port = args[1].trim();
String serverAddress = args[2].trim();
String servername = args[3].trim();

ClientSpeak clientSpeak = new ClientSpeak(username, listening_port, serverAddress, servername);
ClientListen clientListen = new ClientListen(username, listening_port, serverAddress, servername);
}
}
}

但它总是显示错误,例如:

S
Via:STo:SFrom:nahPort:1
Nov 21, 2016 7:20:34 PM Stest run
SEVERE: null
java.net.BindException: Address already in use: Cannot bind
at java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.bind0(DualStackPlainDatagramSocketImpl.java:84)
at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:93)
at java.net.DatagramSocket.bind(DatagramSocket.java:392)
at java.net.DatagramSocket.<init>(DatagramSocket.java:242)
at java.net.DatagramSocket.<init>(DatagramSocket.java:299)
at java.net.DatagramSocket.<init>(DatagramSocket.java:271)
at Stest.run(test.java:41)
at java.lang.Thread.run(Thread.java:745)

我应该做什么?

最佳答案

问题在于,在 while 循环内的服务器中,您不断尝试将新的数据报套接字绑定(bind)到端口 5050

try {
sock = new DatagramSocket(5050);
} catch (SocketException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}

第二次,服务器读取并处理UDP消息后,会尝试绑定(bind),但会出现异常。

您只需在 while 循环之前创建并绑定(bind) UDP 套接字一次,然后在 while 循环内使用该套接字进行整个通信。

关于java - UDP 网络中的绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40728033/

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