Connecting to localhost from devices outside the network - java

I am currently doing an project at the university where we have to develop a multiplayer roleplay game with an client-server architecture. My part is to develop a Java Server which responds to client requests.
What do i have to do so my teammembers are able to connect to my localhost server?

If you want clients outside your local network to be able to see your server, you'll need to use something like ngrok. It's designed for exactly this. https://ngrok.com/
If your clients will be connecting from inside your network, they will need your machine's ip address. They can type it into their browser window. If you're running your java server on a port other than 80, then they'll need to add that to the url.
// from inside the network
example: http://192.168.1.125:80
example: http://192.168.1.125:3000
example: http://192.168.1.125:5000
// from outside the network
example: http://yourNgrokName.ngrok.com

Usually all the computers (i.e. macOS, linux, windows) have at least a double interface (localhost) which is not reachable outside and an external interface (wifi or ethernet) that is reachable from outside.
On most computer systems, localhost resolves to the IP address
127.0.0.1, which is the most commonly used IPv4 loopback address, and to the IPv6 loopback address ::1. The name localhost is also a
reserved top-level domain name, set aside to avoid confusion with the
definition as a hostname.
If you want that your team members are able to reach your computer, you have to use the address of this second interface.
But in this case, there could be many problems related to your personal firewall, network topology and etc.
The word outside itself is very generic because, outside could mean:
computers in my home network (small size)
computers in my office network (small-medium size), may be spread across more buildings
all the computers on the internet
Different network problems arises when you want that you personal computer is reachable from outside.

Related

Do I need to port forward for communication between two machines on the same network?

I'm planning on writing a relatively simple client-server socket program in Java, where the server will run on one machine and the client on another machine (both wired on the same network).
Link two computers with Socket in Java.
From the answer to the above question, I believe that I will need to port forward (not an issue as I know how), but this does, however, go against my intuition as I am under the impression that port forwarding would only be necessary if I needed to connect to a service/machine/whatever on my network from a remote machine (not on the network).
So my question is, do I need to port forward for communicating between two machines on a local network? If yes, why?
If the machines are in the same network, you don't need to configure port-forwarding. Even if the machines are in different networks, you don't need to configure port-forwarding, assuming those machines have routable IP addresses (and there is no firewall blocking that access).
Port-forwarding is only relevant when communicating to - for example - a home network that has a single IP address, where there are multiple machines on an internal network, and the local router that is visible to the internet needs to perform NAT (Network Address Translation) to map between the local network and the big bad internet. Port-forwarding makes sure that an internet visible port is forwarded to the IP address of a machine on the internal network.
What you do need to take into account is the possibility that local machine firewalls (e.g. Windows Defender) may block access without explicitly whitelisting the application and/or port.

Can we connect to a computer on the same private network using MAC address?

I could connect two computers withing a private network using private IP addresses using socket programming. Is it possible to do the same using MAC addresses?
If it is possible, any example on Java/C/C++ would be appreciated. I couldn't find any helpful posts on the web regarding the same.
UPDATE:
I was curios to know if it's possible to connect to another computer on the same private network just by knowing the MAC address? One logic behind the question could be the IP address assigned to a network device can be different everytime the router is rebooted. So, MAC address sounds more solid option to have for identity in this case.
(Though I am aware MAC address can be changed too.)
MAC addresses are used to identify "physical" entities in a network.
Whereas sockets exist on the IP layer.
MAC addresses denote hardware objects; if you want those objects to communicate, you need to look into the layers above of that. And either you use existing protocols; such as UDP/TCP; or you would have to define your complete own protocol.
In other words: the one and only function of a MAC address is to identify a unique hardware component within the "scope" of the visible network. The MAC address doesn't define any service/protocol that could be used to connect one MAC address endpoint to another one.
So, in short the answer is: no it is not passable.
Yes. IP is one possible layer 2 protocol, but you can run your own protocol on top of ethernet. FCoE, for example, bypasses TCP-IP and runs its own protocol. Since Fibre Channel can support network traffic as well as storage traffic you could theoretically connect up your network using FCoE. With a little programming effort you could add a socket layer.

Not able to access deployed web application other than on local computer

I am trying to deploy a web application with a url say
http://10.x.x.x:8080/test using apache tomcat. This works from local computer.
The problem is the address 10.x.x.x is my ipv4 address and when i try to hit this from other computers its not accessible.
Using noip / what is my ipaddress.com I also see that the public ip is different other than ipv4 address.
Even if i try http://1.x.x.x:8080/test it dosent work. Moreover i found that when i do a ping 1.x.x.x from command prompt it times out without success.
I am using an wifi dongle.
10.x.x.x is an IPv4 private IP address. It is not addressable outside of your "local network". The chances are that your computer doesn't have a public IP address at all.
Your computer is probably talking to the outside world via a NAT server. By default, that only works for out-going connections. Depending on how your networking is set up, it might be possible to configure the NAT server to map a port on its public IP address through to port 8080 on your computer.
But this is not a programming issue. The question doesn't belong here.

Java TCP/IP Socket internet connection problems

Im using TCP/IP sockets in java to try and create a client-server application. The program works fine when run locally and also over the local area network, but when I use the internet IP address the clients connection is refused.
I used this website to get my IP address and have added a firewall entry to unblock the port im using (port 4445).
I am almost certain the problem lies in some sort of security measure that is blocking the port. Does it matter that I'm running the client and server on the same PC but using the IP address from the previously mentioned website?
If I could get a list of ways to test the port is in fact open, or a list of things to try in order to get my program running, that would be great!
That website may very likely give you the IP address of the gateway through which your PC is connecting to the internet, and if the gateway is out of your control (which is most of the cases as far as I know) there's nothing you can do to use that IP address to test your program. Here's some advice:
Try http://aws.amazon.com, once registered you have one-year free access to a micro-server (which can be accessed publicly through DNS/Elastic IP.)
If your PC have a public IP address, you don't need that website to find out what it is. Just check your network adapter control panel.
Where is the server has been located? If your server is located in some commercial hosting, there is possibility that the ports you use are blocked. Also if you use modem with router or just router in your local network you should check nat table.

LAN Game in Java

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.

Categories

Resources