Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Unhandled java.lang.Error in com.sun.net.httpserver.HttpServer

843798Nov 27 2007 — edited Dec 19 2007
I'm experiencing the sample problem on SCO. I've included a test case below. Run the Sample class on the server, run the vbs script on the client and you should see the error. Who should be catching this error?

Compiled with javac 1.5.0_03 (Windows XP SP2)
Run with version 1.5.0_09 (SCO_SV 5v6.0.0)

The following library is required.
http://download.java.net/maven/2/com/sun/net/httpserver/http/20070405/http-20070405.jar

The sample class was based on the Sun documentation.
http://java.sun.com/javase/6/docs/jre/api/net/httpserver/spec/com/sun/net/httpserver/package-summary.html

The following documentation may also be useful.
http://www-1.ibm.com/support/docview.wss?uid=swg1IZ07128
http://www-1.ibm.com/support/docview.wss?uid=swg1PK46040

Client
url="http://machine.domain.com:6666"
set http = createObject("Microsoft.XMLHTTP")
http.open "POST", url, false
http.send "<x></x>"
msgbox http.responseText
msgbox "click me to cause java.lang.error"
set http = nothing

Source
import java.io.*;
import java.net.InetSocketAddress;
import com.sun.net.httpserver.*;

public class Sample implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
System.out.println("handle{");
InputStream is = t.getRequestBody();
is.read(); // .. read the request body
String response = "This is the response";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
System.out.println("}");
}
public static void main (String args[]) {
try {
HttpServer server = HttpServer.create(new InetSocketAddress(6666), 0);
server.createContext("/", new Sample());
server.setExecutor(null); // creates a default executor
server.start();
while(true) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("Exit.");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Output
bash-3.2$ /usr/java2/jre/bin/java -Djava.ext.dirs=. Sample
BEGIN
END
Exception in thread "Thread-2" java.lang.Error: java.net.SocketException: Invalid argument
at sun.nio.ch.Net.localAddress(Net.java:123)
at sun.nio.ch.SocketChannelImpl.localAddress(SocketChannelImpl.java:394)
at sun.nio.ch.SocketChannelImpl.toString(SocketChannelImpl.java:770)
at sun.net.httpserver.HttpConnection.close(HttpConnection.java:91)
at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:552)
at sun.net.httpserver.ServerImpl$DefaultExecutor.execute(ServerImpl.java:119)
at sun.net.httpserver.ServerImpl$Dispatcher.handle(ServerImpl.java:356)
at sun.net.httpserver.ServerImpl$Dispatcher.run1(ServerImpl.java:330)
at sun.net.httpserver.ServerImpl$Dispatcher.run(ServerImpl.java:277)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.net.SocketException: Invalid argument
at sun.nio.ch.Net.localInetAddress(Native Method)
at sun.nio.ch.Net.localAddress(Net.java:120)
... 9 more
bash-3.2$
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 16 2008
Added on Nov 27 2007
1 comment
3,308 views