我知道这个问题已经被问了一百万次了,但我从来没有发现过像我这样的情况。我用java编写了一个简单的聊天应用程序,几个月前它还在运行,现在我又回到了它上面,现在遇到了问题。当我在eclipse中运行我的server.java时,一切工作正常。之后,当我运行client.java文件时,我得到以下结果:
Exception in thread "main" java.net.BindException: Address already in use: NET_Bind
at java.base/java.net.DualStackPlainSocketImpl.bind0(Native Method)
at java.base/java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
at java.base/java.net.AbstractPlainSocketImpl.bind(Unknown Source)
at java.base/java.net.PlainSocketImpl.bind(Unknown Source)
at java.base/java.net.ServerSocket.bind(Unknown Source)
at java.base/java.net.ServerSocket.<init>(Unknown Source)
at java.base/java.net.ServerSocket.<init>(Unknown Source)
at chat.Server.main(Server.java:18)我已经终止了eclipse中的所有启动,在命令提示符中找到了PID并终止了它,尝试更改端口号,但每次尝试运行我的程序时,我仍然得到仍在使用的地址。我还尝试在命令提示符下运行netstat -a命令来查找正在使用的端口号,这些端口号都不是我尝试过的端口号。
Server.java(包含服务器代码和客户端处理程序类):
public class Server
{
//vector to store active clients, static so threads share the vector of clients
static Vector<ClientHandler> activeUsers = new Vector<>();
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = new ServerSocket(1234); //server is listening on port 1234
Socket s;
while (true) //infinite loop for getting client request
{
s = serverSocket.accept(); //accept the incoming request
//obtain input and output streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
String username = dis.readUTF(); //username is first thing sent from the client for naming
ClientHandler newClient = new ClientHandler(s,username , dis, dos); //create a new handler object for handling this client
//notify currently connected users that a new user has joined
newClient.notifyUsers(activeUsers, " has joined the server!");
Thread t = new Thread(newClient); //create a new Thread with this object.
activeUsers.add(newClient); //add this client to active clients list
t.start(); //start the thread.
}//end while
}//end main
}//end class Server
class ClientHandler implements Runnable
{
private String name;
DataInputStream dis;
DataOutputStream dos;
Socket socket;
boolean isloggedin;
public ClientHandler(Socket socket, String name, DataInputStream dis, DataOutputStream dos)
{
this.dis = dis; //data input stream
this.dos = dos; //data output stream
this.name = name; //client name
this.socket = socket; //socket
this.isloggedin=true; //handler is being created so user is logged in
}
}Client.java:
public class Client extends JFrame implements ActionListener
{
DataInputStream dis = null;
DataOutputStream dos = null;
final static int ServerPort = 1234;
JButton sendMessage;
JTextField messageBox;
JTextArea chatBox;
JButton logoutButton;
JButton activeUsersButton;
JFrame newFrame = new JFrame(".Chat");
String username;
Socket s = null;
public Client(String user)
{
this.username = user;
}
public void start() throws UnknownHostException, IOException
{
newFrame.setTitle("chat - " + username);
InetAddress ip = InetAddress.getByName("localhost");
//establish the connection to the server
s = new Socket(ip, ServerPort);
//obtaining input and out streams
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(username);
//readMessage thread
Thread readMessage = new Thread(new Runnable()
{
@Override
public void run()
{
while (dos != null && dis != null) {
try
{
//read the message sent to this client
String msg = dis.readUTF();
System.out.println(msg);
chatBox.append(msg);
} //end try
catch (IOException e)
{
e.printStackTrace();
}//end catch
}//end while
}//end run
});
display();
readMessage.start();
}
//not sure what this needs to do? just to satisfy an error
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object obj = e.getSource();
if(obj == sendMessage){
String msg = messageBox.getText();
try {
dos.writeUTF(msg);
chatBox.append(username + ": " + msg + "\n"); //place what the user sent in the text box
messageBox.setText("");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else{}
}
}我省略了按钮代码和UI代码,因为它只在套接字上发送/接收消息。
发布于 2018-07-06 16:13:16
当您启动服务器时,端口1234上已经有一些东西在侦听:可能是它以前的实例;或者可能在TIME_WAIT中仍有一个套接字。尝试使用SO_REUSEADDR,如下所示:
ServerSocket serverSocket = new ServerSocket();
serverSocket.setReuseAddress(true);
serverSocket.bind(new InetSocketAddress(1234));注意,你还有另一个问题。您正在忽略流的结束。如果要调用readUTF(),则需要捕获EOFException并退出循环,并在抛出套接字时关闭套接字。EOS不会神奇地使输入或输出流为空。
https://stackoverflow.com/questions/51204697
复制相似问题