Skip to main content

How to open a server socket?

3 replies [Last post]
kingpommes
Offline
Joined: 2008-11-12
Points: 0

Hi,
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: javax.microedition.io.ServerSocketConnection

The java program:
package sockettest;

import java.io.*;
import javax.microedition.io.*;

/**
*
* @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) Connector.open("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.
os.writeUTF(result);

// Close everything.
is.close();
os.close();
sc.close();
scn.close();

} catch (IOException e) {
System.err.println(e);
}
}
}

For the creation of the suite file I use the "d.sh" 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 javax.microedition.io.StreamConnection 3056863960 (image @ 256728)
interface javax.microedition.io.InputConnection 3056759396 (image @ 152164)
interface javax.microedition.io.Connection 3056759272 (image @ 152040)
interface javax.microedition.io.OutputConnection 3056759540 (image @ 152308)
interface javax.microedition.io.StreamConnectionNotifier 3056863368 (image @ 256136)
Uncaught exception in Thread.run():
on thread sockettest.Main - main
java.lang.ClassCastException: Expected object of type interface javax.microedition.io.ServerSocketConnection but got object of type class com.sun.squawk.io.j2me.channel.Protocol
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)
at com.sun.squawk.Isolate.run(bci=414)
at java.lang.Thread.run(bci=17)
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?

Dennis

Reply viewing options

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

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.

ericarseneau
Offline
Joined: 2009-10-13
Points: 0

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.

derek_white
Offline
Joined: 2006-09-08
Points: 0

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:

http://fisheye4.atlassian.com/browse/squawk/branches/squawk-native/tests...