0

My company creates their own custom android hardware for our system.

We currently have 2 android devices that need to communicate together and sync up the sqlite databases inside of them.

I started writing a custom socket protocol to fix this problem. This was based on a server client model. However as I did more development I found that each device needs to have a server socket running and it is possible to communicate 3 or more devices in the system and also there is a possibility that the server could die or reboot or crash.

So my idea is to have each device search a set of ports that I store in an array and bind to one that is currently available when the device checks and create my ServerSocket around that.

This means that maybe device1 has ServerSocket(1111) and device2 has ServerSocket(1112), etc.

Each device then sends a message through multiple ports. Ex: device1 iterates through the array and opens a client socket and sends a message for each port: Socket socket = new Socket(IP,1112); sends message... Socket socket = new Socket(IP,1113); sends message... etc

Ofcourse I have already made this multithreaded (ran by an android service) so no blocking of the UI thread in the android device.

My question is, am I doing this right? Or is there a better way in Java to do this?

EDIT:

I decided to add some code to explain my problem.

I have multiple android devices connected together through TCP/IP and I create a ServerSocket on each device like this:

ServerSocket serverSocket = new ServerSocket(4000);

Now when the second device tries this, it gives me an error in Java because the socket is being used by another device. Their IP addresses are something like 172.23.3.128 and 172.23.3.127.

So even having different IP addresses I am getting an error from Java saying that I already binded to that port/IP which is true. This is what I don't understand.

Juan Acevedo
  • 1,768
  • 2
  • 20
  • 39

3 Answers3

1

This doesn't make sense. The devices must each have their own IP address: they don't need different port numbers as well. They can all use the same port number.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

You could also:

  • have the clients listen on a specific UDP port
  • have the clients also broadcast to that port, annoucing their presence
  • once clients receive a broadcast, they can initiate a direct connection from an ephemeral port to a pre-determined secondary port -- either over TCP or UDP.

This is how it's usually done at least.

Community
  • 1
  • 1
jedwards
  • 29,432
  • 3
  • 65
  • 92
  • Problem here is that each client needs to act as a server. Basically each device opens a ServerSocket and each device sends information to different ports. With UDP, we are still in a 1 sever, many clients model. – Juan Acevedo Mar 29 '13 at 22:42
  • @Juan, maybe you misunderstand. All clients do exactly the same thing; there is no "server". All clients both broadcast their presence as well as listen for others' broadcast. – jedwards Mar 30 '13 at 11:40
  • I believe you are correct. However, This is not working. When I register a ServerSocket at a specific port in java, I cannot register to it from another device again, I can only send a message from my client to that port. I will add some code. – Juan Acevedo Apr 01 '13 at 16:48
  • Ok, so I know where my disconnect was. On my confusion of making this work, I was trying to open the same port on the same device (I am using a terminal for each device and I was did telnet to the other devices), it was Friday night. I came this morning and looked again at the problem and I understood. Thanks! – Juan Acevedo Apr 01 '13 at 17:01
  • I know the feeling :-) Well great, happy to help. – jedwards Apr 01 '13 at 17:03
0

Basically Server needs to run without failure, your thought is disruption of server. Otherwise you can use single server that accepts connection from several clients each in a port eg: 5000,5001,5002,etc.

Fix the socket port for each client .Server opens an array as you tried.

What do you meant by sending messages through multiple ports? Use a single port for each client otherwise you cant recognise which client is sending message.

user207421
  • 305,947
  • 44
  • 307
  • 483
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
  • Basically each client also needs to Open serverSocket. I did create the array like u said and it seems to be working fine. I am just wondering if this is the right direction. – Juan Acevedo Mar 29 '13 at 22:43