Recently we installed a new Leap 42.2 for some high school students doing a stage here. I intended them to use a java application of my own which happily runs on opensuse 11 (and I’m told on windows too). The application has a little standalone tester program (attached below), which essentially attempts to open a socket with itself. Apparently it fails on the statement marked in red.
DEBUG socket created
DEBUG socket bound to null
DEBUG got local address 0.0.0.0/0.0.0.0:48070
Exception in thread “main” java.net.ConnectException: Connection refused (Connection refused)
at prova.main(prova.java:19)
After trying to play with java.policy (which wasn’t the case), I posted it to a java forum, and got the suggestion “this is a linux problem caused by the kernel security features that prevent local connections” and was directed to “modifying some of the options in /etc/sysctl.d/99-sysctl.conf”. However there are too many (I tried some of those different in my old suse 11) and no obvious documentation.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketAddress;
public class prova {
public static void main(String] arges) throws Exception {
Socket sk = new Socket();
System.out.println(" DEBUG socket created");
sk.bind(null);
System.out.println(" DEBUG socket bound to null");
SocketAddress addr = sk.getLocalSocketAddress() ;
System.out.println(" DEBUG got local address "+addr);
sk.connect(addr);
System.out.println(" DEBUG socket connected");
BufferedReader in = new BufferedReader(new InputStreamReader(sk.getInputStream()));
System.out.println(" DEBUG input stream OK");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sk.getOutputStream())), true);
System.out.println(" DEBUG output stream OK");
System.out.println(" ");
out.println("ABCD");
System.out.println(" written to output channel message ABCD");
out.println("XYZT");
System.out.println(" written to output channel message XYZT then I will
read one from input");
System.out.println(" read "+in.readLine());
out.println("ababab");
System.out.println(" written to output channel message ababab");
out.println("zxzxzx");
System.out.println(" written to output channel message zxzxzx");
out.println("maimai");
System.out.println(" written to output channel message maimai");
System.out.println(" then I will read THREE from input and exit");
System.out.println(" read "+in.readLine());
System.out.println(" read "+in.readLine());
System.out.println(" read "+in.readLine());
}
}
The provisional workaround was to replace the local sockets with a couple of a PipedReader and PipedWriter, but why should I not be able of opening a socket to myself ?
If you posted to, and had a conversation in a public Java forum, can you pls post a link to that forum thread?
That way people can evaluate the quality of the conversation and perhaps get a head start on whatever theories are made.
I’d have to think about whether this is likely a Java/kernel problem, particularly since you appear to be opening a <network> socket which is generally considered an external interface. My initial reaction is to wonder if you somehow defined the IP address to be a localhost address and then test using a working network addressable address (eg 192.168.1.1), whether that would make a difference instead of your general broadcast (0.0.0.0) address.
I don’t have a java compiler installed
imo there are 2 possibilities #1 you used an older version of Oracle’s java for development and said function does not exist in openjdk 1.7 (did you try recompiling it) #2 your firewall is blocking it, it doesn’t matter if you’re usin 127.0.0.1 the firewall blocks even local inbound connections so add an exception or compleatly disable the firewall
I don’t have a large understanding of opensuse’s apparmor but that could be causing you issues too
it’s my understanding that a java application does not deppend on the underlying OS just on the runtime and byte compiled code does not interact with the OS kernel only with the runtime
if I get the time I’ll install the java 1.7 compiler and try it out
I compiled that java code with openjdk 1.8 ie java 8
and had a different error from yours
this is the error I get when I try to run it (it says nothing about connection refused)
me@linux:~/Documents> java prova
DEBUG socket created
DEBUG socket bound to null
DEBUG got local address 0.0.0.0/0.0.0.0:53818
Exception in thread "main" java.net.UnknownHostException: linux: linux: Name or service not known
at java.net.InetAddress.getLocalHost(InetAddress.java:1505)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:204)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at prova.main(prova.java:17)
Caused by: java.net.UnknownHostException: linux: Name or service not known
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getLocalHost(InetAddress.java:1500)
... 6 more
me@linux:~/Documents>
I’ve named my machine linux so a linux host name is valid for me I don’t know why openjdk throws an an exception Exception in thread “main” java.net.UnknownHostException: linux: linux: Name or service not known
the error is related to your line 17
sk.connect(addr);
sk can’t connect to ‘linux’ you should re-read the socket connection in the java doc
It is a very short thread on Jan 18 in Usenet’s comp.lang.java.programmer … since I read it via nntp I won’t know how to give an universal link.
Well, the story is long … the original “real” application was an applet talking with a servlet over such socket. When I transformed it into a standalone application I wanted to change the less possible in the protocol, so now there is a method inside the application which emulates what the servlet did, and the application talks with itself.
Of course the test code I show is a bare-bone example which does just the socket stuff.
That’s not true, is in documentation “everywhere” (yes, that can’t be absolutely true “everywhere” but is predominantly so) and is exactly why I suggested the test… The localhost interface is special in that firewall rules don’t normally apply… unless the rule is written specifically to apply to that interface which would generally be very unusual.
Typically all Usenet forum archives can be found somewhere on the Internet and are often browsable using http… You just need to identify the exact forum name you’ve subscribed to, and sometimes go back to the original subscription information to identify specific servers or the company that the servers belong to (and their mirrors).
From a Developer point of view,
I’d probably also ask myself whether your choice of using network sockets is necessarily the best method for your proposed application. It might be, particularly if one of your objectives is to create a network-scalable application.
But,
Since you seem to be creating an application connecting to something else on same machine, I’d also consider whether there is a common, off the shelf solution for what you seem to be creating from scratch (while also recognizing in a classroom setting, you may be creating your app as an intellectual exercise).
If your reasons might be pragmatic, then you might want to take a look at things like JDBC drivers for exchanging data between the Java app and a non-Java app using ODBC standards.
Thanks for your interest, but that was not my question. The immediate problem with the REAL application (and also the test one) has already been solved replacing the sockets with piped readers and writers (something about which I did not know when I wrote the original precursor). As I already said, such precursor was an applet talking over the sockets with a servlet, which in turn was talking JDBC to a mysql database (the latter one is on a different machine from the servlet, the servlet is on my machine). I used happily the applet and also some colleagues from a remote site. Others elsewhere did not. That was years ago. Than things become painful because of signed applets and so on, and when I tried the move to java web start I discovered that with a little change I could make a standalone application. Then I decoupled the usage of the servlet (some parts of it are still called but via a GCI called via http). There were some problems with java.policy so I wrote the test program (but a java policy failure happens differently). Anyhow when I did the decoupling **I did not want to change the protocol **(and still don’t) used by the real application (essentially is “get image” “obtain image URL” “get region” “obtain list of regions to overlay”) so I moved the answering part of this from the servlet to the application, and had the application talk to itself via the local sockets (now via the piped reader).
I am just curious **why such a simple thing like opening a socket on the local machine **(which works on Suse 11, and I’m told works even on Windows) does not work on Suse Leap (and I’m told also on some Xubuntu).