[官方Demo]SocketDemo 通讯例子
[官方Demo]SocketDemo 通信例子
SocketDemo.java
/** * SocketDemo.java * * Copyright �1998-2010 Research In Motion Ltd. * * Note: For the sake of simplicity, this sample application may not leverage * resource bundles and resource strings. However, it is STRONGLY recommended * that application developers make use of the localization features available * within the BlackBerry development platform to ensure a seamless application * experience across a variety of languages and geographies. For more information * on localizing your application, please refer to the BlackBerry Java Development * Environment Development Guide associated with this release. */ package com.rim.samples.device.socketdemo; import net.rim.device.api.ui.*; /** * This sample enables client/server communication using a simple implementation * of TCP sockets. The client application allows the user to select direct TCP as * the connection method. If direct TCP is not selected, a proxy TCP connection * is opened using the BlackBerry MDS Connection Service. The server application * can be found in com/rim/samples/server/socketdemo. */ public class SocketDemo extends UiApplication { SocketDemoScreen _screen; /** * Entry point for application. * @param Command line arguments. */ public static void main(String[] args) { // Create a new instance of the application and make the currently // running thread the application's event dispatch thread. SocketDemo app = new SocketDemo(); app.enterEventDispatcher(); } // Constructor public SocketDemo() { // Create a new screen for the application. _screen = new SocketDemoScreen(); // Push the screen onto the UI stack for rendering. pushScreen(_screen); } /** * Provides access to this application's UI screen * @return This application's UI screen. */ SocketDemoScreen getScreen() { return _screen; } }
SocketDemoScreen.java
/* * SocketDemoScreen.java * * Copyright �1998-2010 Research In Motion Ltd. * * Note: For the sake of simplicity, this sample application may not leverage * resource bundles and resource strings. However, it is STRONGLY recommended * that application developers make use of the localization features available * within the BlackBerry development platform to ensure a seamless application * experience across a variety of languages and geographies. For more information * on localizing your application, please refer to the BlackBerry Java Development * Environment Development Guide associated with this release. */ package com.rim.samples.device.socketdemo; import net.rim.device.api.system.*; import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; /** * A MainScreen class to allow for user interaction. */ public class SocketDemoScreen extends MainScreen { private EditField _hostField; private CheckboxField _useDirectTcpField; private RichTextField _statusField; private StringBuffer _message; private boolean _threadRunning = false; // Constructor public SocketDemoScreen() { setTitle(new LabelField("Socket Demo")); add(new RichTextField("Enter local host name in the field below and select 'Go' from the menu." ,Field.NON_FOCUSABLE)); add(new SeparatorField()); // Need to get the local host name from the user because access to // 'localhost' and 127.0.0.1 is restricted. _hostField = new EditField("Local Host: " , ""); add(_hostField); _useDirectTcpField = new CheckboxField("Use Direct TCP" , RadioInfo.getNetworkType() == RadioInfo.NETWORK_IDEN); add(_useDirectTcpField); _statusField = new RichTextField(Field.NON_FOCUSABLE); add(_statusField); _message = new StringBuffer(); } /** * Method to display a message to the user. * @param msg The message to display. */ void updateDisplay(final String msg) { UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { _message.append(msg); _message.append('\n'); _statusField.setText(_message.toString()); } }); } /** * Returns the text entered by the user. * @return text entered by the user. */ String getHostFieldText() { return _hostField.getText(); } /** * Indicates whether the direct TCP checkbox is checked. * @return True if checkbox is checked, otherwise false. */ boolean isDirectTCP() { return _useDirectTcpField.getChecked(); } /** * Setter for boolean _threadRunning * @param running True if a ConnectThread is running, otherwise false. */ void setThreadRunning(boolean running) { _threadRunning = running; } /** *@see net.rim.device.api.ui.container.MainScreen#makeMenu(Menu,int) */ protected void makeMenu(Menu menu, int instance) { // If a ConnectThread is running we won't add our menu item if (!_threadRunning) { menu.add(_go); } super.makeMenu(menu, instance); } /** * Prevent the save dialog from being displayed, nothing to save. * * @see net.rim.device.api.ui.container.MainScreen#onSavePrompt() */ public boolean onSavePrompt() { return true; } /** * Handles the user pressing ENTER while the * 'use direct tcp' CheckboxField has focus. * * @see net.rim.device.api.ui.Screen#keyChar(char,int,int) * */ protected boolean keyChar( char key, int status, int time ) { if ( key == Characters.ENTER ) { Field fieldWithFocus = getFieldWithFocus(); if(fieldWithFocus == _useDirectTcpField) { if(_useDirectTcpField.getChecked()) { _useDirectTcpField.setChecked(false); } else { _useDirectTcpField.setChecked(true); } return true; // We've consumed the event. } } return super.keyChar( key, status, time ); // We'll let super handle the event. } /** * An anonymous MenuItem class. */ private MenuItem _go = new MenuItem("Go" , 11000, 0) { public void run() { // Don't do anything unless there is a host name in the _host field. if (_hostField.getText().length() > 0) { new ConnectThread().start(); _threadRunning = true; // Hide the virtual keyboard so the user can see status updates. if(VirtualKeyboard.isSupported()) { VirtualKeyboard keyboard = getVirtualKeyboard(); if(keyboard.getVisibility() != VirtualKeyboard.HIDE) { keyboard.setVisibility(VirtualKeyboard.HIDE); } } } else { Dialog.ask(Dialog.D_OK, "Please enter a valid host name" ); } } }; }
ConnectThread.java
/* * ConnectThread.java * * Copyright �1998-2010 Research In Motion Ltd. * * Note: For the sake of simplicity, this sample application may not leverage * resource bundles and resource strings. However, it is STRONGLY recommended * that application developers make use of the localization features available * within the BlackBerry development platform to ensure a seamless application * experience across a variety of languages and geographies. For more information * on localizing your application, please refer to the BlackBerry Java Development * Environment Development Guide associated with this release. */ package com.rim.samples.device.socketdemo; import java.io.*; import javax.microedition.io.*; import net.rim.device.api.ui.*; /** * A thread class to handle communication with the server component. */ public class ConnectThread extends Thread { private InputStreamReader _in; private OutputStreamWriter _out; private SocketDemoScreen _screen; // Constructor public ConnectThread() { _screen = ((SocketDemo)UiApplication.getUiApplication()).getScreen(); } /** * Pass some data to the server and wait for a response. * @param data The data to send. */ private void exchange(String data) throws IOException { // Cache the length locally for better efficiency. int length = data.length(); // Create an input array just big enough to hold the data // (we're expecting the same string back that we send). char[] input = new char[length]; _out.write(data, 0, length); // Read character by character into the input array. for (int i = 0; i < length; ++i) { input[i] = (char)_in.read(); } // Hand the data to the parent class for updating the GUI. By explicitly // creating the stringbuffer we can save a few object creations. StringBuffer s = new StringBuffer(); s.append("Received: ") ; s.append(input, 0, length); _screen.updateDisplay(s.toString()); } /** * Implementation of Thread. */ public void run() { StreamConnection connection = null; try { _screen.updateDisplay("Opening Connection..."); String url = "socket://" + _screen.getHostFieldText() + ":44444" + (_screen.isDirectTCP() ? ";deviceside=true" : ""); connection = (StreamConnection)Connector.open(url); _screen.updateDisplay("Connection open"); _in = new InputStreamReader(connection.openInputStream()); _out = new OutputStreamWriter(connection.openOutputStream()); // Send the HELLO string. exchange("Hello"); // Execute further data exchange here... // Send the GOODBYE string. exchange("Goodbye and farewell"); _screen.updateDisplay("Done!"); } catch(IOException e) { System.err.println(e.toString()); } finally { _screen.setThreadRunning(false); try { _in.close(); } catch(IOException ioe) { } try { _out.close(); } catch(IOException ioe) { } try { connection.close(); } catch(IOException ioe) { } } } }