ClassCastException in Connector.java code

Hi All,

I understand what ClassCastException is. This is the code from one of the CLDC API's provided (Connector.java) which causes the exception :-

public static DataInputStream openDataInputStream(String name)

throws IOException {

InputConnection con =

(InputConnection)Connector.open(name, Connector.READ);

try {

return con.openDataInputStream();

} finally {

con.close();

}

}

Connector.open() is supposed to return a new Connection object. I am trying to cast it to Inputconnection which is a subinterface of Connection. With this error I am unable to create any network connection. The name string I am providing is "datagram://1.9.67.1:32767". When I printed the object returned by Connector.open(), I found this :-

com.sun.cldc.io.j2me.datagram.Protocol@1a4cfaaa

This does not appear to be the same as javax.microedition.io.Connection (which is an interface anyway). datagram.Protocol implements DatagramConnection.

Now can anyone help as to how to fix this problem? Or just show me sample code to open a simple datagram connection (UDP) and associate with InputStream object.

Thanks,

SM.

[1218 byte] By [sm111] at [2007-9-26 1:14:07]
# 1

I figured out what the problem is. InputStream can only be associated with a StreamConnection. That is possible only with "socket" protocol. So to use "datagram" protocol and also associate it with InputStream object, an intermediate layer has to be written which is not provided by CLDC API.

Cheers,

SM.

sm111 at 2007-6-29 0:12:26 > top of Java-index,Java Mobility Forums,Java ME Technologies...