Error sending files via Java Socket

1

Server.java

   package javaapplication48;

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

   public class Servidor {

      public static void main(String[] args) throws IOException {
         ServerSocket servidor = new ServerSocket(5656);

         Socket sv = servidor.accept();

         ObjectInputStream out = new ObjectInputStream(sv.getInputStream());

         FileOutputStream file = new 
   FileOutputStream("C:\Users\DeveloperEng\Documents\newOrder.xml");

         byte[] buf = new byte[4096];

         while (true) {
            int len = out.read(buf);
            if (len == -1) break;
            file.write(buf, 0 , len);
         }
     }   
  }

Client.java

  package javaapplication48;

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

  public class Cliente {

    public static void main(String[] args) throws IOException {
        Socket cliente = new Socket("127.0.0.1", 5656);

        ObjectOutputStream out = new 
  ObjectOutputStream(cliente.getOutputStream());

        FileInputStream file = new 
  FileInputStream("C:\Users\DeveloperEng\Documents\order.xml");

        byte[] buf = new byte [4096];

        while (true) {
           int len = file.read(buf);
           if (len == -1) break;
           out.write(buf, 0, len);
       }
   }   
}

In NetBeans, I run the Server.java first and then the Client.java. But it generates the following error:

  

Exception in thread "main" java.net.BindException: Address already in   use: JVM_Bind at java.net.DualStackPlainSocketImpl.bind0 (Native   Method) at   java.net.DualStackPlainSocketImpl.socketBind (DualStackPlainSocketImpl.java:106)     at   java.net.AbstractPlainSocketImpl.bind (AbstractPlainSocketImpl.java:387)     at java.net.PlainSocketImpl.bind (PlainSocketImpl.java:190) at   java.net.ServerSocket.bind (ServerSocket.java:375) at   java.net.ServerSocket. (ServerSocket.java:237) at   java.net.ServerSocket. (ServerSocket.java:128) at   javaapplication48.server.main (Server.java:9)   C: \ Users \ DeveloperEng \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml: 53:

     

Java returned: 1 CONSTRUCTION FAIL (total time: 0 seconds)

If someone can tell you why it causes this crash and, if possible, help me correct code, I would be very grateful !!

    
asked by anonymous 15.09.2017 / 21:32

1 answer

2

There is some process on your machine listening on the 5656 port. Since you are using Windows:

netstat -ano | find "5656"

lists the process by listening on the 5656 port. Then use

taskkill -pid "pid do processo" /f

To kill the process by listening on the 5656 port.

    
15.09.2017 / 21:50