TLDR: Allow people not on localhost connect to my chat server if they have the server IP.
I am starting to learn about socket programming in Java and I have come across an issue that I can't seem to figure out. I have created a basic chat program with a server and a client using sockets. This works perfectly in localhost, but I want to share it with my friends. I have searched for hours and can't really find a solution. From what I've read I need to bind my ServerSocket with my IP or Port forward. Just looking for some clarification and possible steps I could take.
How I'm starting the server
ServerSocket server = new ServerSocket(5056);
How I'm connecting to the server
Scanner readIp = new Scanner(System.in);
String ip = readIp.nextLine();
socket = new Socket(InetAddress.getByName(ip), Integer.parseInt("5056"));
Posts I've looked at
Java Socket port forwarding
https://coderanch.com/t/571967/java/connect-machine-localhost
What does it mean to bind() a socket to any address other than localhost?
To 'Allow people not on localhost connect to my chat server if they have the server IP.' and outside of your LAN:
public 'white' external IP.
So for example this page https://whatismyipaddress.com/ should display exactly same IP address as displayed in your OS
non blocked incoming connections on ISP level: your ISP should allow incoming connections to your computer from internet.
some opened port, not blocked by local firewall or by ISP.
Typically all ports below 1024 are blocked for input on ISP level.
No presence of NAT ( exclusions are present but outside of this topic ), thats common for WiFi/3/4/5g home routers.
As you may already understood - thats complicated.
So faster will be to use an external service,like https://portmap.io/ :
it does VPN connection + port forwarding, so knocking on their server from internet will actually call your local chat server.
Related
Im making a client/server app just for learning purposes, and i want my Client to connect the server (which is running in my pc) from another pc.
Right now im trying to use the server's pc public ip (which I got with whatsmyip.org) in the Client socket: Socket sock = new Socket(HOST, PORT); where host is the public ip of the server's pc.
When I was testing it with localhost and the client running in the same pc, the connection worked.
I already oppened the port im using in the Server pc and added firewall rules, including router's firewall. So any of my firewalls should be blocking the connection to the port im using.
I read in another question this (it was an example): "Your Server PC is in New York with an IP 192.168.1.121, that is behind a router with internal IP 192.168.1.1 and public IP 40.20.26.63. You will need to make sure that you go into the router and forward port 8084 (TCP) to internal address 192.168.1.121 (the internal Server PC)." How can I do that? I know the public IP and the internal IP of the server, but I dont know what IP I need to type in the Client Socket.
Thanks in advance.
I have to build a server/client chat room in java as a school project, and I want to know if I can connect to that server from the world network (not local network) using the IP address and ports (I wanted to host it but I realized too late that I should have built a web app not a desktop one). My app is using Transmission Content Protocol(TCP) sockets. I have tried to connect to the app by using the public IP and the port that I have opened in firewall. I can post the code if needed. Thanks in advance
If your networks firewall settings let you do it, of course you can connect.
Now, your computer has an internal ip address, which is like 192.168.xxx.xxx and your router has an external ip address which is unique.
Set up your router to forward connections. Steps to do it may change for each router but the point is to forward all connections coming to this router with a port number you have declared, to a internal ip address and a port number.
In your client side, your connection statement will have your external ip adress and the port number you have written in your router settings. In your server side, it will listen your internal ip address and the port number your router forwards to.
To be clear;
Client ---"xx.xx.xx.xx:9999"---> Router ---"192.168.xx.xx:8888"---> Server
As you see above, the router forwards all the connections coming to 9999. port, to 192.168.xx.xx address and 8888. port.
I have fully developed a chat room for multiple clients with multi-threaded server which does the job, however only on my local machine. I want to go beyond this, and make this chat room to be working over the internet. So far I have made the port forwarding on my router for TCP protocol to route to my local IP address, however this didn't solve the problem and I still can't connect the client, even on my own local network. What other steps should I follow to get my chat room working on my own local network and then the internet?
try disable (windows) firewall ?
So my friend the basic rule for make anything to work over internet is to do Port forwarding or in simple way you can say that to open your server for the public network. For that you need to make sure that the routing path is complete from internet machines to your desktop. For this to work you need to open the port for which you need to access your machine from firewall settings, and also ensuring that trafic is routed from your public IP address to the server's IP as your server will be private under some router or ISP.
Way to do that:
You need to configure your home network i.e router setting. So in your router, configure the port you want the communication to happen(say port 5443).
In your router, configure a port-forward for the port 5443 to the internal IP address of your actual server, also to port 5443.
Reference: https://www.noip.com/support/knowledgebase/general-port-forwarding-guide/
On your server(your Desktop Machine) ensure that your firewall settings for port 5443 is on and set to allow rather than block.
I am trying to create a java chat application for my networking class. As of right now I am stuck trying to connect to someone behind a different router. The way I have my project right now is I have a client program and a server program. The client programs first logs into the server program which logs their IP and port in a database and then the server gives them back the list of their friends with their IPs and ports. Then the client closes down the connection to the server and tries to connect to another client using the information the server sent back. So far my program only works connecting to the server and getting the friends IP and port but when I use those values to connect to the other client I cant connect.
socket = new Socket();
socket.setReuseAddress(true);
socket.setKeepAlive(true);
socket.setSoLinger(true, 10);
socket.bind(new InetSocketAddress(Port));
socket.connect(new InetSocketAddress(host, SERVER_PORT));
reusePort = socket.getLocalPort();
Above is a snippet of java code used to connect to the server then below is what i do on the client side.
ss = new ServerSocket(reusePort);
So now technically I am listening on the same port I used to connect to the server with which is logged in and is retrievable to another client and is in the NAT table with my ip and port. I am not sure what I am missing or if there is some protocol or something that I have to do. I have looked at TCP and UDP hole punching but I am not sure how that is actually accomplished or how to implement it.
Any suggestions would be appreciated.
If you want to send a message you'll need to set up port forwarding on any device that acts as a server (any device which creates a socket server). Port forwarding is done on the Router. The reason you cannot connect to the other client is because they are hidden behind their routers firewall. Their address to the rest of the world is actually the address of the router, not of their physical computer. On their local network they have a different address then what the rest of the world sees, and the router figures out what messages from the outside world need to be sent to the client based on an address translation table.
Given your architecture, this would mean that all clients need to have their routers doing port forwarding, which is of course unfeasible (imagine gtalk or aim requiring users to do port forwarding).
The more common architecture is to have the Server do the work of rebroadcasting messages to the connected clients and maintain tables to lookup whose talking with who. This way there is a single server which will need a static ip (or be port forwarded), and all users are simply clients which connect to the server socket and read messages from it.
For actual code describing the second architecture please see http://pirate.shu.edu/~wachsmut/Teaching/CSAS2214/Virtual/Lectures/chat-client-server.html. Then the machine which is running the server code either needs a static ip or if it is behind a router needs traffic from the port it is listening on to be forwarded.
So on the server code you will bind to the ip assigned from your router (something like 192.168.1.2 at some port say 5000). Then go to your routers configuration page (it may be 192.168.1.1 see http://www.wikihow.com/Port-Forward/Open-Ports-on-a-Linksys-Router), and forward port 5000 to the address 192.168.1.2.
The Interactive Connectivity Establishment (ICE) protocol combines various NAT traversal utilities such as the STUN and TURN protocols in order to offer a powerful mechanism that allows Offer/Answer based protocols such as SIP and XMPP to traverse NATs.
This project provides a Java implementation of the ICE protocol that would be usable by both SIP and XMPP applications. The project also provides features such as socket sharing and support for Pseudo TCP.
ice4j is maintained by the Jitsi community.
ice4j
I am trying to tcp connect to a server machine in java, by using its public i.p. but when i run the client application i constantly getting a connection refused error. if i used localhost instead of the public ip, it works perfectly.
i search the internet for several causes of the issue but i couldnt fix it.
i forwarded the port to my machines' local i.p address(192.168.1.3) in routers settings. then i checked if port is listening when i ran the server application using netstat -an. i saw lines like,
0.0.0.0:19999 or []:19999 .
19999 is the port number i am trying to listen to. Then i changed my ServerSocket constructor to the 3 parameter one, which also binds the local address.
InetAddress miad = InetAddress.getByAddress(addr);
ServerSocket socket1 = new ServerSocket(port,10,miad);
addr is the InetAddress of my machines local i.p. After these modifications, when i start the server application, i run netstat and it shows:
TCP 192.168.1.3:19999 0.0.0.0 LISTENING
Here i think that my server is listening on the port specified properly.
I have also disabled my firewall and antivirus software.
I have seen several threads and discussions on the net about the issue, and tried most of the things mentioned there, but i keep getting the connection refused error.
What can i be doing wrong? It runs without any errors when i test with localhost.
This is because of the router (not very sure, but almost). Try to see if a webservice like www.canyouseeme.org can connect to your server.
The main idea is that an internal machine (inside the LAN) cannot connect to a machine inside the same LAN by using the external (public) IP address.
I'm pretty sure that it will work, using you internal ip (192.168.1.3).
And if you are sure that you forwarded ports correctly, CanYouSeeMe will say your server is reachable. If it doesn't, make sure you ISP isn't blocking the ports for some kind of "safety reasons".
To figure out if your problem relates to Java and programming please do
telnet 192.168.1.3 19999
If it can't connect then superuser.com would be a better place to discuss this issue.