Gain a deep understanding of the design of the network program

Source: Internet
Author: User
Tags network function

This article will focus on the design of the network program for j2_vpc. This structure in the javax. microedition. io package includes the Connection class and several useful interfaces (including Stream Connection, ContentConnection, and HTTPConnection ). This article discusses the design of this package and uses the Stream Connection and ContentConnection interfaces to enhance the functions of the address bookMIDLet example described earlier.

Network Program Design

Previously, I introduced the development of local device data storage through the record management system (RMS). Another important feature of j2's is the ability to use the j2's connection structure to open network connections and transmit data. This structure in the javax. microedition. io package includes the Connection class and several useful interfaces (including Stream Connection, ContentConnection, and HTTPConnection ). This article discusses the design of this package and uses the Stream Connection and ContentConnection interfaces to enhance the functions of the address bookMIDLet example described earlier.

1. javax. microedition. io package

If you have experience using J2SEjava.net package development programs, you will know that it is widely used and provides some very advanced network performance. Unfortunately, due to the memory size of the device, these advanced features are not suitable for configuring CLDC for a limited connection device. To make up for this, we have a simplified but fully functional connection structure that allows simple connections to transmit data. The MID short table has a step forward. It also defines an HTTPConnection interface for HTTP access on the network.

2. Modify the Address Book MIDLet

The routines in this section are almost the same as those in the address book routine of the Record Management System in the previous section. The examples in the previous section use local data files, now I want to introduce the example of using the network function of J2EE to retrieve an address from a text file stored on the Internet. The text file name is address book.txt, and the names and addresses in the file are separated by commas. As mentioned above, the following example uses two different interfaces, Stream Connection and Content Connection, to transmit data.

3. Use Stream Connection to access data

The Stream Connection interface defines the minimum function required by a Stream Connection. Now let's modify the address bookMIDLet application:

Delete dbAddress in address bookMIDLet () constructor. addAddress () method call. This method call can be deleted because the new program does not have to add data to the database, the new program will use the network function of j2m's to retrieve the address stored on the Internet.

Add the specific connection code to the AddressDB constructor. The specific connection code in the two examples simply retrieves the address through TCP/IP and manually adds each address to the address book.

 
 
  1.  StreamConnection connStream=null;InputStreaminStream=null;  
  2. byte[]b=newbyte[255];  
  3. Stringaddress,name;  
  4. intcommalocation=0;  
  5. try{  
  6. connStream=(StreamConnection)  
  7. Connector.open("http://localhost/address book.txt");  
  8. inStream=connStream.openInputStream();  
  9. intcount=inStream.read(b);  
  10. address=newString(b);  
  11. addressaddress=address.trim();  
  12. StringTokenizerst=newStringTokenizer(address,"");  
  13. while(st.hasMoreTokens()){  
  14. address=st.nextToken();  
  15. commalocation=address.indexOf(',');  
  16. name=address.substring(0,commalocation);  
  17. addressaddress=address.substring(commalocation+1);  
  18. addAddress(name,address);  
  19. }  
  20. }  
  21. catch(IOExceptione){  
  22. System.out.println(e);  
  23. e.printStackTrace();  
  24. }  
  25.  

The above code can work, but it has a small problem. The length of the byte array must be limited to 255, because the StreamConnection interface cannot estimate the size of the downloaded data. To this end, I can use the ContentConnection interface and the getLength () method that is very useful.

Another problem is that J2SE java. util. StringTokenizer class is missing in J2SE. Although you can write the string analyzer class by yourself, you can also find the StringTokenizer class on the Internet.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.