I have two devices 1 as a hotspot host other as a client connected.
Both are android devices.
I tried to doing it with tcp programming but was unable to do it.
Do anyone knows how to do it correctly?
I was trying to do it with java sockets.
The thing that was actually confusing was the use of ip addresses.
I never was able to figure out,
How to assign ip address or how to get ip address of the wifi hotspot host.
So,
I Found a code through which I was able to scan all the connected devices to the hotspot and their respective ip addresses.
So I instantiated it from the host side...
Which I wanted to do from client side...
Calling ServerSocket on the Client
And connected to it from wifi hotspot host using Socket(ipAddr,port)
I still would like to do it as I wanted.
I didn't knew the core concepts of ip addresses and networking it is just the solution that i came up with the help of the internet and little bit of code that found here and there.
Related
So I've been learning Java Sockets for some time now, and all my codes are tested basically with a localhost (my own computer).
I was wondering if say I have another machine in another country, does the simple client-server connection still work? (My codes are peer-to-peer connection).
Is it that simple with just IP address and Port?
Sorry this question seems weird but back in the days when I was playing online games, simply putting "connect 'ip address' " didn't always work.
I was wondering if say I have another machine in another country, does the simple client-server connection still work?
Possibly yes, possibly no.
Is it that simple with just IP address and Port?
Possibly yes, possibly no.
If the IP address is a public IP address, AND there are no firewall issues, then it should work. But that is a BIG IF ......
If the remote IP address you are trying to connect to is not a public IP address, then there is no way that packets can be routed to it. No connection is possible.
If there are firewalls between your machine and the remote IP, they need to let packet for that IP / protocol / port through, otherwise connections will fail.
IMO, you would be better off doing some basic research / reading on how IP-based networking works before you ask questions like this.
(My codes are peer-to-peer connection).
That is at the next level up the networking stack. Peer-to-peer is implemented on top of transport-level protocols like TCP/IP and UDP/IP. If the transport level doesn't work, then application level protocols won't either.
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
For a small java game I made, I would like to be able to play it with two computers in the same (home) network. I think I will use RMI and am now trying with computer on ipaddress 192.168.2.3.
I know I can search for a registry on this ipaddress on my other computer at 192.168.2.6, but I would like to show a list of all ipaddresses in the network my computer is connected with. Prefferably only if they actually host a game.
Now I tried some questions here on stackoverflow:
How to enumerate IP addresses of all enabled NIC cards from Java?
How can I find all locally bound IP addresses in Java?
How to get a list of IP connected in same network (subnet) using Java
Why does InetAddress.isReachable return false, when I can ping the IP address?
,but I don't think I need all my computer network interfaces and InetAddress.isReachable() always seem to result to false (even though I can ping via cmd and I have firewall turned off) and calling a commandline
"ping -n 1 192.168.2.i" for all i, where 0<=i<=255,
always exits normal, so it results always in true.
What is the best way to get a list of ipaddresses of computers in the same network as the computer the JVM runs on?
With the linked answers, you should be able to filter the available interfaces down to a few possible options (i.e. interfaces that are up, no loopback, have an IPv4 address, etc.).
To discover game hosts, you can do something like the following.
Let the game hosts listen for UDP broadcasts on a specific port.
Let the clients send out a UDP broadcast to the broadcast address of each of the remaining interfaces from above. The broadcast address can be determined by getBroadcast() in class InterfaceAddress.
The host replies, to let the client know it is waiting. When using UDP, the hosts IP is in the received DatagramPacket. When using TCP, the hosts IP can be determined from the Socket.
Then the client can use the address of the host to establish a direct connection and/or set up RMI.
Edit: I found this blog post, which includes code that does more or less what I described.
I have searched the web for this with no success.
This is my problem: I am developing an app that reads data from arduino connected with Ethernet shield.
I can connect to it on the internal network (home network) or through the Internet.
The problem is I need to know if the arduino is present on the local network and if not then look for it on the external ip address.
I have tried to use the ping function but it’s not working. Any help would be appreciated.
I presume you know the IP address of the arduino?, have you tried putting your pc\laptop onto the same subnet as the device, e.g. if the arduino has an IP address of 10.254.103.20 you could change the IP of your laptop\pc to 10.254.103.21 and then try pinging it?
Well for one thing, if your router supports NAT loopback (most do, but you may have to enable it) you can always just connect to the external IP address, regardless of whether you're on LAN or WAN.
Cheers,
I have spent some time learning about socket programming in Java and have managed to make a couple of simple apps that utilize sockets. (instant messenger, tic-tac-toe, basic things) For my programs I used a client-server relationship with ServerSocket and Socket classes. So far I have been testing all my games on the same machine, meaning the client and the server both run on the same machine and the socket ip I am using is 127.0.0.1. Now I want to make a LAN game using the same logic. One computer will be the server and another will be the client.
The thing I wanted to ask, and pardon me if this is a stupid question, I am not really educated about networks and whatnot, but under what conditions can I establish a socket connection between two machines. What I mean is, I run my socket server on one computer and I want the socket on another computer to connect using the first computer's ip. Say, for example my ip is "192.1.1.4" I want to be able to connect to that computer. Is it possible to establish a connection like this between just any two computers in the world? I know "lan" stands for "local area network" but I am quite ignorant on it beyond that. Sorry it it is a dumb question and I can clarify it if someone needs me to.
Basically, what criteria must be met on two machines for me to be able to establish a socket connection between them using a Java program?
You can make a TCP/IP connection between:
Two machines in the same LAN (private IP)
Two machines with public IP (internet)
A machine in a LAN and a machine with public IP provided that the connection is openned from the LAN to the public IP
You can't open a direct TCP/IP connection to a machine inside a LAN from outside the LAN, unless the gateway is configured to redirect the connections to a specific port to that machine.
On an internal network you do just what you said, client connects to server using server's ip address or hostname on the given port.
over the internet can be tricky because of firewalls and NAT. For example, your computer's ip address on the home network is probably somewhere along the lines of "192.168.0.xxx" - but if you go to: http://www.whatismyipaddress.com you'll see that your internet facing ip address is completely different. What you'll see is basically your router's IP address on the internet (WAN).
So basically, the server will have to setup port forwarding on their router for your game's port to his computer. Then he will have to provide the clients with his internet facing ip address for connection.
The main criterion for establishing a connection - ignoring a multitude of possible factors such as firewall configuration, etc. - is that the two machines are simply on the same network. You may be aware that an IP address starting with 192.168.. always refers to a computer on a local network, which is the situation you are asking about, so if you have two computers connected on a local network (e.g. via a router), and you know the IP address of each machine, then it really is as simple as that - you connect in the same manner you have been using up until now. In fact, the same applies on the internet - even if you have two machines set up on different sides of the world and you know their IP addresses (again, ignoring potentially more significant firewall issues), the process is exactly the same. This is precisely the reason that the internet has proved so scalable, as the process of locating a machine with a particular IP address is handled by the lower layers of the network stack.
tl;dr: It's the same as what you are doing already, just with the appropriate IP addresses.
you can use a public IP address and you don't have to change anything about your own address, this is handled by your router that connects to the internet. What you do have to consider is your firewall settings. The ports you are using in your socket should be added to your 'allowed' list. Normally "established" traffic is allowed by default. This means that you need to allow your incoming ports # serverside to go to your server.