Error sending files via Java Socket

Asked

Viewed 143 times

1

Java server.

   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);
         }
     }   
  }

Java client.

  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 Server.java first and then 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.Servidor.main(Server.java:9) C: Users Developereng Appdata Local Netbeans Cache 8.2 executor-snippets run.xml:53:

Java returned: 1 BUILD FAILURE (total time: 0 seconds)

If anyone can tell me why it generates this flaw and, if possible, help me correct code, I would be very grateful!!

1 answer

2

Is there any process on your machine listening at the door 5656. Whereas you are using Windows:

netstat -ano | find "5656"

lists the listening process at the door 5656. Then use

taskkill -pid "pid do processo" /f

to kill the process by eavesdropping on the door 5656.

  • Hi, thanks for your help, but you’re still making the mistake. I killed the process at the door and even tried to use other doors, even so it keeps giving error. I would have some other suggestion?

  • @Did Andréspironello even using other ports generate the same error? Are you sure you are stopping the execution of the project when it throws the error before running it again?

  • yes, I stopped running whenever I was going to try again. I even tried to restart the computer, yet it kept generating the same error

  • 1

    I was able to solve the problem. When I created the connection between the server and the client, the client’s program was locked when trying to read what the server was sending through the port, because the server was not sending anything! As I had not implemented either the method to close the client connection, nor an exception to handle it, the connection on the port remained open even if I closed the server. With the modifications I made, the client reads what the server sends, prints the result and closes in sequence. Still, thank you very much for the help!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.