Java - async socket channel cannot connect to remote async server socket channel - java

I created and debugged a simple net application that uses AsynchronousSocketChannel and AsynchronousServerSocketChannel and they work fine when accessed via localhost, but even if I move the server application to another device it refuses to connect at all. I first noticed this when I tried to port-forward the application, but upon moving it onto a device on the same network and connecting via the IPv4 it still didn't work. I checked both devices and they both are allowing Java through the firewall and there is nothing blocking the port number I am using (I have also tested various other ports). The client can't even connect to the server if it's on the same machine and you use that machine's IPv4. It literally only works on localhost. Has anyone else ever had this issue?
This is how the server channel is opened:
serverSocket = AsynchronousServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress("127.0.0.1", port));

Fixed it by removing the IP part of the InetSocketAddress:
serverSocket = AsynchronousServerSocketChannel.open();
serverSocket.bind(new InetSocketAddress(port));
Replacing 127.0.0.1 with the IPv4 of the machine has the same effect. If someone could explain this that would be great, because every example I've seen online has binded to localhost. Are AsynchronousServerSocketChannels not supposed to be accessed remotely or something?

Related

Open socket server to clients outside of localhost

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.

ServerSocket.accept() on host does not accept connection originating in guest VM

In my Java app, I have the following code:
_serverSocket = new ServerSocket();
_serverSocket.setReuseAddress(true);
_serverSocket.bind(new InetSocketAddress(port));
// _serverSocket = ServerSocket[addr=0.0.0.0/0.0.0.0,localport=33202]
_socket = _serverSocket.accept();
...
In a VM, I launch a (black box) client application that tries to connect to the above server. For an unknown reason, no connection is established and accept never returns.
I know the client app is trying to connect, as I see appropriate packets in Wireshark:
I can't launch the same client application on my host machine but I can successfully establish a TCP connection from the host with nc, so I know the server is actually listening.
The VM and host are in the 10.11.1.0 subnet. The host has IP 10.11.1.1, the VM 10.11.1.68. They can ping each other.
But still, for some reason, the TCP packets do not seem to arrive at my Java code. Any ideas why or how I could further debug this?
I could solve my problem. It was indeed a problem with the host's Windows Firewall settings. After adding an inbound rule for javaw.exe of the specific JDK I was using to run the server, the connection could be established successfully.
Shout out to Corporate IT that didn't just let me disable the firewall quickly to determine if that was the cause of the problem. :\

Connecting a client pc to a server pc via an ethernet cable? (java)

Currently the setup I have is the client code is on my laptop, which is hooked up to an embedded box PC which will run the server code.
I've tested the client/server interaction running both the client code and server code on my laptop, and can confirm they run together. However I'm stuck as how to proceed and adapt my code from what it is now. If I understand correctly, the Server sets up an IP address and the client looks for it and connects to it?
Currently this the code I have converted to on the sever side (The commented out section is what was in place when I was running it on my laptop)
InetAddress ip = InetAddress.getByName(ipAddress);
server = new ServerSocket(SOCKET_PORT, 1, ip); //SOCKET_PORT = 8010
//server = new ServerSocket(SOCKET_PORT); //SOCKET_PORT = 8010
Would this be considered to be the right way to go about doing things on the server side?
As for the client I'm still quite confused.. so far this is the code I had implemented for when I had the code ruinning on the same device...
(I have a loop that spins off clients into separate threads when they connect to the server)
//Socket clientSocket = new Socket("localhost", port);
And then here is the code I think it will convert to, but I may be wrong here
Socket clientSocket = new Socket(ipAddress, port);
I appreciate any help, even if it's just helping to describe the general process of an IP connection between server/client on different devices
I would think you just have to connect the devices, look up the IPs (probably using the command line) and replace the IP addresses with those IPs.

java tcp connection with public i.p

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.

Java TCP socket: java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine

I am creating socket using socket = new Socket(host, port, InetAddress.getLocalHost(), clientPort);. I want the socket to listen to particular port at client side. But when I use InetAddress.getLocalHost() I get java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine.
But when I use InetAddress.getByName("localhost") it works fine. But I require IP address of the machine in server side. So when I use socket.getInetAddress() I want ipadress and not 127.0.0.1.
Can anyone please help.
I am using eclipse. Can this be a firewall issue?
Thanks
You're using the four-argument form of the Socket constructor (really unusual; it's normal to only use the two argument form and let the OS figure out the local address side for itself) so you need to make sure that the two addresses associated with the socket are compatible, i.e., that it is possible to route packets that way. In particular, if either end is localhost, the other end must be too because that address is only ever routed over the loopback network interface.
The simplest fix for you (on the client side) is going to be to switch to using the two-argument constructor, leaving the OS to figure out the rest of it for you; it does a good job. (If the server depends on the client connection coming from a specific port number, that's awful and terribly terribly fragile.)
Sounds like confusion over client-side and server-side responsibilities - sounds like you're trying to get two Java applications talking to each other on the same host via TCP/IP, using Java Sockets.
If this is the case you first need to use a ServerSocket in the 'server' application to create a listening socket on all interfaces:
serverSocket = new ServerSocket(4444);
You should then be able to connect to the ServerSocket from the 'client' application by using a Socket to connect to the localhost:
clientSocket = new Socket("localhost", 4444);
The following page(s) looks like they cover this in detail:
All About Sockets
Hope that helps.

Categories

Resources