So I've been working on a small application that would allow me to create a server, have the server connect/bind to a hamachi network/port, and allow the client to join the hamachi server, then use the client application to communicate and other such things over the server.
There is no good documentation that I've been able to find in the last 4 hours that specifies how to do this. I have a basic Server application that works fine on the "localhost", but I really want to make it work over hamachi.
code example:
InetAddress addr = InetAddress.getByName("**.***.***.***");
ServerSocket sSocket = new ServerSocket(****, 5, addr);
The error received is:
Error: java.net.BindException: Cannot assign requested address: JVM_Bind
I am using netbeans, and yes, I am 100% positive that the port is NOT in use. Please help!
Just specify null as the third argument to the constructor. That means 'listen at all local IP addresses'.
I suspect the IP address you're trying to bind to isn't local. You can't do that.
Related
I have BrowserMobProxy realization in the project. This logic uses the IP address for Proxy connection and test UI web-service (Proxy used for request/response statistic saving). All worked fine before, but we restart docker and the IP address for the proxy was changed. Now I need to found a new IP address for the proxy.
Code where IP address used
public static void startProxyServer(String address) {//address = "172.17.0.2"
if (browserMobProxy.isStarted()) {
browserMobProxy.stop();
}
try {
browserMobProxy.start(9090, Inet4Address.getByName(address)); // {1}
useExclusivePort = browserMobProxy.getPort();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
After docker was restarting the project began fails on the line {1}.
I started the search for new IP on the server. Unfortunately, I can't use ifconfig command from the docker image since this command does not install there. So I determined the address from the server in the following way:
After I use IP 172.17.0.2 the code works well and I hoped I resolved this issue, but as it turned out I lost connection with this proxy - on the UI I got the following result:
So I totally confused:
All works well before the docker image was restarted
Old IP looks like 172.19.0.5 but the code fails with it now.
I "found" new IP 172.17.0.2 and code work with it.
I not sure I determined correct IP since all old IP was started with 172.19.0.{4,5,8}
I have no connection with the new IP
I do not know or I found the correct IP and why it suitable for code but not suitable for connection
The project deploys with Jenkins docker image. Browsers start on the selenium grid
#Question:
How do I need to found the correct IP that I can use for a proxy connection?
there are other commands with which you can find which ip your server is running on. check if you have access to any of the following
first you can try
netstat -an
which will give you all IPs your server is listening to along with what ports, and what IPs are connected on it along with the ports
if this doesn't work try this
https://dev.to/trexinc/netstat-without-netstat-inside-containers-9ak
then try
ip addr
which will give you a similar output as ifconfig
and last you can try with
docker inspect -f '{{ .NetworkSettings.IPAddress }}' containerID
which will give you the network interfaces of your docker
I'n using Kotlin coroutines to setup a Java serversocket in Android studio 4.0 Beta 5. I'm running in the emulator on Windows 10. When my very reliable c language socket client attempts to connect using 127.0.0.1 as the IP it receives error 10061. The same client program has worked well for many years with a Java Swing socketserver.
Google give the following explanation for error 10061:
10061 is a Connection Refused error sent to you by the server. You could not make a
connection because the target machine actively refused it. The most common cause is a
misconfigured server, full server, or incorrect Port specified by the client.
Here's my code snippet
int myPort = 8080;
String localIP = InetAddress.getLocalHost().getHostAddress();
ServerSocket srv = new ServerSocket();
String hostname = InetAddress.getLocalHost().getHostName();
srv.bind(new InetSocketAddress(hostname,myPort));
srv.setSoTimeout(socketAcceptTimeOut); //This sets the timeout on the accept
Socket cli = srv.accept();
I used the server bind based on another stackoverflow answer but it did't help when I removed it. In any case I believe the serversocket is listening at 127.0.0.1. I'm using port 8080 but I've tried a few others.
On the Android side the srv.accept is just timing out.
What am I missing?
Thanks
I'm having trouble with my java socket programm.
I am opening a ServerSocket on host A.
On host B, which is in the same private network, I
try to bind to the server, but I always get a timeout.
Host A | Server | IP: 192.168.56.1
Host B | Client | IP: 192.168.47.1
On the server host A::
welcomeSocket = new ServerSocket(2323);
socket = welcomeSocket.accept();
After opening that serverSocket; on host B:
socket = new Socket("192.168.56.1", 2323);
Then after a few seconds, there is the Exception:
java.net.ConnectException: Connection timed out: connect
The socket does work well when I start the server on either host A or B
and enter "localhost".
What's wrong here? Thanks for your help.
If your subnet mask is 255.255.255.0 (which is the norm for class C private subnets) then your two machines are on different subnets:
Host A | Server | IP: 192.168.56.1
Host B | Client | IP: 192.168.47.1
the .56. and .47. being the operative elements. Try to change either IP to be either in 192.168.56.0/24 or 192.168.47.0/24 and you'll be fine.
Alternatively, you'll need to setup a static route between the two subnets.
Cheers,
Javadoc for setSoTimeout(int) says this:
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
With this option set to a non-zero timeout, a call to accept() for
this ServerSocket will block for only this amount of time. If the
timeout expires, a java.net.SocketTimeoutException is raised, though
the ServerSocket is still valid. The option must be enabled prior to
entering the blocking operation to have effect. The timeout must be >
0. A timeout of zero is interpreted as an infinite timeout.
Try this:
welcomeSocket.setSoTimeout(0);
Add this statement before you block the server for accepting request from the client.
Note: Make sure that both the machines are accessible to each other to ensure proper connection.
This usually means incoming port 2323 is not open on host A.
So host B cannot connect to port 2323 on host A.
In a LAN not all ports are open. You cannot connect
to any machine (from your LAN) on any port you want.
Try this from host B:
telnet 92.168.56.1 2323
If you cannot connect this way, then your client java program
will not connect too, and that's normal.
You should then contact your network administrator.
If you can connect this way, only then you should look at
fixing your java programs in some way.
Thank you for helping me!
My program is working fine now.
Thanks to Anders' hint with the different subnet mask, I recognized that
the IP is wrong. Looking in the Windows control panel revealed me a different IP adress
for host A. Actually they are in the same subnet mask.
I was rather rather expecting the adress given by
InetAddress.getLocalHost().getHostAddress()
as my adress.
So how can I show my "real" IP adress in a java program?
Add an Entry in HOST file if IP is reachble
Imagine the following code:
String hostName = "0.0.0.0";
int port = 10002;
int timeout = 5000;
Socket socket = new Socket();
socket.connect(new InetSocketAddress(hostName, port), timeout);
On the Mac it works fine and executes the connect (even with nothing running on port 10002) and on Windows I get the following exception:
java.net.SocketException: Permission denied: connect
What's the difference here and what would be the alternative on Windows? This is used in unit tests.
Regards
Jonas
Just in case somebody else stumbles upon this question, I am answering it.
Unfortunately, connecting to the any address is not allowed on Windows.
The Winsock function connect will return the error code WSAEADDRNOTAVAIL
[The remote address is not a valid address (such as INADDR_ANY or in6addr_any)],
as stated at the Windows API Documentation:
If the address member of the structure specified by the name parameter is filled with zeros, connect will return the error WSAEADDRNOTAVAIL.
So without using any localhost address, I think what you are trying to do will not be possible on Windows (Though I wonder if the Unix behavior is a bug or intentional.).
I would suggest setting up more loopback interfaces, as Mark Reed suggested in his comment.
I'm trying to implement Sun's example Socket program, i.e. the KnockKnock server and client found here: http://download.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
So I build the 3 files (EchoClient, KnockKnockServer, KnockKnockProtocol) into a project, build them, then go to cmd to run them:
> java KnockKnockServer
> Could not listen on port: 4444.
Also, I have trouble with the EchoClient (not that it means much since the server doesn't work). I get the following:
> java EchoClient
> Couldn't get I/O for the connection to: localhost
The one thing I changed in EchoClient class was to try and connect to "localhost" instead of their example machine "taranis". I don't understand the I/O error at all though.
So I need to figure this stuff out so I can later adapt it. Here's what I'm wondering: how do I know what port listen for in the KK Server? And if I want to connect to another computer in the EchoClient, would I directly put their (IPv4) IP address instead of "localhost"?
Thank you for any help
Try a different (higher port) because 4444 might already be in use on your machine:
Technical description for port 4444:
The port 4444 is specifically assigned to the Kerberos 5 authentication features particularly the implementation of Kerberos 4 in various systems including those running under the Mac OS X platform. The communication port 4444 is used in the conversion of Kerberos 5 credentials into an acceptable Kerberos 4 format.
source
That tutorial breaks rule #2 about handling exceptions: it makes up its own error message ' Couldn't get I/O for the connection to: ...' instead of printing the actual exception. Change it to do that, then you have some hope of finding out what went wrong.
I complained about that tutorial about eight years ago ;-(
(Rule #1 is print something.)
I had this problem yesterday when I was trying to learn the same thing you are!
1) Make sure both the server and client have the same port for example:
kkSocket = new Socket("localhost", 802); //Client
serverSocket = new ServerSocket(802); //Server
(I ran into this problem by accident)
2) Try changing both the server's port and the clients' port to 10000 or higher
3)The program outputs "Knock! Knock!" and than you need to type the input.(The hang you described might just be the server waiting for an input)
try this:
change taranishost name to localhost
kkSocket = new Socket("localhost", 4444);