0

I am new to android and I want to create a WiFi based chat engine using socket programming. I was able to retrieve the list of devices connected to WiFi using this project, now I want to create a server socket on my app to listen for connections on some port (say 6066).

My concern is that all the devices on WiFi network deployed with my app will start their service listening on the same port (say there are 192.168.1.100:6066 and 192.168.1.101:606). When I want to connect to the device with the port 6066, will it accept it?

An alternative I've considered is to specify the port as 0 in serversocket. It will assign a temporary port, but how would the client side know which one was assigned to request a connection?

jerry
  • 2,545
  • 1
  • 18
  • 32
  • There will be no confusion between ports on different hosts - as long as *either* the IP address *or* the port is unique, you have no problem as the *combination* of IP and port is therefore unique. However there is the chance that something on a given device might already be using the port you want. Most commonly, that's another version of your own project, but it could be someone else. Another possibility is something having "just used" the port without the flags which permit it to be immediately re-used. – Chris Stratton Feb 06 '14 at 18:31

1 Answers1

-1

yes u can set the same port number for all the clients. but u can‘t assign 0 as port number, always assign port number above 1024. and here nothing is automatically assigned. everything is assigned by the user.

sam
  • 83
  • 1
  • 7
  • Here all clients acts as servers for other clients requesting does it possible i mean i have 10 devices each running server code with same port and whenever a device want to connect to another device it requests the device on same port –  Feb 06 '14 at 18:22
  • Yes it is possible with the same port number. Unless the IP address which you provide isn‘t same it shouldn‘t be problem. Port number is basically a address of the app which is in ur computer, so it has nothing to do with address of app stored in other devices. – sam Feb 06 '14 at 18:56
  • You can indeed assign zero as the port number. It means that the system chooses an ephemeral port. – user207421 Feb 10 '14 at 02:34