Java Socket Programming - java

Java Socket Program did not work for WAN
I have written a TCP IP socket program which works fine in my LAN.
One of my friend is at bangalore He ran the server and I ran the
client with the host name of my friend's IP. In this case my
socket program did not work.

You said that your program is attempting to connect to host 192.168.1.107 port 46216.
192 prefix specifies it is a class C address and is private. Making your program connect to that will force it to remain on the local network searching for that node. You will need to find the IP address of your router (you can use http://whatismyip.org/ to find this out). Then go into your router settings and forward port 46216 to 192.168.1.107 (your node), or even better, your MAC address which is not subject to change (in case your router is running DHCP).
on a side note, it isn't good to hardcode IP addresses. Simply use a textfield to avoid having to redistribute the client when your IP is changed, as it is likely you have a dynamic IP from your ISP.

Your friend running the server is most likely behind either a firewall or NAT. Make sure you are using the external IP address and if necessary port forwarding the packets to the correct IP.

The IP address you gave seems to be a local address, rather than a public internet address. If you are looking for 192.x.x.x, you will not make it out to "the internet", but will be confined behind your router.
WhatIsMyIP is a good way of getting a public IP address, and THAT is the address you should use in your connection. Also, be sure to forward any ports that will be used by your program, because otherwise your router will likely filter the traffic and still create an issue.

You could use an implementation of STUNT or other NAT Traversal protocol.

The ip of computer on thih u deployed your server program is not in your reach.
192.x.x.x ip means (class C) it is for local subnet.
You need to have change your ip address of your net-adapter so that your router could route it through internet.

Related

Which ip address should I use to connect 2 computers in my home using datagram sockets in java?

I am trying to connect 2 computer in my home using DatagramSockets (or even Sockets) in java. What exactly should I do? Which IP Address should I use to connect them?
If your machine is using DHCP then it's not upto you to decide which ip address you will use. Your machine will be assigned some dynamic ip address. To see that use ifconfig on Linux box and ipconfig on win machine. Once you have their IP address you can use these to connect your machine. Chances are high that your machine has dynamic ip's.
Assuming you're a windows user:
to get your LAN IP address open a command prompt and type ipconfig.
A bunch of stuff will show up, you are looking for the line that says
IPv4 address.....: 192.168.#.#
It should be noted however, that this is a so "dynamic" ip address, that can be changed whenever you disconnect and reconnect from your router.
I recommend that you either make your ip static (look this up on google, there are lots of tutorials) or use your computers hostname instead. To obtain your hostname you simply type hostname in the commandprompt.
in your code you can get your ip address by doing this in your client code:
String ip = Inet4Address.getByName("<your servers hostname>").getHostAddress();
I hope this helps, although questions like these belong on Super User, as they really don't have much to do with coding.

Static and Dynamic Ip address problems

Let's say we are hosting on a Personal Computer and Server Program is written in Java.
1.If we host server using static IP-address then does that means we can change machine and replace it with other ones, restart machine as many times we want and our Client will still be able to communicate with us after system is back?
2.if we host server using Dynamic IP-address then just by restarting once the machine we have to tell every single client out there that new IP-address is this one?
3.Will Dynamic IP-addresses change even if we don't restart the machine ?
Yes, a static ip address means that it is fixed. There is more to the whole system, of course, since there is resolution of an IP to a MAC address. Nonetheless, a static IP (assuming it is only on the network once) means that any machine with that IP will respond. So a client can always connect to the IP address.
Note: not a very friendly way to go.
Just because something has a "dynamic" IP address does not necessarily mean it changes every single time. Using DHCP it is possible to assign the same IP address to the same MAC address each time. It is a much better approach than hardcoding an IP address to a machine.
In addition, you really should not have your clients use an IP address to connect. They should look up the machine by a name in DNS. You can coordinate the DNS lookup to the DHCP, so machines do not need to do anything but resolve the hostname.
However, as your question stands, if a machine uses DHCP and does not receive the same IP address each time, and you have your clients connecting by IP address, then on each new assignment the clients would need the new IP address.
Whether a machine's IP address updates even when it does not restart is a policy that is controlled by the DHCP server. In general, there is a renewal time for an IP address. Without going into great deal, the client during the DHCP conversation may request the same IP address, but it is up to the server as to whether to hand out the same one or not. Conversely, most servers are configured to hand out the same IP address to a given MAC address as long as the DHCP cache is current. It is completely a policy decision.
At the end of the day, however, it seems like you are attempting to tie clients to a server's IP address, and this is not a good strategy. It is a better strategy to use DNS and have the clients resolve a hostname. If I am incorrectly inferring your intent, I apologize.
NOTE: I have used DHCP as the way to give out dynamic IP addresses. You might use some other strategy, but I think the concept is the same.
1: Yes, if the server has a static IP clients will always be able to count on the server being at that IP address.
2: Most likely, yes. In many networks the DHCP server will give the same IP back to a machine that has rebooted but you certainly should not count on that.
3: Yes - dynamically assigned IP addresses have a "TTL" - a time to live. This may be a long time (weeks or more) but they still could expire and change. Most often the machine will get the same IP back but, again, you should not count on that.

Java Network Connection - No Further Information Error

Alright, I have a java server setup using port 6567 and IP address 0.0.0.0 as to accept any connection. When I attempt to connect over my local network (192.168.1.15) I am able to connect just fine using the server. However when I switch to a non-local IP address (my routers public IP) I am unable to connect to it.
I have the router port forwarded and the proper rules in place on my firewall/etc. Is there any limitations on Java connecting in this fashion? I'm able to connect externally but not internally. Any thoughts on what might be causing this problem?
I'm starting to think it might be a router-specific problem, being that it could be rejecting the connection but I am unable to test that currently.
Turns out it was just the router itself that rejects internal connections using an external IP address. My personal fix was to just add a bit of testing code that automatically changes the IP if on a local machine to 127.0.0.1 rather then the external IP.
Worked flawlessly both on my own PC and having people connect externally once I set that up.
Hmm I'm not sure about it but maybe that will help.
Most probable, Your ServerSocket gets bound to a local IP address (e.g. 0.0.0.0) and ServerSocket binds to the port address there; and wouldn't respond to any requests coming from an IP address. Try new ServerSocket(4444, 50, InetAddress.getByAddress(new byte[] { YOU IP ADDRESS }).
or check again firewall
edit: Tell me how did you tried to connect from other IP than local?

How can I change IP in HttpServletRequest from client side?

server code:
String ip = request.getRemoteAddr()
if(ip='127.0.0.1')
System.out.print("hello");
Now I am accessing that remote site from my machine, so obvious my IP address should be like 192.*.*.*.
How can I cheat the server(IP spoofing) so server always prints "hello" for my request?
New answer to edited question:
You can't in Java. If need to pretend that the request is coming from 127.0.0.1 (the server itself), so you'll need to hack into the network stack of your operating system.
Old answer:
The IP Address your client uses to connect to the server depends on the network interface it uses and the kind of network attached to this network interface.
Example:
If your client is a laptop it most likely has only one network interface. This network interface uses 192.168.1.10 as its IP address (e.g., assigned from the DHCP on your router) as its internal IP address. Your router might also be connected to the internet, with an IP, say 20.20.20.20, which it shares with connected devices via NAT.
If you use this to connect to your server which is on your local network, the client's IP address that the server sees will be 192.168.1.10; if you connect to your server which is not on your local network but somewhere on the internet, your client's IP (that the server sees) will be 20.20.20.20
So you cannot make your client pretend to use 127.0.0.1 (if server and client are running on the same machine, your client will most likely have 127.0.0.1). Of course there are techniques like IP spoofing where you pretend to have a different IP than you actually have, but that's totally different issue.

How do I create a network connection with other computers (JAVA)?

I'm developing a game that can be played with computer or versus other players. The GUI is Swing but irrelevant for the matter. My question is : how do I connect with other players in a network?
I am familiar with terms like client / server, sockets etc, and i can write a basic client/server program, but it can only be run from ONE computer. I don't know how to connect to this network from a computer, say, half way across the country.
You are probably setting behind a router which hides your local IP address from the rest of the world (look for NAT for more info on this). Basically, the world only sees your YOUR_ROUTER_IP, your router takes care of carrying all IP packets from the outside world to you and vice versa. You will need to change your router settings so that your computer/server gets the router ip address. This way you can access your server application from anywhere in the world by using simple socket operations.
I hope this helps.
I like to use Kryonet for network connections, it works very well and has really good documentation.
You indicated you know how to use client and server sockets, so I'll just throw out there that you ought to try connecting with "real" ip address instead of localhost (127.0.0.1). Take two machines on your local network, get the IP address of the "server" machine and use that address to connect from the client.
This will work all the way around the world, except for the fact that you are likely behind some Network Address Translation (NAT) firewalls and will likely need to "open" or "forward" the ports you need. If you need more information on NAT, google and serverfault will be pretty helpful.
If your client and server is located on the same machine, then you can use loopback address(ie 127.0.0.1), in LAN you can do with private ip addresses
Private ip address for LAN
CLASS A - 10.x.x.x
CLASS B - 172.16.0.0 - 172.31.255.255
CLASS C - 192.168.0.0 - 192.168.255.255
you can use this for LAN environment
For connecting someone over the internet, you will need Public ip addresses.
Address apart from the private ip in every range are public address.
Now if you have a server which is having a static ip then it wont be a problem for the client to access it anywhere from the world over internet.
But if its over a LAN , and accessing the internet from a gateway, then there will be a NAT, then you will need to set the inbound and outbound traffic rules at the gateway, for letting the client access the server.

Categories