|
|||
This example covers how to make a simple socket connection to a remote machine. There are really four different steps to this tutorial, they consist of:
There isn't much to setting up networking support for the POSE. The first obvious step is that the PC it is running on should be connected to a network. The only other step you need to do is to make sure and redirect NetLib calls to the host TCP/IP. You set this up by performing the following steps.
If you want to run this example on your Palm, you need to have some type of internet connection supported on your device. I am using a Palm V modem with my Palm. Unfortunately, I can't give step by step configuration instructions, as each is specific to your ISP. You start your configuration of your Palm by selecting the "Prefs" icon on the main panel, and then selecting the "Network" menu at the top right. If you do so, you get a screen similar to what is shown on the right. In my case I am using a "Unix" connection through the Palm V. The Palm allows you to set connections details and even build a script.
By selecting the "Details" button at the bottom, you get the screen shown
at right. This allows you to set the connection type between SLIP, CSLIP
and PPP. It also allows the IP address to be set by the script.
This final screen shows an
example login script. Yours will probably be different, but you should be
able to copy the script you use for your Windows PC login for your ISP.
The source code for this project:
The client side of this example is actually fairly simple. When the Palm connects to the server, it sends a quick hello message, then recieves a reply from the server...that's it. Since it is a one shot connection I don't use Threads on the client program (they will be saved for a later example). The biggest thing to remember is that when you finally use the MakePalmApp class on your project, you need to include the networking flag. This allocates the memory required by NetLib to bring up the networking services. If you don't do this, your application will compile just fine, it just won't work! If you have been using my examples so far, your final step in compilation should look like this:
java -classpath %allclasspath% palm.database.MakePalmApp -v -bootclasspath %j2meclasspath% -networking -classpath classes SockTest
Note the -networking
flag
There aren't many components in this program. I use a scrolling text box to hold all of my output (This is actually my BasicScrollTextBox, see my scrolling text tutorial for more details). There is a single Socket connection with an input and output stream.
You make the socket connection as follows:
StreamConnection socket; socket = (StreamConnection)Connector.open("socket://128.220.101.101:1501", Connector.READ_WRITE, true);This particular piece of code opens a socket connection on webdev.apl.jhu.edu (128.220.101.101) on port 1501. The Connector.open() call takes three arguments. The first is the URL for the connection. The CLDC API states:
This is done by dynamically looking up a class the name of which is formed from the platform name and the protocol of the requested connection. The parameter string describing the target conforms to the URL format as described in RFC 2396. This takes the general form: {scheme}:[{target}][{parms}] Where {scheme} is the name of a protocol such as http}. The {target} is normally some kind of network address, but protocols may regard this as a fairly flexible field when the connection is not network oriented. Any {parms} are formed as a series of equates on the form ";x=y" such as ;type=a.In my case, the scheme was "socket" and the target was "//128.220.101.101:1501"
The second argument for the open() call is the mode, and the third argument indicates whether the caller wants timeout exceptions.
Once you get the socket, it is just like any other java.net and java.io sequence, you first open an input and output stream on the object. I write a status string to the screen to indicate the connection has been made. Once the streams are opened, we send our hello message:
if (getFlashID() == null) { os.write(("Hello from somebody's POSE").getBytes()); } else { os.write(("Hello from Palm " + getFlashID()).getBytes()); }
This sends either our flash ID if we are a real Palm, or the POSE hello if we are using the POSE. I use a StringBuffer to collect the incoming characters, and then write the string to the scrolling text area.
The screen at right shows the output of the application when run. I have a server program listening on port 1501 for any connections should you choose to try. The server program keeps track of how many connections have been made since its inception.
The source code for this project:
To aid you in testing your program (and because I thought it would be fun) I have left the server program running on webdev.apl.jhu.edu (128.220.101.101) on port 1501 so that you can test your program on another server. The server program has nothing to do with the Palm, and in fact could be used by any Java application. I have provided it so you could see what was being done on the other end of the network connection. The code is well commented, and it basically does the following: