Connecting to lan network - java

I am writing a platform game, and i thought it would be cool to add a multi-player mode for people who are playing on the same network. My question is how would i query through all the available computers open on a certain port for connecting to play multi-player, and then how would i establish a connection with them. I thought i could just create a socket and just try to connect on every port, but how would i do that if i dont know the other computer ip address. On google i saw this question get asked several times, however none of the answers actually seemed helpful.

You will propably want to broadcast a message (broadcasts are received by all devices on the network). Then you would have the other machines listening fir such incoming broadcasts.
Basically in a broadcast you would advertise that a computer is running the program, and is willing to establish a direct connection. Then one of the computers would connect straight to the other, and you would work on from there.
EDIT: Someones similarily done aproach in java (blog post)

Related

Is there a way to connect two computers with a two-way connection on different networks (one has port forwarding)

I am trying to make a two-way instant messaging app over two different networks. One of these networks is mine, which has port forwarding enabled(sends traffic on certain port to specific ip address). My problem is that I need a two way connection(sockets can only send to serverSockets, serverSockets can't send to sockets). Is there a way to connect to a computer via a pre-existing connection? Is there a library for this? ie. socket.connect(serverSocket.getConnection, 5001); (I have made my own classes which handle all the Input/Output Streams and sockets, I just need a library for a function I can put in the class).
If what you're asking for is to have a computer exposed to the internet to directly connect to a computer behind a NAT, you might get your app working if you are able to implement something similar to reverse ssh tunneling. See here and here, for a java library.
But I would recommend some sort of client-server approach for this, in which everyone connects to the server, and through the server they connect to each other.

How does one connect with another machine with IP address?

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.

Direct TCP/IP networking between two android devices over internet

Hey I am just a beginner at networking.
Is it possible to connect two android devices to each other using sockets over the Internet without using any intermediate dedicated server, Given that there are Nat routers in the wifi and GPRS/3G connections.
I heard that you need to send a outgoing message to other device first , then only NAT would allow that device to reply back to you. But when both device behind NAT how would the first message or packet reach to anyone of them?
EDIT : Observation-> When i query for location of my public IP address I get random places
like other cities and sometimes other states!!! What's the matter? can anyone explain?
The overall design of such a solution involves the use of a third party server. In order for the clients to connect to each other, their public IP (i.e., the NATs IP) has to be known to the other party. When I had to something similar, I implemented STUN, which is part of the ICE technique for establishing peer-to-peer connections. STUN supports both TCP and UDP-traffic. If you want to understand STUN, you should also read about TURN.
Even though STUN requires the user of a third-party for clients to be able to connect directly, there are many public STUN servers available. Here is one of the lists a quick-google search turned up.

How does a server browser e.g. in games work?

I have written a variety of network programmes such as chats and games, but the user always had to enter the ip of the server, which is very unusual.
I want to realize a server browser you may know from common games, which locates servers in the lan automatically. I want to do it in Java.
My questions are:
How does that browsing work? Also WiFi for example, just everything which automatically finds another party.
Would it be possible and also a good approach to broadcast status packages to whole lan all the time? Which time intervals would be useful?
For WiFi, the device listens to a certain frequency range and scans the range for signals. This is much the same process as a TV uses to pick-up stations, or a person might do to find local radio stations.
For an internet game server, you can't scan the entire internet. Normally the services provides a master server which is configured to send the list of servers on a known port. Typically it will also allow servers to update their information as well.
For a local game server, typically it's single packet every few seconds or so. There's actually quite a lot of traffic even on an idle network with various services requesting this sort of data (Apple's discovery protocol, Bonjour is commonly seen, as is the Windows network discovery protocol).
For internet games, there is a central set of servers that host the information needed to a set up a game. In the past, this is typically just the IP address and port of the person hosting the game, as well as any game information, such as the name, map, etc.
These days due to firewall/NAT issues and problems with cheating, most internet games actually send their data through those servers as well. This is expensive to do.
For games on your local network, UDP packets are sent to the broadcast address, which are received by all devices on that subnet. The hosting game sends the packet with information on where to connect, and those joining in receive those packets to know where the game server is. They then connect directly to the game server.
If you are on an IP network and your address is 192.168.1.100 with a subnet mask of 255.255.255.0, then your broadcast IP address is 192.168.1.255. See also: http://en.wikipedia.org/wiki/Broadcast_address#IP_networking

Pinging computers on a network

I am a researcher at University and I'm trying my hand at a bit of amateur java2 programming.
I've been working through the KnockKnockServer/Client Java2 tutorial to send strings from one program to another using Java Sockets.
http://download.oracle.com/javase/tutorial/networking/sockets/clientServer.html
Full source can be downloaded there and it compiles nicely and is a great example of using IP's to get two programs to talk to each other in Java.
This works fine when I use both the server and the client running on the same machine and the machine name as "localhost"
kkSocket = new Socket("localhost", 4444);
And it also works at home when I use it on my home network with a InetAddress:
InetAddress myaddress = InetAddress.getByName("129.11.138.64");
kkSocket = new Socket(myaddress, 4444);
However when I try to do the same on the university machines it doesn't work because they are behind some kind of proxy despite the fact that the two machines are next to each other and plugged into adjacent ethernet ports.
Question:
How to I work out how to get the two machines to speak to each other, when I don't have full control of the network they are on? I can't even work out how to get them to ping each other. I have visited "www.whatismyip.com" to get the external ip's of each machine but that doesn't work, and I've looked at the next work adapters ip and the gateway they are on but still I can't get them to talk to each other.
Any ideas.
Thanks!
It sounds to me like your network is blocking traffic on port 4444. Try a different port, or confirm that port 4444 is allowed.
There are several different parts to this problem:
1) Can the two machines connect to each other?
You can test this from the command line with the "ping" command which is available in both Windows and *nix. Ping tests for the existence and reliability of a connection to the named machine.
2) Could the route be filtered?
Just because you can ping a machine, doesn't mean there is no firewall or proxy between them. You can find the route taken between the two machines with the "tracert" command. The more steps in the route, the more things that might stop your signal
3) Is Java firewalled?
Just because it is installed, does not mean it has open network access. Try fetching a few URLs or similar to see if Java has network connectivity.
4) Is the port blocked?
A port could be blocked in two ways: some other application is using it, or a firewall is limiting listening ports. If the former, Java will not be able to attach a Socket to the port. If the latter, try a different port. There is nothing to stop you running 10000 clients simultaneously and seeing if you can connect to any of them.
5) How do you do this in Java?
Ping can be implemented by InetAddress.isReachable().
6) What should I say to the angry Network Admin guy who has just turned up?
"I'm not port and network scanning! Honest!" Hunting for usable ports is a basic hacking operation. Some workplaces have automatic tools to check for it and will view it as inappropriate.
Even i cannot ping two machines at my work place.
you need to change the network preferences or try the following:
(instructions for IE)
Go to tools-> internet options -> lan settings -> advanced.
Add exceptions to the field: "Do not use proxy server for addresses beginning with:"
enter the networks ip starting numbers, for eg if your network has ip adds starting with 10.--.--.-- write 10.* in the field.
You need to make sure that each machine knows about the other's private IP address on the LAN (see the ipconfig or ifconfig command). It changes (nearly) each time the devices are connected on the LAN, and sometimes even while they are connected on the LAN.
www.whatismyip.com won't help you, because it will only give you the public IP address of the router/NAT which is closest to that server. There is no guarantee it is also the closest one to your devices. Even if it were, this information would be useless, because you (most often) can't predict how the NAT will translate your private IP address into public IP address.
Even if you could 'guess' that information, there is a possibility that the router/NAT might not let you use this translation behind it (i.e., on the LAN).
To summarize it, make sure you use the proper private IP addresses assigned to your devices and make sure that no router or firewall is blocking traffic over the considered port.

Categories

Resources