Java 套接字绑定抛出异常但工作正常
Java socket bind throwing exception but working correctly
我正在尝试制作类似于具有多个客户端的客户端服务器聊天系统的东西
服务器端代码
public class Server{
private Socket socket=null;
private ServerSocket serversocket=null;
public Server(int port){
Values val = new Values();
while(true){
try{
serversocket=new ServerSocket(port);
System.out.println("Server started\nWaiting for clients ...");
socket=serversocket.accept();
System.out.println("Client accepted");
ProcessRequest pRequest= new ProcessRequest(socket, val);
Thread t = new Thread(pRequest);
t.start();
}
catch(Exception e){
System.out.println("Socket creation exception: "+e);
break;
}
}
现在当我 运行 服务器在任何端口上监听连接时,它抛出异常
Server started
Waiting for clients ...
Client accepted
Socket creation exception: java.net.BindException: Address already in use (Bind failed)
但我能够毫无问题地在客户端和服务器之间发送和接收消息。
显示错误,但线程正确启动并按应有的方式处理请求。
那么,为什么会出现这个异常,如何解决?
Class 使用线程 --
class ProcessRequest implements Runnable{
private DataInputStream inp=null;
private DataOutputStream oup=null;
private Socket socket=null;
private Values val=null;
private String ip;
ProcessRequest(Socket s, Values v){
socket=s;
val=v;
ip=(((InetSocketAddress) socket.getRemoteSocketAddress()).getAddress()).toString().replace("/","");
}
public void run(){
try{
inp=new DataInputStream(socket.getInputStream());
oup=new DataOutputStream(socket.getOutputStream());
}
catch(Exception e){
System.out.println(e);
}
String line = "";
while (!line.equalsIgnoreCase("exit")){
try{
line = inp.readUTF();
// System.out.println(line);
String[] tokens=line.split(" ");
if(tokens[0].equalsIgnoreCase("put")){
val.setValue(ip, tokens[1], tokens[2]);
}
else if(tokens[0].equalsIgnoreCase("get")){
String value=val.getValue(ip, tokens[1]);
oup.writeUTF(value);
}
}
catch(IOException i){
System.out.println(i);
return;
}
}
try{
inp.close();
oup.close();
socket.close();
}
catch(IOException i){
System.out.println(i);
return;
}
}
ServerSocket 只需要创建一次(在本例中)并绑定到 IP 和端口。
您在这里所做的是创建多个 ServerSocket 并绑定到相同的 IP 和端口。创建不同的 ServerSockets 并绑定到同一组 IP 和端口组合显然会抛出此异常。
因此,要使其正常工作,请从循环中删除 ServerSocket 创建行。
public Server(int port){
Values val = new Values();
// Add your ServerSocket code here instead of the loop
serversocket = new ServerSocket(port);
System.out.println("Server started\nWaiting for clients ...");
while(true) {
try {
socket=serversocket.accept();
System.out.println("Client accepted");
ProcessRequest pRequest = new ProcessRequest(socket, val);
Thread t = new Thread(pRequest);
// And so on...
}
我正在尝试制作类似于具有多个客户端的客户端服务器聊天系统的东西
服务器端代码
public class Server{
private Socket socket=null;
private ServerSocket serversocket=null;
public Server(int port){
Values val = new Values();
while(true){
try{
serversocket=new ServerSocket(port);
System.out.println("Server started\nWaiting for clients ...");
socket=serversocket.accept();
System.out.println("Client accepted");
ProcessRequest pRequest= new ProcessRequest(socket, val);
Thread t = new Thread(pRequest);
t.start();
}
catch(Exception e){
System.out.println("Socket creation exception: "+e);
break;
}
}
现在当我 运行 服务器在任何端口上监听连接时,它抛出异常
Server started
Waiting for clients ...
Client accepted
Socket creation exception: java.net.BindException: Address already in use (Bind failed)
但我能够毫无问题地在客户端和服务器之间发送和接收消息。
显示错误,但线程正确启动并按应有的方式处理请求。
那么,为什么会出现这个异常,如何解决?
Class 使用线程 --
class ProcessRequest implements Runnable{
private DataInputStream inp=null;
private DataOutputStream oup=null;
private Socket socket=null;
private Values val=null;
private String ip;
ProcessRequest(Socket s, Values v){
socket=s;
val=v;
ip=(((InetSocketAddress) socket.getRemoteSocketAddress()).getAddress()).toString().replace("/","");
}
public void run(){
try{
inp=new DataInputStream(socket.getInputStream());
oup=new DataOutputStream(socket.getOutputStream());
}
catch(Exception e){
System.out.println(e);
}
String line = "";
while (!line.equalsIgnoreCase("exit")){
try{
line = inp.readUTF();
// System.out.println(line);
String[] tokens=line.split(" ");
if(tokens[0].equalsIgnoreCase("put")){
val.setValue(ip, tokens[1], tokens[2]);
}
else if(tokens[0].equalsIgnoreCase("get")){
String value=val.getValue(ip, tokens[1]);
oup.writeUTF(value);
}
}
catch(IOException i){
System.out.println(i);
return;
}
}
try{
inp.close();
oup.close();
socket.close();
}
catch(IOException i){
System.out.println(i);
return;
}
}
ServerSocket 只需要创建一次(在本例中)并绑定到 IP 和端口。
您在这里所做的是创建多个 ServerSocket 并绑定到相同的 IP 和端口。创建不同的 ServerSockets 并绑定到同一组 IP 和端口组合显然会抛出此异常。
因此,要使其正常工作,请从循环中删除 ServerSocket 创建行。
public Server(int port){
Values val = new Values();
// Add your ServerSocket code here instead of the loop
serversocket = new ServerSocket(port);
System.out.println("Server started\nWaiting for clients ...");
while(true) {
try {
socket=serversocket.accept();
System.out.println("Client accepted");
ProcessRequest pRequest = new ProcessRequest(socket, val);
Thread t = new Thread(pRequest);
// And so on...
}