0

I want to make a server and client program and transfer variables between they. I tried for a server program :

package testSocket;
import java.net.*;
import java.io.*;
import javax.swing.JFrame;

public class server {

    public static void main(String[] args) throws IOException {
        
        JFrame jframe = new JFrame("TEST SEVER");
        jframe.setVisible(true);
        jframe.setSize(800, 600);
        
        try (ServerSocket sws = new ServerSocket(12480)) {
            Socket socket = sws.accept();
            
            OutputStream outs = socket.getOutputStream();
            DataOutputStream douts = new DataOutputStream(outs);
            
            douts.writeUTF("Hello.");
            douts.close();
            outs.close();
        } catch (Exception e) {
            System.out.println("Exception : " + e);
        }
        
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    
}

And I tried for a client program :

package testSocket;
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;

public class client {

    public static void main(String[] args) throws IOException {
        
        Socket tcs = new Socket("localhost", 12480);
        InputStream iss = tcs.getInputStream();
        DataInputStream dis = new DataInputStream(iss);
        String str = new String(dis.readUTF());
        
        JOptionPane.showMessageDialog(null, "test" , str , JOptionPane.YES_NO_CANCEL_OPTION);
        
        dis.close();
        iss.close();
        tcs.close();
        
    }
    
}

But the program did not work and gave the error :

Exception : java.net.BindException: Address already in use: NET_Bind

How can I solve this error ? Can you recommend anything ?

Thanks.

michael
  • 3
  • 3
  • 1
    You have something already running on that port. Either kill it or change the port number in your program. – Federico klez Culloca Jan 14 '21 at 10:51
  • 1
    Maybe you already had a instance of that program running? – Jems Jan 14 '21 at 10:53
  • I tried on different ports but it did not work, all gave the same error – michael Jan 14 '21 at 10:55
  • It gave the same error when I run it for the first time – michael Jan 14 '21 at 10:55
  • You most likely still have the same application running from a previous start in your IDE. Either find the old run and kill it or restart your IDE, which should do the same thing. – f1sh Jan 14 '21 at 10:55
  • It is also possible that an other application uses the port. I had a look at https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers, and although it doesn't list the port you use it is still possible. – tibetiroka Jan 14 '21 at 10:58
  • I tried that too but this time client did not write "str" ​​variable, just wrote test on option pane @fish – michael Jan 14 '21 at 11:00
  • 1
    Does this answer your question? [How do I resolve the "java.net.BindException: Address already in use: JVM\_Bind" error?](https://stackoverflow.com/questions/12737293/how-do-i-resolve-the-java-net-bindexception-address-already-in-use-jvm-bind) – tibetiroka Jan 14 '21 at 11:01
  • I looked it from that site, 12480 is not used @tibetiroka – michael Jan 14 '21 at 11:01
  • Maybe you can try restarting your computer or changing the port and check if the error still appears – tibetiroka Jan 14 '21 at 11:03
  • I've tried both of them, but it's the same. Do I need to open a port for this kind programs? – michael Jan 14 '21 at 11:05
  • No, java should do that by itself when you create a new server socket. – tibetiroka Jan 14 '21 at 11:09
  • Could there be something missing in my program? – michael Jan 14 '21 at 11:13
  • Maybe, but it can't be relevant. You try to create the server socket with the port, and it throws an error. There is no other actions needed before you crete the socket, and anything after that is irrelevant. – tibetiroka Jan 14 '21 at 11:16
  • Are there other ways to create a server socket? if there are I try to use them, maybe it works – michael Jan 14 '21 at 11:21
  • I think they all use the same methods in the background, at least within the standard library. (although maybe SSL sockets are different, not sure) – tibetiroka Jan 14 '21 at 11:25
  • I made a few changes, it no longer gives the error but works incompletely. It was supposed to write "test hello" in joptionpane opened in client program , and the string "hello" from server. but it just says "test". Could this be related to this issue, or is the socket working but there is an error while receiving variables? – michael Jan 14 '21 at 11:34

0 Answers0