not able to connect to server on internet - java

I have a very simple client server code written java(server listens on some port and client connects to server port and after connection is established, client ip is displayed on server console). This program is working very well in intranet, but if client and server are on internet, my server cannot detect it.I have no firewall installed on my client and server and port forwarding is done on server(I can see it from canyouseeme.org).
Server is directly connected to modem along with other three computers(they are also connected to modem directly)
Please help me figure out why I am not able to detect client on internet.Thanks in advance.
Client code:
------------
String remoteIP = //remote ip
int port =1888;
try{
new Socket(remoteIp,port);
}catch(Exception e){
System.out.println(e.message());
}
Server code
-----------
ServerSocket serversocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
displayIp(socket);

Check your proxy configurations on the client side, see http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
Which protocol do you use?

Related

Java : How to connect to ServerSocket using router public IP?

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.

Using my Python TCP Server to Communicate with my Android App

I'm trying to make an app that is being monitored by my Python server; the server can send requests to the app, and then the app would send a message back to the server saying if the request was accepted or denied. I do have to use a Python server and I would like my app to be in Kotlin. For example, the app will inform the server that bluetooth is on, and the server would send a request to turn it off, then the app would send a message back telling the server if the user said yes or no to the request.
I have code for server and client using TCP sockets that I made last year. In this case, the client would be the Android app.
Here is a very small code sample to show where I'm going:
#SERVER
import socket
HOST = '0.0.0.0'
PORT = 10000
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print("main socket is listening...")
connection, address = s.accept()
with connection:
print('Connected by', address,'\n')
connection.sendall(b'Thank you for connecting!')
#CLIENT
import socket
HOST = '' # The server's hostname or IP address
PORT = 1000 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
# print thank you message
print(s.recv(1024).decode(encoding='utf-8'))
With that being said, would a be able to work with my Python TCP server, or should I go another direction that uses a Python back-end?

Java application can connect to a specific ip and port but port isn't open in the firewall

The following java code listens for incoming connections on a specific port. I see that clients can connect but how is that possible if the server port is not open in the firewall?
ServerSocket serverSocket = new ServerSocket();
InetSocketAddress addr = new InetSocketAddress("localhost", 5555);
serverSocket.bind(addr);
Thank you.
I see that clients can connect
Only if those clients are in the same localhost, because your server port is bound to 127.0.0.1:5555, not 0.0.0.0:5555.
but how is that possible if the server port is not open in the firewall?
Because the firewall doesn't have anything to do with communications within the localhost.

Java sockets over internet

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.

how tunnel all RMI traffic over SSH

I'm working on tunnelling the cajo rmi traffic through a SSH tunnel.
For that I have a server running an SSH deamon (apache Mina) and a client running an SSH client (Trilead SSH).
The shh connection between these machines can be established and by applying local and remote port forwarding I can tunnel rmi traffic, however this works only in the outging (to server) direction.
The setup:
Active SSH connection (port 22)
client: forwarding local port 4000, to remote host port 1198 (this traffic actually goes trhough the tunnel)
server: forwarding server port 4000, to client port 1198 (this part of the tunnel is not being used by cajo)
The server exports an object using:
Remote.config(null, 1198, null, 0);
ItemServer.bind(new SomeObject(), "someobject");
The client does an object lookup using:
ObjectType someObject = (ObjectType)TransparentItemProxy.getItem(
"//localhost:4000/someobject",
new Class[] { ObjectType.class });
logger.info(someObject.getName());
Port forwarding is invoked using the trilead ssh library on the client side:
conn.createLocalPortForwarder(4000, "Server-IP", 1198);
conn.requestRemotePortForwarding("localhost" 4000, "Client-IP", 1198);
When analysing the ip traffic between the two machines with WireShark, I see that the lookup is being redirected throug the tunnel, but the response is not.
The respons is ordinarily send to port 1198 of the client.
How can I force the server to send the response of a remote invocation to a local port, in order to get it tunneled back to the client?
UPDATE: The problem here was that the ports for RMI objects are different then the registry port and therefore also need to be forwarded.
In short, client 10.0.0.1 makes lookup to //10.0.0.1:4000 which is forwarded to the RMI port on the server (through the tunnel).
Subsequently the server responds to 10.0.0.1:1198 where I would like the server to send its traffic to its local port 4000 instead, in order to use the tunnel.
I have tried to fiddle with the cajo Remote.config(ServerAddress, ServerPort, ClientAddress, ClientPort) settings, however when I set the clientaddress to 10.0.0.1 or 127.0.0.1 for this method, I'm unable to get response back and I don't see any responding traffic at all...
I did find a solution to this problem, in which I omitted the cajo framework from the setup and use pure java rmi. This makes things more transparent.
On both client and server I placed a security policy file: C:\server.policy
grant {
permission java.security.AllPermission;
};
Then on the server, set security permissions and start registry on desired port:
System.setProperty("java.rmi.server.hostname", "127.0.0.1");
System.setProperty("java.security.policy","C:\\server.policy");
System.setSecurityManager(new RMISecurityManager());
new SocketPermission("*:1024-", "accept,connect,listen");
createRMIRegistry(Property.getProperty("rmi.registry.port"));
Notice the hostname 127.0.0.1, this makes sure we are always pointing to localost,
this tricks the client in thinking the object got from the remote registry is local and then connects to its local forwarded ports.
On the client I give the same permissions as above, I don't start the register, but bind an extra socket factory to use for the registry lookup.
RMISocketFactory.setSocketFactory(new LocalHostSocketFactory());
This socket factory creates a SSHClientSocket to the localhost ssh port (to the remote registry).
The remote objects are exported with a custom ClientSocketFactory, which is therefore implemented on the clientside. (On the serverside it needs to be disabled, otherwise you will ssh to your own machine :$)
It then creates a ssh socket and port forwarder on the fly.
public class SSHClientSocketFactory implements RMIClientSocketFactory, Serializable {
public Socket createSocket(String host, int port) throws IOException {
try {
Connection conn = new Connection(hostname, 22);
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(username, password);
LocalPortForwarder lpf1 = conn.createLocalPortForwarder(port, serverAddress, port);
return new Socket(host, port);
catch (Exception e) {System.out.println("Unable to connect")};
}
}
This automatic port formwarding ensures that whatever port is being used to bind an RMI object, it goes through the SSH tunnel and points to localhost for that.
Remote port forwarding is not needed for this setup.

Categories