Introduction to the general network framework in j2s

Source: Internet
Author: User

The universal networking framework plays a very important role in the j2's platform. Without it, the applications we write can only be "mysterious" to outsiders ", the application of the universal network framework for networking is "amazing. This article will analyze the general network framework as a whole. The specific application will be detailed in subsequent articles.

The generic connection framework (GCF) is defined in cldc. Due to resource limitations of mobile information devices, java.net and Java. Io packages are not suitable for use here. In the real world, various network communication protocols are used, which makes it very difficult to design this framework. At least I think so. However, when analyzing GCF, we can clearly find that it has the following features: Based on the interface design, it facilitates expansion, provides the factory method for creating connections, and simplifies the programmer's work by using standard URLs. When we look at the cldc1.1 API, we can find that eight interfaces, a connector class, And a connectionnotfoundexception are defined. Extended in midp2.0 and provided the httpconnection and httpsconnection interfaces, so that the MIDlet can communicate with the server over HTTP or HTTPS, and provided interfaces for socketconnection, serversocketconnection, and udp1_ramconnection, this enables the MIDlet to communicate with each other through sockets or using datagram on the TCP/IP layer. The commconnection and push technologies are not described here.

It is the best way to understand the general network framework by combining the GCF interface Hierarchy Diagram. Let's look at its hierarchy.

The top interface is connection, and other interfaces are inherited from it. Only one method close () is defined in connection (). In our real world, packet data exchange and circuit exchange are usually used. Therefore, the corresponding datagramconnection and streamconnection are defined in the networking framework. Because stream-based transmission requires the ability to operate the input stream and output stream, streamconnection extends inputconnection and outputconneciton, which we often use. openinputstream (), Conn. the openoutputstream () method is defined in these two important interfaces. The streamconnectionnotifier interface defines the capabilities that the connection listener should possess. Its method acceptandopen () returns a streamconnection type connection. serversocketconnection inherits the streamconnectionnotifier interface, in this way, if you are using the socket server, you can use this URL to establish a listening port on your device and wait for the connection. Socketconnection inherits streamconnection and can interact with serversocketconnection. Udpdatagramconnection is used in group data exchange. It inherits the datagramconnection interface. The contentconnection interface defines only three methods: getencoding (), getlength (), and GetType (). httpconnection, which we are very familiar with, is its subclass. It defines a large number of operations in httpconnection, the HTTP networking feature is also required by the MIDP specification. Now you should be clear about the layers. Continue to see how to use GCF.

GCF is very simple to use and mainly focuses on the open () method of ctor. All we need to do is provide a standard URL parameter to pass to the open method. For example, to get an httpconnection, we should write code similar to the following,
String url = "http: // myip: myport/myservlet ";
Httpconnection httpconn = (httpconnection) connector. Open (URL );
We should know how to define the URL format. If you are interested, you can refer to rfc2396. Here I will only include the basic format: {scheme }: [{target}] [{parms}]. For different network communication modes, you need to write different URLs and obtain the desired connection type by force conversion. This article does not provide how to perform data transmission operations, you can refer to the midp api documentation httpconnection interface in a detailed description, you can also refer to the following article http://blog.csdn.net/mingjava/archive/2004/07/07/36528.aspx
Do not forget to close the connection after the data transmission ends. In this case, you should use the close () method defined in connection.
Try {
// Here close any open streams
Conn. Close ();
} Catch (ioexception IOE ){
// Handle the exception.
// Throw the exception, ignore it, or return an error.
}

So far, the overall introduction to GCF is over. We should be clear about the interface hierarchy of GCF and how to use GCF. At the same time, do not forget to remember the design of this framework. In fact, I think many designs in cldc and MIDP deserve us to think a lot.

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.