The General Networking frame played a very important role in the J2ME platform. If there is no, the application we wrote can only say "mysterious" for outsiders, and the program applying the general networked framework is "magical". . This article will perform overall analysis of the general networking framework, and specific applications will be described in detail in subsequent articles.
Generic Connection Framework (hereinafter referred to as GCF) is defined in the CLDC, because the resource resource resource resource, so Java.Net and Java.IO packets are not suitable here. The real world uses a variety of network communication protocols, which makes it easy to design this framework, at least I think so. However, when we analyze GCF, it can clearly find that it has the following characteristics: based on interface design, easy to expand, provide factory methods for creating a connection, using standard URL simplifying programmers. When we look at the API of CLDC1.1, we can find 8 interfaces, a Connector class and a connectionNotFoundException exception. Extension in MIDP 2.0, providing HTTPConnection, HttpsConnection interface, so that MIDlet has the ability to communicate with Server via HTTP or HTTPS protocol, providing SocketConnection, ServerSocketConnection, UDPDataGramConnection interface, making MIDlet to pass in TCP / IP layer Socket is communicating or using datagram for communication. As for CommConnection, Push technology is not described here.
Combined with GCF interface hierarchical diagram to learn about the general networking framework is the best way, let's take a look at its hierarchy. The uppermost interface is Connection, and other interfaces are inherited from him. A method close () is only defined in the Connection. In our real world is typically used, packet data exchange and circuit exchange, so DataGramConnection and StreamConnection are defined in the networking framework. Since we need to have an operation based on streaming, the stream and output circulation is an operation, STREAMCONNECTION extends the InputConnection and OutputConneciton, and the conn.openocutstream () we often use is all in these two Important interfaces are defined. The StreamConnectionNotifier interface defines the ability to connect to the listener. Its method AcceptAdopen () method returns a connection, ServerSocketConnection inherits the StreamConnectionNotifier interface, so if you do Socket Server, you can use this URL, Socket: //: Port is established on your device to wait for a connection. SocketConnection inherits StreamConnection. You can confirm with ServersocketConnection. UDPDataGramConnection is to use in packet data exchange, he inherits the DataGramConnection interface. The ContentConnection interface only defines three methods getENCODING (), getLength () and gettype (), our very familiar HTTPConnection is his subclass, defining a lot of operation in HTTPConnection, HTTP networking function is also required for MIDP specifications The connection to the connection must be supported. Now you should be more clear about the level, continue to see how to use GCF.
GCF is very simple, mainly focused on the open () method of Connector. What we have to do is to provide a standard URL parameter to the Open method, for example, in order to get an HTTPConnection, we should write similar to the following code, string url = "http: // myip: myport / myservlet"; httpConnection httpconn = (httpConnection) Connector.Open (URL); we should clear how this URL format is defined, if you are interested, you can refer to RFC2396, I am only included in his basic format: {scheme}: [{target}] [{PARMS}] For different network communication methods, what you have to do is to write different URLs and get the type of connection you need by forced conversion. This article does not provide operational operations for data transmission, you can refer to the detailed description of the HTTPConnection interface in the MIDP API document, or refer to the following article http://blog.9cbs.net/mingjava/archive/2004/07 / 07/36528.ASPX Don't forget to close the connection after the data transfer, this time you should use the Close () method defined in the Connection. Try {// Here Close Any Open streams conn.close ();} catch (ooException ie) {// handle the Exception. // throw the exception, ignore it, or return an error.} To this, about GCF's overall introduction It is over. We should clear the relationship between GCF interface hierarchy, master how to use GCF. At the same time, don't forget to remember the design of this framework in fact, in fact, many of the design in CLDC and MIDP is worth thinking about us.