[ Pobierz całość w formacie PDF ]
.DatagramSocketA DatagramSocket provides an unreliable, connectionless, datagram connection (that is, a UDP/IP socket connection).Unlike the reliable connection provided by a Socket, there is no guarantee that what you send over a UDP connection actually gets to the receiver.The TCP connection provided by the Socket class takes care retransmitting any packets that might get lost.Packets sent through UDP simply are sent out and forgotten, which means that if you need to know that the receiver got the data, you will have to send back some sort of acknowledgment.This does not mean that your data will never get to the other end of a UDP connection.If a network error happens (your cat jiggles the Ethernet plug out of the wall, for instance) then the UDP layer will not try to send it again or even know that the packet did not get to the recipient.Connectionless means that the socket does not have a fixed receiver.You may use the same DatagramSocket to send packets to different hosts and ports, whereas a Socket connection is only to a given host and port.Once a Socket is connected to a destination it cannot be changed.The fact that UDP sockets are not bound to a specific destination also means that the same socket can listen for packets as well as originating them.There is no UDP DatagramServerSocket equivalent to the TCP ServerSocket.Datagram refers to the fact that the information is sent as discrete packets rather than a continuous ordered stream.The individual packet boundaries are preserved.It might help to think of it as dropping fixed-size postcards in a mailbox.For example, if you send four packets, the order in which they arrive at the destination is not guaranteed to be the same in which they were sent.The receiver could get them in the same order they were sent or they could arrive in reverse order.In any case, each packet will be received whole.Given the above constraints, why would anyone want to use a DatagramSocket? There are several advantages to using UDP, as follows:lYou need to communicate with several different hosts.Because a DatagramSocket is not bound to a particular host, you may use the same object to communicate with different hosts by specifying the InetAddress when you create each DatagramPacket.llYou are not worried about reliable delivery.If the application you are writing does not need to know that the data it sends get to the other end, using a UDP socket eliminates the overhead of acknowledging each packet that TCP does.Another case would be if the protocol you are using has its own method of handling reliable delivery and retransmission.llThe amount of data being sent does not merit the overhead of setting up a connection and the reliable delivery mechanism.An application that is only sending 100 bytes for each transaction every 10 minutes would be an example of this situation.lThe NFS (Network File System) protocol version two, originally developed by Sun with implementations available for most operating systems, is an example application that uses UDP for its transport mechanism.Another example of an application where a DatagramSocket might be appropriate would be a multiplayer game.The central server would need to communicate to all of the players involved, and would not necessarily need to know that a position update got to the player.NOTEAn actual game that uses UDP for communication is Netrek, a space combat simulation loosely based on the Star Trek series.Information on Netrek can be found using the Yahoo subject catalog at.There is also a Usenet newsgroup, news:rec.games.netrek.Decisions, DecisionsNow that you know what the classes are capable of, you can choose the one that best fits your application.Table 29.1 sums up what type of connection each of the base networking classes creates.The direction column indicates where a connection originates: “Outgoing” indicates that your application is opening a connection out to another host; and “Incoming” indicates that some other application is initiating a connection to yours.Table 29.1.Low-level connection objects summarized.ClassConnection TypeDirectionSocketConnected, ordered byte stream (TCP)OutgoingServerSocketConnected, ordered byte stream (TCP)IncomingDatagramSocketConnectionless datagram (UDP)Incoming or OutgoingYou need to look at the problem you are trying to solve, any constraints you have, and the transport mechanism that best fits your situation.If you are having problems choosing a transport protocol, take a look at some of the RFCs that define Internet standards for applications (such as HTTP or SMTP).One of them might be similar to what you are trying to accomplish.As an alternative, you could be indecisive and provide both TCP and UDP versions of your service, duplicating the processing logic and customizing the network logic.Trying both transport protocols with a pared-down version of your application might give you an indication that better serves your purposes.Once you've looked at these factors you should be able to decide what class to use.A Note on Java Security and the Network ClassesOne of the purposes of Java is to enable executable content from an arbitrary network source to be retrieved and run securely.To enable this, the Java runtime enforces certain limitations on what classes obtained through the network may do.You should be aware of these constraints because they will affect the design of applets and how the applets must be loaded.You will need to take into consideration whatever security constraints are imposed by your target environment and your development environment, as well, when designing your application or applet.For example, Netscape Navigator 2.0 allows code loaded from local disk more privileges than code loaded over a network connection.A class loaded from an HTTP daemon may only create outgoing connections back to the host from which it was loaded.If the class had been loaded from the local host (that is, it was located somewhere in the class search path on the machine running Navigator), it would be able to connect to an arbitrary host.Contrast this with the appletviewer provided with Sun's Developer's Kit
[ Pobierz całość w formacie PDF ]
Tematy
Indexinformatyka programowanie uslug wcf wydanie iii juval l wy ebook
Wheeler Secure Programming for Linux and UNIX HOWTO (2003)
Visual Studio 2005 Programowanie z Windows API w jezyku C
Wprowadzenie do programowania Neurolingwistycznego J O'Connor, J Seymour
Linux Programmer s Guide PL P.Niewiadomski
Kernighan The C Programming Language 2e
Jezyk C Nowoczesne programowanie Wydanie II
Micuno Luigi Zorro JeÂździec w Masce 01
This Man Confessed Malpas, Jodi Ellen
Chester Himes Il Pleut des Coups Durs