I have created a client server program on java language with serversocket and socket classes with datainputstream and dataoutputstream for sending and receiving data.
But the problem is that when I run it on LAN(local area network) or localhost it works properly but when I try to connect client with server over WAN (wide area network) it doesn't even connects to server
And to connect it over WAN I have entered ip address of server side program as parameter of socket class's contractor ex: socket s= new socket(ipadd,port); and it doesn't connects to server without any error or something
So please help me out I am stuck on this problem from a long time.
This is my server side program code
This is my client side program code
You are using your mobile phone to provide internet to your server(which is your laptop in your case), so here your mobile is acting as a router, and your mobile has provided local ip addresses to devices that connects to it. So what is happening is that when you enter your laptop's (server) ip address in client application for starting connection and your computer doesn't have any ip address in public(it has an ip address which is assigned by your mobile but it's not publically available and it's a local ip address which generally starts with 192.168........) so client app is unable to detect your server and servers which are connected to routers same is applied in their case.
So solution to this is, you enter your mobile's (which is providing internet to your server) ip address in your client application, and with any port forwarder application you do port forwarding to your server's(which is your laptop) ip address(and it's local ip address assigned by your phone/router to your laptop) and port(which you are using for communication with client), and there are many port forwader applications available on Google play store you can use anyone of them. So steps to start connection with client and server in your case are:
1: install and start any port forwader application in your phone from your mobiles store. 2: unable port forwarding (for every request which comes to your mobile's ip address) to your server's ip address (which is assigned by your mobile to your server). 3: Than enter your mobile's ip address in your client application.
And it will get connected to your 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.
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.
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 absolutely no idea what I am doing wrong. About a month ago, I set up my router to work with a server/socket connection in Java. I just moved back to my apartment, and am using a different router with a different ip in a different area.
Connecting to localhost (as the default) with my ServerSocket, and then connecting to the ip I get from whatsmyip.org as the hostname for the client socket, shouldn't I be able to connect?
The server and client are running on the same computer, and if I switch the hostname of the client to localhost, the connection works perfectly. I have a port forwarding set up to my computer's ip address that the router gets for me (not from whatsmyip.org) to port 1640, which is what I was using back at my old place where it was working. What am I doing wrong here?
EDIT 1: I am using DynDNS.com to set up a hostname URL that links to my computer's IP, which I then have all of the clients connect to. The client program can be on any computer anywhere. Before I switch routers, this was working perfectly. I was using the Dynamic DNS feature of the router using my DynDNS account, which was set up on my old router, and my new one. So basically, I should just use my old router?
Some routers will not route the external IP while you are on the internal network. I had a router which was like this. Try connecting from an external location (have a friend try, connect to a remote server and connect back in, or use a device connected to 3G wireless etc).
But im not sure from your question if you actually want to connect from the outside. If you dont, there is no need to creating the port forward (in fact you are just making your server visible to the world unnecessarily). Use the local address of your machine (192.168.x.x / 10.1.1.* etc depending on your router) from any machine within your LAN.
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