问题描述
- java代码运行出现错误“Address already in use: Cannot bind”
-
这是数据接受端代码:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;public class ReceiveUDP {
public static void main(String[] args) throws IOException { //定义Socket,并开放一个端口,用来接受数据 int port=10000; DatagramSocket ds = new DatagramSocket(port); //定义一个数据缓冲池,用来存放接收到的数据 byte bytes[] = new byte[1024]; DatagramPacket dp = new DatagramPacket(bytes,bytes.length); //用Receive方法将接收的内容放入数据缓冲池 ds.receive(dp); System.out.println(dp.getPort()+":"); System.out.println(new String(dp.getData())); ds.close();
}
}
这是发送数据端代码:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;public class UDPSend {
public static void main(String[] args) throws IOException { DatagramSocket ds = new DatagramSocket(10000,InetAddress.getByName("127.0.0.1")); byte bytes[] = "hello,UDP".getBytes(); DatagramPacket dp = new DatagramPacket(bytes,bytes.length); //发送数据 ds.send(dp); System.out.println("sent!"); ds.close(); }
}
运行后出现问题:
Exception in thread "main" java.net.BindException: Address already in use: Cannot bind
at java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.bind0(Unknown Source)
at java.net.AbstractPlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.DatagramSocket.(Unknown Source)
at java.net.DatagramSocket.(Unknown Source)
at UDPSend.main(UDPSend.java:11)
请各位大神帮忙看看是什么问题,怎么解决?
解决方案
在启动JBoss 5的过程中老是报错:
java.net.BindException: Address already in use: Cannot bind
at java.net.PlainDatagramSocketImpl.bind(Native Method)
at java.net.DatagramSocket.bind(DatagramSocket.java:340)
&......
答案就在这里:java.net.BindException: Address already in use: Cannot bind
解决方案二:
端口号10000被占用了!建议查看是否重复开启了程序,如果没有,请在任务管理器里找到占用该端口的程序,关闭掉就好了!
解决方案三:
端口被占用了,使用命令netstat -ano | find "端口号"看一下
解决方案四:
关掉javaw.exe你启动的小程序自己没关闭
解决方案五:
端口被占用,换个端口
解决方案六:
你启动了多个服务端,,启动一个就好了
解决方案七:
端口 重复占用,.....................