简体   繁体   English

Java:地址已在使用中

[英]Java: Address already in use

I'm trying to create a simple class that can is able to send files over a network. 我正在尝试创建一个简单的类,该类可以通过网络发送文件。 Basically, this class is created once on each machine, a separate listening thread is started, and it can send and receive files. 基本上,该类在每台计算机上创建一次,启动一个单独的侦听线程,它可以发送和接收文件。 At this point I've hard-coded the other addresses (to a loopback) and the file-locs in order to simplify testing. 在这一点上,我已经将其他地址(到回送)和文件位置进行了硬编码,以简化测试。

Here's my class: 这是我的课:

public class ConnectionHandler  extends Thread{

    private ServerSocket sSocket;
    private Socket socket;

    public ConnectionHandler(){
        try {
            this.sSocket = new ServerSocket(6533);
            this.socket = new Socket("127.0.0.1", 6533);

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    public void sendFile(File f) {
        try {
            int count;
            byte[] buffer = new byte[1024];

            BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));
            OutputStream out = socket.getOutputStream();

            while ((count = in.read(buffer)) > 0)
            {
              out.write(buffer, 0, count);
            }

            out.flush();
            out.close();
            in.close();
            socket.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void run(){

        try {
            int count;
            byte[] buffer = new byte[1024];

            InputStream in = sSocket.accept().getInputStream();
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(location));

            while ((count = in.read(buffer)) > 0)
            {
              out.write(buffer, 0, count);
              out.flush();
            }

            out.close();
            in.close();
            sSocket.close();

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

However, I'm running into a 'Address already in use' error and cannot figure out the issue within my code. 但是,我遇到了“地址已在使用中”错误,无法在我的代码中找出问题。 Additionally, I recognize that my 'run' thread terminates after one transmission, this is intentional for testing. 另外,我认识到我的“运行”线程在一次传输后终止,这是有意进行测试的。 I plan on implementing a more robust thread that can handle multiple connections, etc. 我计划实现一个可以处理多个连接等的更强大的线程。

别的东西是侦听端口6533. netstat会告诉你什么。

However, I'm running into a 'Address already in use' error and cannot figure out the issue within my code 但是,我遇到了“地址已在使用中”错误,无法在我的代码中找出问题

Any chance you are trying to fork multiple ConnectionHandler threads? 您是否有可能尝试派生多个 ConnectionHandler线程? That would try to start multiple server sockets on more 6533 and the 2nd one would throw an exception. 那将尝试在更多的6533上启动多个服务器套接字,而第二个将引发异常。

Typically the main thread starts the server socket and a ConnectionHandler is forked after a connection is accepted. 通常,主线程启动服务器套接字,并在接受连接后派生ConnectionHandler Something like: 就像是:

// main or server thread
ServerSocket  serverSocket = new ServerSocket(6533);
try {
   while (!shuttingDown) {
       Socket socket = serverSocket.accept();
       // better would be to use an ExecutorService thread-pool here
       new Thread(new ConnectionHandlerRunnable(socket)).start();
   }
} finally {
   serverSocket.close();
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM