Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shannah/cn1sockets
Codenameone Sockets Library
https://github.com/shannah/cn1sockets
Last synced: about 1 month ago
JSON representation
Codenameone Sockets Library
- Host: GitHub
- URL: https://github.com/shannah/cn1sockets
- Owner: shannah
- Created: 2013-12-27T20:03:11.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2021-10-25T15:23:33.000Z (about 3 years ago)
- Last Synced: 2023-04-01T05:43:20.845Z (over 1 year ago)
- Language: Java
- Homepage:
- Size: 22.3 MB
- Stars: 3
- Watchers: 6
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Codename Sockets Library
This library was started as an attempt to add sockets support to Codename One. I have so far created native interfaces for iOS, Android, Blackberry, JavaSE and J2ME.
## Installation
Install through Codename One Settings > Extensions. Or manually install with following steps:
1. Copy the [CN1Sockets.cn1lib](bin/CN1Sockets.cn1lib) library into your application's `lib` directory.
2. Right click on your project's icon in the Netbeans project explorer, and select "Refresh Libs"## Usage Example
~~~~
import ca.weblite.codename1.net.Socket;
// .. rest of imports// … your application's main class
public void start(){
lbl = new Label("Result here");
Form hi = new Form("Socket Tester");
Button btn = new Button("Run Test");
btn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent evt) {
Display.getInstance().callSerially(new Runnable(){public void run() {
runSocketTests();
}
});
}
});
hi.addComponent(btn);
hi.addComponent(lbl);
hi.show();
}
public void runSocketTests(){
try {
if ( Socket.isSocketSupported() ){
Socket sock = new Socket("example.com", 80);
sock.getOutputStream().write("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n".getBytes());String result = Util.readToString(sock.getInputStream());
// NOTE: Util.readToString() closes the input stream automatically.
// Don't use it if you need to keep the socket open. Use
// another mechanism to read the stream.
lbl.setText(result);
//sock.close();
Log.p(result);
} else {
lbl.setText("Sockets not supported");
Log.p("Sockets not supported");
}
} catch ( Exception ex){
Log.p("We have an error");
Log.e(ex);
}
}// …
~~~~
## To Do
1. Try out the J2ME and Blackberry ports. They are there but I have never tested them so they may need some tweaking.
2. Stress test.