I have coded a ip to ip video chat application using JMF for my brothers school project. In which first of all one of the user sends an udp as a chat request to another system. Then the second system sends a reply to the first system after that video chat starts. Every thing is working perfectly while there is no router b/W two systems(e.g. Both are on same lan or connected to internet via usb modem). else when i retrieve the sender's ip from UDP packet it gives the ip of router through which the sender is connected to internet so when second system replies to the request of chatting the first system never receives the reply. So how can i get the local ip of the UDP sender.
Thanks every one in advance.
You're talking about NAT traversal and it's a really hard problem. There's more on Wikipedia https://en.wikipedia.org/wiki/NAT_traversal.
If it's just for a school project, I would just make it work on a local network and not bother with NAT traversal.
Related
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
I'm coding an app which consist of two pieces. Desktop and android. There is one desktop and several android devices. (don't know the count.) I want to communicate android devices between desktop with TCP. However, android devices doesn't know desktop's lan ip address.
I thouht 2 ways:
1-Desktop app changes the local ip address on start. So android devices know the ip address. (I coded with that ip address)
2-Desktop app always tries to connect ip addresses (192.168.0.1 - 192.168.0.255) to sent desktop's ip address. And when an android device connect to the network accept the connection then know desktop's ip.
But there is some problems in both ways.
On first, you must be administrator to changing lan ip. So run command as admin with java is a problem. Because if I do this, when user start the program, uac always asks for it.
On second, I think there will be performance issues because of app always tries to connect. Exept this, when android device connect and dhcp gives it 192.168.0.5 , but loop is on 192.168.0.150. So android device have to wait for connection.
Is there a better way than these?
Look at this post Network discovery in Java using multicasting
I think this would be the best way to do it.
The server will listen for a broadcast message from client
the client sends a broadcast request asking for server ip
server receives request and replies back with server ip.
You can use the hostname. If the network is properly configured, the host name will point to the correct ip even if it changes
I have two wifi modules that connects to my access point. my android phone connects to the same access point. Is there a way in Java to be able to send a message to any of the clients from my phone using just the IP address. I do not want to run a server on my phone.
I'll appreciate any assistance. Thanks
I feel there is a confusion about IP addresses and servers in general. An IP address is a way to find an entity on the network (it is an address). Since your phone is connected to the same access point, it is (very likely) on the same subnet. What that means is that it can talk to any of the other entities without going through a gateway, that is all.
While it is able to talk to them, there has still to be some entity listening. When your phone sends a message to a particular address (the other node on the network), the packet may make it there, but for the communication to proceed:
There has to be some listener on the other end that picks that incoming message
The communication has to follow a prescribed set of rules (protocol)
From what I gather, your phone is acting as the client, not the server. It initiates communication with the other entity on the network. Depending on which node on the network you are trying to talk to, that node is supposed to be expecting that communication and know how to carry it forward.
If you clarify the setup and your desire, may be you can get more to the point response.
What you were not understanding here is that you can send the message to that device with ip address but that data will not be shown on that device's screen because you didn't provided any port number for a process which is listening on that particular device. Your message will be sent but the device would not know what do with it.
I'm currently writing an app that's to connect to a server over a wireless lan.
So far, I've got the IP address hardcoded into the app (which works perfectly), however, the next logical step is to be able to send the app the server IP address.
From what I understand, the router needs to broadcast the IP address over 255.255.255.255 - apart from that, I have no idea what to do.
Any and all help is greatly appreciated
edit
ok, so I know to get this working, all phones have to be on the same network as the server, which is fine.
What I need to do is get the phone to broadcast on the network whilst my server listens. From there, the server sends the app its IP address, then the rest of the code can continue.
Does anyone have a tutorial or anything I can follow to get both sides working?
one simple solution for you not to hardcode the server's IP or name (let us say it changes over time), would be for you to implement a name server on your network and have the android device call a local URL.
you could then post the current server's IP or name in the header of a web page on your local network. this response would then be used by android app.
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.