Following is the part of my error. Because of this error my applet isn't initialized. The following code is part of the init() method:
Socket sock;
try {
sock = new Socket("localhost", 1307);
out = new PrintWriter(sock.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
}
catch (UnknownHostException e) { }
catch (IOException e) { }
I m getting the following error:
java.security.AccessControlException: access denied (java.net.SocketPermission 1
127.0.0.1 resolve)
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:323)
at java.security.AccessController.checkPermission(AccessController.java:
546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkConnect(SecurityManager.java:1031)
at java.net.InetAddress.getAllByName0(InetAddress.java:1145)
at java.net.InetAddress.getAllByName(InetAddress.java:1083)
at java.net.InetAddress.getAllByName(InetAddress.java:1019)
at java.net.InetAddress.getByName(InetAddress.java:969)
at java.net.InetSocketAddress.<init>(InetSocketAddress.java:124)
at java.net.Socket.<init>(Socket.java:186)
at Alice.init(Alice.java:103)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:619)
I am running another program on another JVM. I am trying to connect both using 1307 port on localhost.
Applets may not connect to any host except the one they were loaded from (due to security reasons).
If you want to do that anyway, you must sign your applet.
This is a security issue that does not let you create a connection from within an applet. Applets in general are not allowed to open socket connections.
To be more specific, you can only open connections to the server that served the applet.
Related
We are currently trying to implement WebSocket server using Tyrus and everything went alright (server-client communication worked well) until we tried to test what happens if server initialization fails (e.g. bad port).
The underlying code throws SocketException (permission denied). The exception is written to stdout but the server proceeds and the program continues beyond start() method.
public void runServer() {
// bad port number
Server server = new Server("localhost", 10, "/websockets", null, EchoEndpoint.class);
try {
server.start();
// this line should not be printed
System.out.println("Server started");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please press a key to stop the server.");
reader.readLine();
} catch (Exception e) {
System.out.println("Exception caught");
e.printStackTrace();
} finally {
server.stop();
}
}
Is there any way to detect whether the server started successfully?
EDIT: We know we cannot use port 10 (that is why we tried it). We just need to check whether the server is running (by somehow catching the exception). (And we do not want to test it by an attempt to send some dummy data with a client - that would not really fix the problem)
Your port number '10' is too low to be used by regular user. You must be privileged user or use a port number greater than 1024:
Server server = new Server("localhost", 1025, "/websockets", null, EchoEndpoint.class)
In my desktop application, I am connecting to a server through a web service.
Using the code below getting the client machine Tomcat status, I can get the server IP address and port number, but how can I find the server Tomcat status?
InetAddress locIP = InetAddress.getByName("127.0.0.1");
serverSocket = new ServerSocket(8080, 0, locIP);
You can use Java's URLConnection as follows:
try {
URL url = new URL("http://youserver.com");
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(2000); //Connection timeout
urlConnection.connect();
System.out.println("Server is up and running");
} catch (Exception e) {
System.out.println("Server is not yet up");
}
if urlConnection.connect()returns silently within 2 seconds, means the server is up and running, else an exception is thrown which indicates the server is not up or the URL is incorrect.
All you need to do to detect a running Tomcat here or elsewhere is to try to connect to it with a new Socket(). If that works, it's running; if not, not. Don't send anything, just close the socket immediately.
I am using Java to do the socket programming as below.
Client program is as below:
Socket MyClient;
try {
MyClient = new Socket("Machine name", PortNumber);
}
catch (IOException e) {
System.out.println(e);
}
Server program is as below:
ServerSocket MyService;
try {
MyServerice = new ServerSocket(PortNumber);
}
catch (IOException e) {
System.out.println(e);
}
Socket clientSocket = null;
try {
clientSocket = MyService.accept();
}
catch (IOException e) {
System.out.println(e);
}
Now my question is if I run more than one thread to open several sockets in one port (as the server code above), how my client program know which socket it is connecting to?
Your client connects to the Servers port. So all clients will be having the same code
MyClient = new Socket("Machine name", <port where server is listening>);The port opened at client side is not important. The client will get a free port available in his OS.
how my client program know which socket it is connecting to?
The question doesn't make sense. It doesn't 'connect to a socket' at all, it connects to a listening port, and there is only one of those. Your server only accepts one client, so the second and subsequent threads will get an undefined behaviour ranging from a ConnectException to a ConnectionException to nothing, most probably the latter.
Your application knows it because you set it up with a specific port. There is no "auto discovery" built into TCP/IP, it's up to you to pick a server-port and make sure you set your clients up to connect to that port. Either you hard-code this into your client application or, better yet, have it in some configuration file you include with the client.
This is why you have a bunch of "known ports", like http is port 80. This means that a browser will always connect to port 80 on a web-server, unless you explicitly indicate another port in the URL.
I am using the following java code in Android AVD on Windows7 to create my server with serverPort = 1131;
try {
ServerSocket serverSocket = new ServerSocket(serverPort);
serverSocket.setReuseAddress(true);
while(isRunning){
try {
final Socket socket = serverSocket.accept();
DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();
serverConnection.bind(socket, new BasicHttpParams());
httpService.handleRequest(serverConnection, httpContext);
serverConnection.shutdown();
} catch (IOException e) {
e.printStackTrace();
} catch (HttpException e) {
e.printStackTrace();
}
}
serverSocket.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
I get the following exception :-
01-18 06:30:03.381: W/System.err(1494): java.net.BindException: bind failed: EACCES (Permission denied)
The firewall on my machine is off & I have added special rules for that as well.
Do I need to do something special for running server on AVD on Window7?
Kindly help.
Thanks
I found the following on the MSDN site (search the site for "bind" and "EACCES"):
WSAEACCES - 10013
Permission denied.
An attempt was made to access a socket in a way forbidden by its access permissions. An example is using a broadcast address for sendto
without broadcast permission being set using setsockopt(SO_BROADCAST).
Another possible reason for the WSAEACCES error is that when the bind function is called (on Windows NT 4.0 with SP4 and later),
another application, service, or kernel mode driver is bound to the
same address with exclusive access. Such exclusive access is a new
feature of Windows NT 4.0 with SP4 and later, and is implemented by
using the SO_EXCLUSIVEADDRUSE option.
Thus, if we assume that the JVM native libraries map WSAEACCES to this exception, there are two obvious possible explanations:
This is a permissions-based thing. ADV doesn't have permission to bind to that port.
Some other application has already bound to the port with the SO_EXCLUSIVEADDRUSE socket option.
IMO, either explanation is plausible. (Or it could be something else ...)
I'm trying to connect to a simple Java server on my computer (in the future a true server, but I'm just learning how to program with sockets first. When I try to connect, the application on the phone throws an IOException. However, on the emulator, it does NOT.
I do have:
< uses-permission android:name="android.permission.INTERNET"/>
included in the manifest. And here's the code block that's executed when I hit open:
try {
responseField.setText("Opening socket...");
Socket socket = new Socket(getIP(),Integer.parseInt(getPort()));
responseField.setText("Socket opened. Initializing out...");
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
responseField.setText("Done. Initializing in...");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
responseField.setText("Done.");
} catch (NumberFormatException e1) {
responseField.setText("NumberFormatException");
} catch (UnknownHostException e1) {
responseField.setText("UnknownHostException");
} catch (IOException e1) {
responseField.setText("IOException");
}
Are you making sure that the server end uses a ServerSocket and uses the ServerSocker.accept() method?
So it seems that a weak Wi-Fi signal is causing error. I tried to surf the web (Google, CNN, etc.) afterward, and I could not. So I will just have to test on the emulator for now, or in a stronger signal. Thanks
If you were able to connect to the web before (I am assuming) but not after, then its not a problem with the wifi strength. Also depending on place you are surfing, the wifi router may have been configured not to allow such connections. Try to ping your server IP using a different computer within the same network and see whether you can ping. Emulator will work since the server is running on the localhost.