Connecting to broadcast IP address - java

I am currently working on simple java program that should be able to seek out computer in a local network that runs my second java application, all using UDP networking. One of those apps opens DatagramSocket and starts a thread that processes all of the inputs. The other application connects to broadcast address of current LAN network (e.g. 192.16.0.255), sends a packet and receives the response. I'm not very familiar with the way this works but here is what I'm wondering:
If I launch two of those responding applications each on different computer of the same network and run client application on other computer, which of those will it connect to?
I thought it would connect and send packet to both but it connected just to one of them and sometimes not the same one.
Could you please explain this matter to me? I would appreciate it.

If I launch two of those responding applications each on different computer of the same network and run client application on other computer, which of those will it connect to?
Neither, UDP is a connectionless protocol.
I thought it would connect and send packet to both but it connected just to one of them and sometimes not the same one.
UDP is a lossy protocol, sometimes the data will go to both, one or neither. Your router could be setup to try to direct the broadcast traffic, but usually it will attempt to send all packets to all listeners.
BTW: All the listeners must be on the 192.168.0.255 C class subnet. A host with an IP address of 192.168.1.1 may not see this packet.

Related

How to discover computers in this setup?

I have 3 computers, on the same local network. Computer #1 is a TCP server and computers #2 and #3 are TCP Clients.
When the server starts I am trying to find the IP Addresses of all available clients automatically, so I don't need to enter the IP addresses manually.
I have limited networking experience, can someone please list the ways to do the above?
There are at least 2 ways:
Send a UDP Broadcast message to every computer on the network from server and clients will reply back their IP. This seems not ideal.
Somehow (not sure how) the server set's up a hostname, e.g. "http://localhost//myapp" and clients check every few seconds if the hostname is up and then connect to server. This seems to be implemented in Java RMI.
I am trying to archive my goal using the Java API if possible and avoid writing much code.

How java serverSocket and client socket interacts within the same pc?

In accordance to example in this link:
http://www.javatpoint.com/socket-programming
As i understand port no :6666 is an imaginary or raw port used to illustrate socket programming. I want to know how the PC knows that it has server with port 6666 after running both myClient.java and myServer.java.
Also I want to know while doing Real socket programming the myServer.java needs to be placed in real server location if not then where ? just want to understand where to initialize a serverSocket class object! In server side or in client side ?
Also how Operating system or PC(in general ) search for available ports ?
Your linked document is broken, nevertheless, let me explain a bit how the network sockets work.
A computer has multiple network interfaces. If you're running window you can check them by running ipconfig /all, on linux/osx with ifconfig. You'll see that you have a loopback interface with IP address 127.0.0.1. Also, by convenience it was decided to add a "name" to this loopback interface, and it'd be localhost. You can verify this in /etc/hosts file where a mapping between 127.0.0.1 and localhost exists.
Saying that, a computer can find a route to localhost on himself using the system kernel. This loopback interface is virtual, implemented in the operating system so no packets will go through your Ethernet interface or wifi card.
TCP and UDP are protocols used on top of IP to send data. TCP establishes a connection via the 3-way handshake and packet reception is acknowledged by the server. UDP is non-connection oriented, so a client will send packets to the ports and no acknowledges are sent. That's just a huge summary.
When you want to listen on a port, your application needs to actually tell it to the operating system and when the networking component of the OS receives some packets with the TCP.dst value equal to 6666 (in your case) it will send the payload to your application. The OS is responsible for acknowledging the packets and all the underlaying communication which is transparent from you.
As you might guess, the operating system can only bind the same port port to a single application. That's why if you start twice a web server, the second execution will fail.
You can check which ports are listening with netstat -l on a linux machine.

Getting IP Address of another device (Sever) in Android

I am trying to build an android application that connects two or more devices as a client/server(using socket).
But problem is in client device user need to manually put IP address of Server device to connect with server. But from the client i don't know the server IP Address. and i don't want to enter it manually.
is there any way to get IP address(programmatically) of server device that using same application and on the same network ?
Any help would be greatly appreciated.
Thank in advanced.
After trying many ways finally, I have got a solution which is
Network discovery using UDP broadcast (credit goes to this documentation)
(Thanks #Fildor for your suggestion to implement this service).
Solution
Using UDP packets and broadcasting them! This technique however is not optimal, but as long as we stay in one network this shouldn’t be a problem.
UDP packets however are fairly easy to work with.
Server implementation
Open a socket on the server that listens to the UDP requests.
Make a loop that handles the UDP requests and responses
Inside the loop, check the received UPD packet to see if it’s valid
Still inside the loop, send a response to the IP and Port of the
received packet
Client implementation
Open a socket on a random port.
Try to broadcast to the default broadcast address (255.255.255.255)
Loop over all the computer’s network interfaces and get their
broadcast addresses
Send the UDP packet inside the loop to the interface’s broadcast
address
Wait for a reply
When we have a reply, check to see if the package is valid
When it’s valid, get the package’s sender IP address; this is the
server’s IP address
CLOSE the socket! We don’t want to leave open random ports on someone
else’s computer

Socket communication between Gears on OpenShift

I have 2 DIY Gears running simple a Java Client-Server Application with Sockets.
What I want to do is sent a command via Socket to Gear1, which sends a command via Socket to Gear2, then sends back to the user whatever data Gear2 sent back.
The problem is that I can't connect to Gear2 from Gear1.
Gear1 address = 127.13.55.1:16000
Gear2 address = 127.7.21.129:16001
I forwarded port 16000 on Gear1 so that I can communicate with it from a local client. But when I try to connect to Gear2 from inside Gear1 I get the error message: java.net.NoRouteToHostException: No route to host.
Is there some configuration I forgot about?
The two gears will not be able to communicate with each other on those ports. Inter-gear communication is blocked on non standard ports, except in special cases with scaled applications. You would have to use port forwarding between the gears for this to work, the same as you do from your local machine.

Changing the Sending IP and Port of UDP Packet in Java

I am working on a project for androids/computers have p2p talking, and we are experimenting with hole-punching in order to get through the wifi's firewall. However the wifi needs to UDP packet needs to look like it is coming from the same machine that the initial Packet was sent to. Using netcat (and choosing which port to send from) from the same machine we can send information back into the computer. However if we are coming from a different device we need to appear as though we have the same IP address, and port. I was wondering if you can easily specify the Sending IP and Port of the packet? I was thinking of using DatagramPacket, but didn't find any methods that would work.
You can't do that from within Java. You can do it with Java plus one of the several Jpcaps in existence, but be aware it's a JNI library with all the risks that entails.

Categories

Resources