简体   繁体   English

Java套接字编程 - 已在使用的地址(errno = 98)

[英]Java Socket Programming - Address already in use (errno=98)

I'm try to write a basic server-client java application. 我正在尝试编写一个基本的服务器 - 客户端Java应用程序。 However, I alway got a message of "socket bind on 10 for port 13244 failed: Address already in use (errno=98)" error no matter what port I'm using. 但是,无论我使用什么端口,我总是得到“端口13244上的套接字绑定失败:地址已经在使用(错误= 98)”错误的消息。

I have attach a source code of my application, just wondering is there very stupid mistake I'm making. 我附上了我的应用程序的源代码,只是想知道我正在犯的是非常愚蠢的错误。

Many Thanks! 非常感谢!

Cheers, J 干杯,J

/**
 * Command process test.
 */

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

public class CommandProcessTest implements Runnable{
  private static final int PORT = 13244;
  private ServerSocket serverSocket;

  public static void main(String[] args) {
    CommandProcessTest test = new CommandProcessTest();

    System.out.println("starting server.");
    test.start();
    System.out.println("server start up.");

//     try {
//       test.wait(100);
//     } catch(InterruptedException e) {
//     }


//     Thread client = new Thread(test);

    System.out.println("Server start receiving.");
    test.start();
    System.out.println("Server exit.");
  }

  private void start() {
    try {
      serverSocket = new ServerSocket(PORT);
    } catch (IOException e) {
      System.out.println("Could not listen on port: 4444");
      System.exit(-1);
    }
  }

  private void server() {
    Socket clientSocket = null;

    try {
      clientSocket = serverSocket.accept();
      CommandProcess cp = new CommandProcess(clientSocket);

      int cmd = 10;
      String arg = "";
      cp.sendCommand(cmd);

      arg = "hello";
      cp.sendCommand(cmd, arg.split(" "));

      arg = "hello world";
      cp.sendCommand(cmd, arg.split(" "));

      arg = "world hello world";
      cp.sendCommand(cmd, arg.split(" "));

    } catch (IOException e) {
      System.out.println("Accept failed: 4444");
      System.exit(-1);
    }
  }

  private void client() {
    try {
      Socket socket = new Socket("localhost", PORT);
      CommandProcess cp = new CommandProcess(socket);

      while(true) {
        int cmd = cp.getCommand();
        String[] args = cp.getArguments();

        String s = "Command: " + Integer.toString(cmd);
        if(args != null) {
          for(int i = 0; i < args.length; i++) {
            if(args[i] == null) {
              break;
            }
            s += args[i];
          }
        }

        System.out.println(s);
      }

    } catch(IOException e) {
      System.out.println("Would not connect to local host: 444");
      System.exit(-1);
    }
  }

  public void run() {
    System.out.println("Starting client");
    client();
    System.out.println("Client startup.");
  }
}

You do start() twice. 你做两次start() You cannot start two servers listening on the same port. 您无法启动在同一端口上侦听的两台服务器。

你调用test.start()两次,第二次会失败,因为第一次抓住套接字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 套接字编程 - BindException - 地址已在使用中 - Socket Programming - BindException - Address already in use 套接字-地址已在使用中 - Socket - Address already in use Java:地址已在使用中 - Java: Address already in use 尝试套接字连接后已在使用的地址 - Address already in use after attempting socket connection BindException:地址已在客户端套接字上使用吗? - BindException: address already in use on a client socket? 尝试使Java套接字程序正常工作,但获得“ java.net.BindException:地址已在使用中6666” - Trying to get a java socket program to work, but getting “java.net.BindException: Address already in use 6666 ” “java.net.BindException:已经在使用的地址”,当尝试快速创建Socket并进行负载测试时进行销毁 - “java.net.BindException: Address already in use” when trying to do rapid Socket creation and destruction for load testing 遇到java.net.BindException:服务器客户端套接字应用程序上已在使用的地址(绑定失败) - Running into an java.net.BindException: Address already in use (Bind failed) on server- client socket app 在同一台PC中使用Java套接字传输文件时出现“ BindException:地址已在使用中”异常 - “BindException: Address already in use” exception when transferring file using java socket in a same pc udp服务器上的Java中已经使用的地址错误 - address already in use error in java on udp server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM