0

I've made a server/client that is setup to communicate via sockets. However I'm having some trouble connecting them.

First I start my server on port 7777 (which is open), then I start my client but get a java.net.BindException. Is my coding wrong?

Server side:

public Server(int port) { // port = 7777
        this.port = port; // dont bother with this line
        try {
            socket = new DatagramSocket(port);
        } catch (SocketException ex) {
            return;
        }
        run = new Thread(this, "Server");
        run.start(); // starts thread to listen for sent client packets
    }

Client side:

private boolean openConnection(String address, int port) { // returns boolean to check for established connection as well as establish it, port = 7777
        try {
            socket = new DatagramSocket(port); // THE ERROR IN OUTPUT POINTS TO THIS LINE
            ip = InetAddress.getByName(address);
            return true;
        } catch (UnknownHostException | SocketException ex) {
            ex.printStackTrace();
            return false;
        }
    }

Stack trace:

java.net.BindException: Address already in use: Cannot bind
    at java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method)
    at java.net.DualStackPlainDatagramSocketImpl.bind0(DualStackPlainDatagramSocketImpl.java:81)
    at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:95)
    at java.net.DatagramSocket.bind(DatagramSocket.java:376)
    at java.net.DatagramSocket.<init>(DatagramSocket.java:231)
    at java.net.DatagramSocket.<init>(DatagramSocket.java:284)
    at java.net.DatagramSocket.<init>(DatagramSocket.java:256)
    at chatprogram.client.Client.openConnection(Client.java:45)
    at chatprogram.client.Client.<init>(Client.java:35)
    at chatprogram.client.Login1$8.run(Login1.java:104)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

CMD netstat -an output:

I dont have 10 rep so: http://prntscr.com/4f1hmf and http://prntscr.com/4f1hs3

5
  • Post the stacktrace for your exception. I'm guessing you're trying to bind your server to a port thats already being used (did you remember to close the server last time you ran it?) Commented Aug 21, 2014 at 16:52
  • Yes I remembered to close the server. I'm posting the stacktrace now. Commented Aug 21, 2014 at 16:53
  • @VinceEmigh ok I added the stacktrace and the ports from when I did 'netstat -an'. Any ideas? Commented Aug 21, 2014 at 17:00
  • socket = new DatagramSocket(port); You have that on your client and server. When you call it on your server, it binds to that port. When you call it again on your client, itll attempt to bind (which it shouldnt at all) to the port your server is already bounded Commented Aug 21, 2014 at 17:01
  • @VinceEmigh that you for your response. Would I still be able to recieve packets from the server If I didn't do this? Commented Aug 21, 2014 at 17:07

3 Answers 3

1

Don't create a DatagramSocket on the client side. Just create a Socket, so it wont bind to a specific port locally (like a server would do):

new Socket(address, port);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response. However then I would not be able to recieve packets from the server: socket.receive(packet); would not work
0

Sorry for wasting your time everyone!

The error is in the client side:

socket = new DatagramSocket(); // REMOVED THE PORT PARAMETER, works fine

If I could upvote all those who responded to me here I would. But thank you all again :)

Comments

0

Do you have Hyper V enabled?

If yes:

Disable Hyper-V

Reserve the port with the following command

C:\WINDOWS\system32>netsh int ipv4 add excludedportrange protocol=tcp startport=49838 numberofports=1

and finally:

Enable Hyper-V

Check: https://github.com/googlevr/gvr-unity-sdk/issues/1002

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.