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 ...)
Related
Good day,
I have a java game that I want to play with a friend over network, I have implemented Sockets and tested the game on my pc using localhost as address, but was unable to connect to the external ip of my pal's pc, presumably due to us both being behind routers.
Here is the code of host/client:
CLIENT:
try {
socket = new Socket(inputHostIp(), 5555);
} catch (IOException e) {
e.printStackTrace();
}
SERVER:
try {
hostServer = new ServerSocket(5555);
} catch (IOException e1) {
e1.printStackTrace();
}
listenForUserConnection();
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
Socket socket = null;
try {
socket = hostServer.accept();
} catch (IOException e) {
e.printStackTrace();
continue;
}
joined(socket);
}
The exception I am getting now is
java.net.ConnectException: Connection timed out: connect
On trying to init I/O:
java.net.SocketException: Socket is not connected
java.net.Socket.getInputStream/java.net.Socket.getOutputStream
I have set up port forwarding with the chosen port number (5555) linked to the internal ip on both our machines.
What are my options for getting this to work?
ADDENDUM:
We have also tried using Hamachi to create a virtual LAN, but there seems to be an issue with that - we can’t ping one another even through that, it diagnoses with an issue -
Tunnel:
VPN domain's tap device is down
Local results:
Adapter configuration:
Cannot get adapter config
Traffic test: Cannot complete test
Peer results: [160-056-951]
Adapter configuration: OK
Traffic test: Inbound traffic blocked, check firewall settings
I have tried shutting down firewalls, hamachi issues changed to just ‘cannot get adapter config’, but otherwise no results.
On my pc, however, I got a version of windows that doesn’t seem to display Firewall setting properly, if you think it’s likely an issue, can you tip me on how to test my firewall?
so I'm currently doing my thesis and a part of it is to communicate with KVM (Kernel Based Virtual Machine). I was already able to establish a connection through the use of the libvirt Java Binding. The virtual machine i created in KVM (centostest) is already up and running, however i want to shut it down but i got the following errors:
libvir: Domain error : operation virDomainShutdown forbidden for read only access
org.libvirt.LibvirtException: operation virDomainShutdown forbidden for read only access
at org.libvirt.ErrorHandler.processError(ErrorHandler.java:33)
at org.libvirt.Connect.processError(Connect.java:1322)
at org.libvirt.Domain.processError(Domain.java:830)
at org.libvirt.Domain.shutdown(Domain.java:972)
at Main.testkvm(Main.java:31)
at Main.main(Main.java:16)
Below is a portion of the code:
Connect conn;
try {
conn = new Connect("qemu:///system", true);
Domain testDomain = conn.domainLookupByName("centostest");
testDomain.shutdown();
} catch (LibvirtException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Any suggestions?
You are using read-only connection. Try this:
conn = new Connect("qemu:///system", false);
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'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.
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.