java is the first programming language designed from the ground up with networking in mind. a network is a collection of
computers and other devices that can send data to and receive data from each other,more or less in real time. a network
is normally connected by wires and the bits of data are turned into electromagnetic waves that move through the wires. the
host-to-network layer defines how a particular network interface send ip datagrams over its physical connection to the local
network and the world. the part of the host -to-network layer made up of the hardware used to connect different computers.
a network layer protocol defines how bits and bytes of data are organized into large group called packets and the
addressing shcema by which different machines find each other. the transport layer is responsible for ensuring that
packets are received in the order they were sent and making sure that no data is lost or corrupted. the hypertext transfer
protocol is a standard that defines how a web client talks to a server and how data is transfered from the server back to
the client. http relies heavily on two other standards the multipurpose internet mail extensions and the hypertext markup
lanuage. mime is a way to encode different kinds of data to be transmitted over a 7-bits ascii connection it also lets the
recipeint know what kind of data has been sent.
i/o in java is built on streams. inputs streams read data; output streams wrtite data. different fundamental steam classes
such java.io.fileinputstream and sun.net.telnetoutputstream read and write particulart sources of data. however, all
fundamental output streams have the same basic methods to write data and all fundamental input streams use the same
basic methods to read data. filter streams can be chained to either an input stream or an output stream. filters can modify
the data as it is read or wriiten. readers and writes can be chained to input and output streams to allow programs to read
and write text rather than bytes. there are at least two solutions to this problem. this first is to reuse processes rather than
spawning new ones. the second solution to this problem is to use lightweight threads to handle connections instead of
using heavyweight processes. the simplest way to implement a thread pool is by using a fixed number of threads set when
the pool is first created.
the java.net.inetaddress class is java's encapsulation of an ip address. this class represents an internet address as two
fields;hostname and address. hostname contains the name of the host. the simplest way for a java program to locate and
retrieve data from the network is to use the url class, alithough java can handle only a few protocols and content types out
of the box. the java.net.url class is an abstract of a uniform resource locator. it extends java.lang.object and it is a final
class that can not be subclassed. protocol handlers are the strategies and the url class itself froms the context through which the different strategies are selected. it is helpful to think of url as objkects with fields that include the protocol,hostname,port,path,query string and ref. the java.net.urlencoder class contains a single static method called encode() that encodes a string according to these rules. data is transimitted across the internet in packets of finite size called datagrams. each datagram contains a header and a payload. the header contains the address and port to which the packet is going, the address and port from which the packet came and various other housekeeping information used to ensure reliable transimission. the payload contains the data itself. sockets are an innovation of berkeley unix that allow the programmer to treat a network connection as just another steam onto which bytrs can be written and from which bytes can be read. sockets are an extension of one of unix's most important ideas: that all i/o should look like file i/o to the programmer whether you are working with a keyboard or a network connection. a socket is a connection between two hosts. it can peform seven basic operations: conect to a remote machine,send data; receive data;close a connection; bind to a port;listen for incoming data; accept connections from remote machines on the bound port. the java.net.socket class is java's fundamental class for performaing client_side tcp operations. other client-oriented classes that make tcp network connections all ultimately end up invokong the methods of this class. this class itself use native code to communicate with the local tcp stack of the host operating system. the method of the socket class set up and tear down connections and set as various socket options. java provides a serversocket class to allow programmers to write servers. bascially, a server ' s job is to sit by the phone and waiting for incoming calls. more technically, a serversocket runs on a particular poert on the server machine. when a client socket on a remote host attempts to connect to that port, the server wakes up,negotiates the connection between the client and the servr and opne a regular socket between the two hosts. in other words, srver sockets wait for connection while client sockets initiate connections. once the server socket has set up the connection, the server use a regular socket object to send dat to the client. data always travels over the regular socket.
the seversocket class containsw everything you need to write servers in java. it has constructors that create new serversocket objects,method that listen for connections on a specified port and methods that returns a socket object when a connection is made so that you can send and receive data. the operating system stores incoming connection requests addressed to a particular port in a first-in,first-out queue.
the sslsocket class has a number of methods for configuring exactly how much and what kind of authentication and encryption is performed. different implementation of the jsse support different combinations of authentication and encrytion algorithms. the base java.net.urlconnection class is abstract to implement a specific protocol ,you write a subclass. these subaclsses can be loaded at runtime by your own applications or by the hotjava browser.
java divides the task of handling protocol into a number of pieces. as a result ,there is no single class called protocolhandler. instead,pieces of the protocol hanlder mechanism are implemented by four different classed in the java.net package. a content hanlder is an instance of a subclass of java.net.contenthanlder. this class knows how to take a utrlconnection and a mime type and turn the data coming from the urlconnection into a java object of an appropriate type.
rmi is a core java api and class library that allows java programs to call certain methods on a remote server. furthermore, the methods running on the server can invoke methods in objects on the client. return values and arguments can be passed back and forth in both directions. a remote object lives on a server. each remote object implements a remote interface that specifies which of its method cane invoked by clients. clients invoke the methods of the remote object almost exactly as they invoke local methods. the stub is a local object that implements the remote interfaces of the remote object; this means that the stub has methods matching the signatures of all the methods the remote object exports. in effect, the client thinks it is calling a method in the remote object,but it is really calling an equivalent method in the stub. stubs are used in the client 's virtual machine in place of the real objects and methods that live on the server. it is helpful to think of the stub as the remote object's surrogate on the client. when the client invokes a method, the stub passes the invocation to the remote reference layer. the remote reference layer carries out a specific remote referernce protocol which is independent of the specific client stubs and server skeletons. in essence, the remote reference layer translates the local reference to the stub into a remote reference to the object on the server whatever the syntax or semantics of the remote reference may be, then it passes the invocation to the transport layer. the transport layer sends the invocation across the internet. on the server side ,the transport ;layer listens for incoming connections,upon receiving an invocation, the transport layer frowards it to the remote reference layer on the server. the remote reference layer converts the remote references sent by the client into referemnce for the local virtual machine. then it passes the request to the skeleton. the skeleton reads the arguments and passes the data to the server program which makes the actual method call. if the method call return a value, that value is sent down through the skeleton,remore reference and transport layers on the server side across the internet and then up throught the transport,remote reference and stub layers on the client side. the java.rmi package defines the classes interfaces and exceptions that will be seen on the client side. the java.rmi.server package defines the classes,interfaces and exceptions that will be visble on the server side. the java.rmi.registry package defines the classes,interfaces and exceptions that are used to locate and name remote objects.
to create a new remote object ,you first define an interface that extends the java.rmi.remote interface. the remote interface does not have any methods of its own; its sole purpose is to tag remore objects so that they can be identified as such. your subeinterface of remote determines whick methods of the remote object may be called by clients.
本文详细介绍了Java网络编程的基础概念和技术要点,包括网络模型、URL使用、Socket编程、HTTP协议及其实现方式等。此外还深入探讨了Java中如何处理输入输出流、线程池管理以及SSL加密等内容。
407

被折叠的 条评论
为什么被折叠?



