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.
Related
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
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.
I can SSH and PING a machine running Fedora 18, but whenever I try connect to it using Sockets, I get NoRouteToHostException (I tried Java and C++, and I get the same error).
Any ideas? I have looked around but could not find a solution that works.
If you're not trying to connect to the same port that ssh(1) uses (port 22) then the problem could be that a firewall exists between you and the destination and it doesn't allow your packets through. You can test this by having your code try to connect to port 22 on the destination host. If you don't get the same exception, then this is the likely cause.
You should also verify that your code is OK by having it try to connect to the local host (i.e., the computer on which it's running). You should not get the no-route-to-host exception (unless the firewall on the local host is doing something odd)
I'm pretty sure that my root problem is the antivirus app managed by our network, but my a basic socket client I wrote in Python that worked up until a short time ago is now giving me "socket error 10013". I also have the SocketTest app in JAVA. SocketTest works, but I have been unsuccessful in getting Python to connect.
The WIN 7 PC has two network cards, not bridged. I have switched the firewall on and off. If I disable the connection to the corporate LAN then the Python client is happy. Just to reiterate, this configuration worked for several months.
I cannot find the difference. Here is the sample code:
Python
MY_IP = '192.168.100.2'
MY_PORT = 62828
ROBOT_IP = "192.168.100.10"
ROBOT_SOCK = 29999
robotSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
robotSocket.bind((MY_IP, MY_PORT)) # Tried without binding
robotSocket.connect((ROBOT_IP, ROBOT_SOCK))
Result "socket error 10013"
JAVA:
socket = new Socket(ROBOT_IP, ROBOT_SOCK);
is = socket.getInputStream();
in = new BufferedInputStream(is);
while (!desonnected)
{
try
{
String got = readInputStream(in); // in.readLine();
This works fine, but I cannot find the difference to adjust my Python code. I'm sure it is the OfficeScan, but....
Edit
I ran WireShark as suggested. With both network cards enabled the Python connection request is not sent.
After disabling the corporate LAN, I compared the Connection requests between the JAVA client and the Python client and they contained the same data except for the local port of course.
I have recently struggled a lot with socket binding and multiple NICs on Windows 7.
I'm normally a unix guy, but I had to do some C#.Net for a friend as a favor..
MY problem was that I couldn't bind a socket to a specific port and ip on the client side. The solution was to increase the "binding priority" of the NIC I wanted to bind to. I don't know much about Windows, so I can't really elaborate more. Read this article for some info, it helped me: Network adapter card priority binding order in windows 7
Not sure if you're experiencing the same problem, as a socket error 10013 seems to be permission related. Google gives me this: dealing with socket error 10013
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);