Skip to main content

How to open a server socket?

3 replies [Last post]
Joined: 2008-11-12

I try to open a server socket in my program but always get a ClassCastException.

My test program uses the example code in the following java file:

The java program:
package sockettest;


* @author kingpommes
public class Main {

* @param args the command line arguments
static void main(String[] args) {
try {
// Create the server listening socket for port 1234
ServerSocketConnection scn = (ServerSocketConnection)"socket://:1234");

// Wait for a connection.
SocketConnection sc = (SocketConnection) scn.acceptAndOpen();

// Set application specific hints on the socket.
sc.setSocketOption(SocketConnection.DELAY, 0);
sc.setSocketOption(SocketConnection.LINGER, 0);
sc.setSocketOption(SocketConnection.KEEPALIVE, 0);
sc.setSocketOption(SocketConnection.RCVBUF, 128);
sc.setSocketOption(SocketConnection.SNDBUF, 128);

// Get the input stream of the connection.
DataInputStream is = sc.openDataInputStream();

// Get the output stream of the connection.
DataOutputStream os = sc.openDataOutputStream();

// Read the input data.
String result = is.readUTF();

// Echo the data back to the sender.

// Close everything.

} catch (IOException e) {

For the creation of the suite file I use the "" script:
./d user-suite ../../SunSPOT/workspace/SocketTest

Command for the execution with the squawk-VM:
./squawk -suite:SocketTest -suitepath:../../SunSPOT/workspace/SocketTest sockettest.Main

The error message of the squawk-VM:
=== temp extra debugging info ===
target class: 3056976344 (image @ 369112)
source class: 3056933876 (image @ 326644)
implements interfaces:
interface 3056863960 (image @ 256728)
interface 3056759396 (image @ 152164)
interface 3056759272 (image @ 152040)
interface 3056759540 (image @ 152308)
interface 3056863368 (image @ 256136)
Uncaught exception in
on thread sockettest.Main - main
java.lang.ClassCastException: Expected object of type interface but got object of type class
at java.lang.Throwable.(bci=16)
at java.lang.Throwable.(bci=5)
at java.lang.Exception.(bci=6)
at java.lang.RuntimeException.(bci=6)
at java.lang.ClassCastException.(bci=6)
in static method #20 of com.sun.squawk.VM(bci=137)
at sockettest.Main.main(bci=8)
in virtual method #95 of com.sun.squawk.Klass(bci=32)
in virtual method #47 of com.sun.squawk.VMThread(bci=42)
in static method #3 of com.sun.squawk.VM(bci=6)

Hits - Class:93.93% Monitor:93.73% Exit:100.00% New:96.42%
GCs: 1 full, 0 partial
** VM stopped: exit code = 1 **

What am I doing wrong?


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-11-12

Instead of the ServerSocketConnection I use the StreamConnectionNotifier interface to realize the functionality I need for my program.

I still don't know why the ServerSocketConnection doesn't work.

Joined: 2009-10-13

Which branch are you using, trunk?

This could be a bug actually as we punch through to the Java VM running underneath when we dont understand a GCF protocol.

I think if you were using the native branch, which does not rely on an underlying running VM to provide file and socket type support, you would not have this problem.

So for now your stuck using the higher level interface.

Joined: 2006-09-08

I agree with Eric. On the trunk (and when building with DELEGATED on squawk-native), the connections is really a generic proxy connection to the Java SE JVM. When running the squawk-native branch built with NATIVE, then you get a real ServerSocketConnection.

You can look at some test code in squawk-native/tests2 for examples, such as: