When a program reads from a BufferedReader, text is taken from the buffer rather than directly from the underlying input stream or other text source. When the buffer empties, it is filled again with as much text as possible, even if not all of it is immediately needed. This will make future reads much faster. When a program writes to a BufferedWriter, the text is placed in the buffer.
|Published (Last):||3 March 2017|
|PDF File Size:||7.20 Mb|
|ePub File Size:||20.6 Mb|
|Price:||Free* [*Free Regsitration Required]|
Clients and servers are connected by sockets. A server, rather than connecting to a remote host, a program waits for other hosts to connect to it. Server Sockets A server socket binds to a particular port on the local machine. Once it has successfully bound to a port, it listens for incoming connection attempts. When a server detects a connection attempt, it accepts the connection. This creates a socket between the client and the server over which the client and the server communicate. Multiple Clients Multiple clients can connect to the same port on the server at the same time.
Incoming data is distinguished by the port to which it is addressed and the client host and port from which it came. The server can tell for which service like http or ftp the data is intended by inspecting the port. It can tell which open socket on that service the data is intended for by looking at the client address and port stored with the data. Queueing Incoming connections are stored in a queue until the server can accept them.
On most systems the default queue length is between 5 and Once the queue fills up further incoming connections are refused until space in the queue opens up. The java. ServerSocket Class The java. ServerSocket class represents a server socket. A ServerSocket object is constructed on a particular local port. Then it calls accept to listen for incoming connections.
Then accept returns a java. Socket object that performs the actual communication with the client. If another server socket is already listening to the port, then a java. BindException, a subclass of IOException, is thrown. No more than one process or thread can listen to a particular port at a time.
This includes non-Java processes or threads. Example On Unix systems but not Windows or the Mac your program must be running as root to bind to a port between 1 and It tells Java to pick an available port. The getLocalPort method tells you what port the server socket is listening on. This is useful if the client and the server have already established a separate channel of communication over which the chosen port number can be communicated.
By default, a server socket binds to all available IP addresses on a given port. Threading No more than one server socket can listen to a particular port at one time. Since a server may need to handle many connections at once, server programs tend to be heavily multi-threaded. Generally the server socket passes off the actual processing of connections to a separate thread. There should be a loop which continually accepts new connections.
Rather than handling the connection directly the socket should be passed to a Thread object that handles the connection. Every time a connection is finished the thread is disposed of. Spawning a new thread for each connection takes a non-trivial amount of time, especially on a heavily loaded server.
It would be better not to spawn so many threads. Thread Pools Create a pool of threads when the server launches, store incoming connections in a queue, and have the threads in the pool progressively remove connections from the queue and process them.
The main change you need to make to implement this is to call accept in the run method rather than in the main method. Setting Server Socket Options There are three methods to set and get various options. The defaults are generally fine.
Java Network Programming, 4th Edition
Java Network Programming, Part 2: Sockets, Server Sockets, and UDP
Java Network Programming, Second Edition by Elliotte Rusty Harold