I am testing on java.net.ServerSocket.
What I want is the following.
When connecting to aaa.com, you get aaa.com,
Getting bbb.com when connecting to bbb.com.
My etc/hosts file configuration is as follows.
127.0.0.1 aaa.com
127.0.0.1 bbb.com
I used the following java source.
ServerSocket server = new ServerSocket(port);
Socket request = server.accept();
request.getInetAddress().getHostName();
And when connecting to aaa.com, aaa.com is returned.
When connecting to bbb.com, aaa.com is returned.
How can I get bbb.com when connected to bbb.com?
This code is not connecting to anything. It is accepting connections from ... something.
So ... I presume that you have some client code (not shown) that is connecting to port port using hostnames "aaa.com" and "bbb.com" respectively. And you want this server side to know which hostname that the client side used.
It is not possible.
The client resolves the hostnames to an IP address and then makes the connection using the IP address (and only the IP address). Since the IP address is the same in both cases, the server side cannot distinguish the two cases.
It follows that if the application level of the server needs to know the hostname that the client used to make the connection, then the application protocol must pass this information from the client to the server. (That is what protocols like HTTP, FTP and so on do.)
Related
I'm trying to connect to server socket in java using my router's public ip,
first, I tried by simply configuring server socket to localhost, like this,
server = new ServerSocket(5000);
It is working on localhost but not working on trying internal ip 192.168.1.6
then, I tried configuring server socket to the internal Ip (saw this solution), code is as follows,
int backlog = 5;
server = new ServerSocket(5000, backlog, InetAddress.getByName("192.168.1.6"));
and it is working as my devices are connected to same network, I can connect to this Ip 192.168.1.6 from a device with Ip 192.168.1.5 on the same network but when I use public ip of my router from client side, connection is getting timed out, I've done port forwarding,
What am I doing wrong here? any help is appreciated, thanks in advance.
EDIT :
I came to about NAT-loopback (saw this solution) as I was trying to connect using public ip while being on same network so I tried different network but still it is not connecting, connection is getting timed out.
Firstly replace your port number by 8080, which is the default port for HTTP when you are not root. Then you have to open the port 8080 on your router to allow the client to connect.
Your server must be connected to the Internet and have a public IP address. Then the client can connect to this public IP address.
I've looked online, and everything I find shows how to make a separate server to connect to the main server if it's behind a nat or firewall.
But in my case the client is behind the NAT, and the server is on the local network.
So it's set up kinda like below:
Client Actual:10.0.0.1 -> Client NAT:100.0.0.2:1111 <--> Server 10.0.0.0:1099
The Java code I use to connect to the server is as below:
String serviceUrl = "service:jmx:rmi:///jndi/rmi://10.0.0.0:1099/jmxrmi";
String[] credentials = new String[] {"username", "password"};
String objectName = "org.apache.activemq:type=Broker,brokerName=test";
JMXServiceURL url = new JMXServiceURL(serviceUrl);
Map<String, String[]> env = new HashMap<String, String[]>();
env.put(JMXConnector.CREDENTIALS, credentials);
JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
conn = jmxc.getMBeanServerConnection();
broker = MBeanServerInvocationHandler.newProxyInstance(conn, new ObjectName(objectName), BrokerViewMBean.class, true);
And the error it throws is:
java.rmi.ConnectException: Connection refused to host: 10.0.0.0; nested exception is:
java.net.ConnectException: Connection timed out: connect
So my question is, how do I make this client behind NAT connection work?
First of all: there is nothing special in with regard with network configuration for ActiveMQ to work. ActiveMQ's protocol is single port, and can be easily routed just like most other TCP/IP protocol.
Therefore, given that the server is properly listening on its TCP port and that a client can successfully connect to it locally, then this problem can be analyzed as if it was any other network-related problem.
Can the client machine ping the server machine? It is difficult from the IP address scheme that you present to properly understand your network, but as it is presented right now, the client machine will simply assume that the server is on the local network and therefore send an ARP request asking for the MAC address of "10.0.0.0" (which will timeout because there is no such machine to answer the request) rather than forward the request to its NAT gateway. If that is indeed the problem you have, then there are three possible solutions: a) modify the network layout (have the client use a different IP scheme), b) setup a static route for the server's IP on the client machine to force its routing through the gateway, or c) add a port redirect on the gateway and have the client connect to the IP address of the gateway instead. Now solution a is not very practical, unless your setup is barely a lab configuration. Solution b is a possibility, but a really bad one. Solution C, that is setting up port redirection on the gateway, is the most common solution to this kind of problem.
Use hostnames on both sides, by setting the same -Djava.rmi.server.hostname=XXX. Be sure that hostname is resolvable on both sides. You can have a look at http://docs.oracle.com/javase/8/docs/technotes/guides/rmi/faq.html#nethostname
I just have a jsp file, print
request.getRemoteIp();
request.getRemotePort();
And I can get the real client ip, but the port is always wrong.
The Server Environment is IBM Websphere and IBM HTTP Server (IHS60)
From the iptrace, I get the packet data..
From the Client to Server, the port is 13944 to 80 (http port is 80). So the client port is 13944.
Then via HTTP, in the jsp, I invoke an API on another server, the port is 48186 to 9082.
Actually,
request.getRemoteIp(); I really get the client ip.
but
request.getRemotePort(); I get the port number is 48185, it seems be IHS port.
How can I get my real client port, please?
Thanks very much.
I have been having trouble about socket usage in java. First of all, let me explain what I want to do with sockets in java. I want to connect my laptop over the Internet via it. My laptop has a server and a client must connect over the internet. Because I have a router to handle my local network and I do not want to lead a port on the router to my laptop, I need to follow the path "internet->router->localNetwork->mylaptop". The problem is I have found a way to use both the internet ip address and the local ip address; However, it throws an exception : "Exception in thread "main" java.net.BindException: Address already in use"
The code I try is :
InetAddress addr = InetAddress.getByName("XXX.XXX.XXX.XXX");
InetAddress local = InetAddress.getByName("YYY.YYY.YYY.YYY");
Socket socket = new Socket(addr, 1111, local, 1111); // The line I have got exception
With leading router port to my laptop, I can run this code for the similar purposes:
Socket socket = new Socket("XXX.XXX.XXX.XXX", 1111);
*Xs stand for internet ip address
*Ys stand for local ip address
*Codes are belongs to client side of code
As far as I understand, you have a router with Internet (WAN) IP XXX.XXX.XXX.XXX with NAT, and you have a laptop with Local (LAN) IP YYY.YYY.YYY.YYY connected to the router, and you assume that
Socket socket = new Socket(InetAddress.getByName("XXX.XXX.XXX.XXX"), 1111, InetAddress.getByName("YYY.YYY.YYY.YYY"), 1111);
Will connect to the laptop. That is not correct.
Documentation of the constructor of Socket class you are using tells:
Creates a socket and connects it to the specified remote host on the specified remote port. The Socket will also bind() to the local address and port supplied.
That is not what you want.
You can't connect to a device behind NAT like this. You have to "lead a port on the router".
You don't need to specify the local address:port of the Socket, and you are doing so incorrectly. Remove the last two parameters.
I have written a client and server using java sockets. The two machines are on the same network. When I connect using the local IP addresses there are no issues. However if I use the public IP address the connection times out and the client throws "SocketException: connection reset", and the server throws "SocketException: connection reset by peer: socket write error". This happens most of the time, but once in a blue moon the connection actually succeeds. I can successfully ping the servers local address and the router from the client machine.
I have gone into my router, assigned the server machine to a permanent IP address, and forwarded all traffic on the relevant port to that IP address. Unfortunately that was the only thing that I thought could have solved the issue and it did not. I have also turned off windows firewall on the server, still no luck.
The client: (ip taken from whatismyip.com)
Socket s = new Socket("xx.xxx.xxx.xx", 27499);
to connect.
The server:
InetAddress ip = InetAddress.getLocalHost();
ServerSocket ss = new ServerSocket(27499, 0, ip);
Socket cs = ss.accept();
Any help would be appreciated. I can provide more code if necessary.
"SocketException: connection reset by peer: socket write error"
This means the server is actually rejecting the connection, could be a firewall issue, the server rejecting the connection, the server actually being down or overloaded.