Hi, got a question for the socket

48 views
Skip to first unread message

Albert Gao

unread,
Oct 9, 2017, 10:52:29 PM10/9/17
to CodenameOne Discussions
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans/Eclipse/IDEA
Desktop OS
Simulator
Device

Our client needs to maintain a socket connection to the server. (Plain socket, not web socket)

Then I found the doc:
https://www.codenameone.com/javadoc/com/codename1/io/Socket.html

I see there is only `listen()` without `read()` and `send()`

Our use cases are simple, client sends some `commands` to the server, and the response, and render them on the UI.

Where could I find an example to implement this `read()` and `send()` method?
Or at least some hints.

I checked the developer guide `11.13. Sockets` too.

Looks like I need to implement a `SocketListenerCallback` class,

and doing `send()` and `read()` in the `connectionEstablished(InputStream is, OutputStream os)`, right?
but that's one function, how to implement the `send()` and `read()` via that, or is there a better way?

And it should be cross-platformable, right?

Thanks

Shai Almog

unread,
Oct 10, 2017, 12:32:37 AM10/10/17
to CodenameOne Discussions
Hi,
there is also a cn1lib for sockets that works using a more traditional API. 

listen() isn't cross platform as server sockets are very different in mobile platforms. The callback function can just sleep and block on reading as it has its own thread. So you can do all your networking in one place.
Reply all
Reply to author
Forward
0 new messages