java - 运行我的聊天程序时地址已在使用中错误
问题描述
我正在尝试创建一个聊天程序,但遇到了一些问题。我试图通过运行它的两个实例(一个客户端,一个服务器)在我的机器上对其进行测试。发生这种情况时,在创建第二个实例后,我收到一个 BindException 错误,指出该地址已在使用中。我认为可能是问题的根源在于,由于聊天是大型应用程序的一小部分,我正在尝试制作一个同时具有服务器和客户端功能的程序,用户在加载应用程序时选择一个。下面,我附上了相关代码。任何和所有的帮助将不胜感激!(抱歉格式不好)
public ChatPanel(){
//Sets up main panel and variables
this.setBackground(new Color(0, 155, 228));
this.setFocusable(false);
this.setLayout(new BorderLayout());
textBox = new JTextField();
chatBox = new MessagePanel();
this.add(chatBox, BorderLayout.CENTER);
this.add(textBox, BorderLayout.SOUTH);
begin();}
public void begin() {
String[] options = {"Server", "Client"};
int entry = JOptionPane.showOptionDialog(null, "Server or Client?", "Setup",
JOptionPane.YES_NO_OPTION, JOptionPane.NO_OPTION, null, options, options[0]);
thisIsAServer = !(entry == 1);
showMessage("" + entry);
if(thisIsAServer) startRunning();
else {
serverIP = (String)JOptionPane.showInputDialog(null, "Enter the IP Address of the Host ", "Connect to Host",
JOptionPane.PLAIN_MESSAGE, null, null, "");
startRunningClient();
}
}
//Server
private void startRunning() {
try {
server = new ServerSocket(9999, 100);
while(true) {
try {
waitForConnection();
setUpStreams();
whileChatting();
} catch(EOFException eof) {
showMessage("Chat Ended");
}finally {
close();
}
}
} catch (IOException e) {e.printStackTrace();}
}
//Server
private void waitForConnection() throws IOException{
System.out.println("1");
showMessage("Waiting for connection");
connection = server.accept();
System.out.println("1.5");
showMessage("Connected to: " + connection.getInetAddress().getHostName());
}
//Server
private void setUpStreams() throws IOException{
System.out.println("2");
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("Streams successfully setup");
}
//Client
private void setUpStreamsC() throws IOException{
System.out.println("2");
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("Streams successfully setup");
}
//Server
private void whileChatting() throws IOException{
System.out.println("3");
String message = "Hello World";
sendMessage(message);
do {
try {
message = (String) input.readObject();
showMessage(message);
}catch(ClassNotFoundException cnfe) {
showMessage("Error");
}
}while(!message.equals("CLIENT - END") || !message.equals("SERVER - END"));
}
//Client
private void whileChattingC() throws IOException{
System.out.println("3");
String message = "Hello World";
sendMessage(message);
do {
try {
message = (String) input.readObject();
showMessage(message);
}catch(ClassNotFoundException cnfe) {
showMessage("Error");
}
}while(!message.equals("CLIENT - END") || !message.equals("SERVER - END"));
}
//Server
private void close() {
try {
output.close();
input.close();
connection.close();
} catch (IOException e) {e.printStackTrace();}
}
//Server
public void sendMessage(String message) {
String name = "CLIENT";
if(thisIsAServer) name = "CLIENT";
try {
output.writeObject(name + " - " + message);
output.flush();
showMessage("\n " + name + " - " + message);
}catch(IOException e) {chatBox.getChatBox().append("\nERROR");}
}
//Server
private void showMessage(String message) {
chatBox.getChatBox().append(message);
}
private void startRunningClient() {
try {
server = new ServerSocket(9999, 100);
while(true) {
try {
connectToServer();
setUpStreamsC();
whileChattingC();
} catch(EOFException eof) {
showMessage("Chat Ended");
}finally {
close();
}
}
} catch (IOException e) {e.printStackTrace();}
}
private void connectToServer() throws IOException{
showMessage("Attempting connection... \n");
connection = new Socket(InetAddress.getByName(serverIP), 9999);
showMessage("Connected to: " + connection.getInetAddress().getHostName());
}
解决方案
您正在打开一个服务器套接字
//Server
private void startRunning() {
try {
server = new ServerSocket(9999, 100);
这开始侦听端口 9999。
然后你再次在客户端代码中打开一个服务器端口。
private void startRunningClient() {
try {
server = new ServerSocket(9999, 100);
为什么在客户端需要一个 serversocket?这会导致您在问题中提到的问题。
推荐阅读
- batch-file - 删除目录中的所有文件,而不删除具有相似文件类型结尾的其他文件
- python - 从字典列表中删除单个值
- swiftui - 如何添加视图,使其位于导航工具栏和标签栏之间
- triggers - 如何正确执行分散的插件谷歌表
- reactjs - 如何访问“transformErrors”中的输入值
- scala - Scala 3 宏创建枚举
- github - Github 代码审查:更改后自动重置和重新请求批准
- cassandra - Cassandra 的 Stargate 文档 API 的分页
- javascript - 是否有任何建议使用自执行功能动态添加 jQuery?
- github - 如何使用 Deploy 密钥进行身份验证以使用 Github API?