1

I am developing a google glass/android application. It is a video streaming application that has a server/client setup where the phone/glasses is the server and hooks the pc up with the session description for playing the video. It works great on the android and everything runs fine but as soon as I try to test it on the google glass it throws an error at this line

sSocket = new ServerSocket(sPort);

The exception message says "EADDRINUSE" which I'm assuming means the port is already opened but I never opened it. Even if I had opened it and my program didn't close it I changed the port a couple of times and it still says it's in use.

Thanks

Tyler Helmuth
  • 129
  • 2
  • 11
  • Need more code to (possibly) be able to help. – ErstwhileIII Oct 08 '14 at 15:21
  • The only reason I didn't include more code was because it's basic tcp server/client code. The ServerSocket declaration is in a separate thread as per Android specifications. The run method just has a loop with new ServiceHandler(sSocket.accept())).start(); Which adds a new socket connection. The run method in the ServiceHandler just does the io I need to do with that socket. I have tried setting soReuseAddress to true and then binding it after that but instead of EADDRINUSE I get tons of exceptions on the serversocket accept() loop. – Tyler Helmuth Oct 08 '14 at 19:33

1 Answers1

1

Tyler,

Google Glass, like android, consistently will have many of it's ports occupied by applications running in the background. When creating a socket for your server to listen on, you have two choices:

1) Have a predetermined list of ports you can choose to have your server listen on.

If you choose to do this, then you can simply have a datastructure (list, queue, heap [if you have some priority of which ports you would like to use], etc) which contain all of your ports, then you can simply traverse them until you find an open port.

This can be achieved in the following manner:

private ServerSocket allocatePort(List<Integer> myArray) throws IOException {
    for (int individualPort : myArray) {
        try {
            return new ServerSocket(individualPort);
        } catch (IOException io) {
            continue; // An exception will be thrown if this port is currently in use. It's OK, let's try another port.
        }
    }

    // When no ports are available, let's throw an exception stating we were unable to find an open port.
    throw new IOException("we were unable to find an open port");
}

Then simply invoke this method within your as follows:

int[] arrayOfPorts = {5000, 5001, 5002, 8000, 8001, 8002, 8003};
List<Integer> myArray = new ArrayList<>();
myArray = IntStream.of(arrayOfPorts).boxed().collect(Collectors.toList());
ServerSocket sSocket = allocatePort(myArray);

2) If you don't mind which port to listen in on, you can use the constructor to pick any available port.

This can be achieved as follows:

  ServerSocket sSocket = new ServerSocket(0);

You can read up more on ServerSocket's Javadocs. Notice under the parameter's subsection:

  port - the port number, or 0 to use a port number that is automatically allocated.

Please let me know if you have any questions!

Devarsh Desai
  • 5,984
  • 3
  • 19
  • 21
  • Thank you!!!!!!!! I tried three different ports and all were getting the open port error so I figured all of them were blocked or something. When I used the operating system to designate the port and just logged it it worked great. My only question is how can I communicate that port to my pc side app? I need the port to setup my socket connection. Should I just put a list of ports and loop through that on the android side until I get one that works and then have the same list on the client side that I am looping through until one works and gets the right response from the server? – Tyler Helmuth Oct 09 '14 at 01:18
  • 1
    hey Tyler, I'm SO HAPPY everything worked out for you!! :0) haha; you got it, you should have the same list of ports on the client side and cycle through them, as how you described. Hope you have a very good week ahead and please let me know if you have any other questions!! Thank you for your time! :-) – Devarsh Desai Oct 09 '14 at 04:14