TCP/IP Sockets in Java: Practical Guide for Programmers (The Practical Guides)

Free download. Book file PDF easily for everyone and every device. You can download and read online TCP/IP Sockets in Java: Practical Guide for Programmers (The Practical Guides) file PDF Book only if you are registered here. And also you can download or read online all Book PDF file that related with TCP/IP Sockets in Java: Practical Guide for Programmers (The Practical Guides) book. Happy reading TCP/IP Sockets in Java: Practical Guide for Programmers (The Practical Guides) Bookeveryone. Download file Free Book PDF TCP/IP Sockets in Java: Practical Guide for Programmers (The Practical Guides) at Complete PDF Library. This Book have some digital formats such us :paperbook, ebook, kindle, epub, fb2 and another formats. Here is The CompletePDF Book Library. It's free to register here to get Book file PDF TCP/IP Sockets in Java: Practical Guide for Programmers (The Practical Guides) Pocket Guide.
UDP Socket Programming in Java Tutorial

Chapter 5 NEW introduces the scalable interface facilities which were introduced in Java 1. Chapter 6 discusses the relationship between the programming constructs and the underlying protocol implementations in more detail. Programming concepts are introduced through simple program examples accompanied by line-by-line code commentary that describes the purpose of every part of the program. No other resource presents so concisely or so effectively the material necessary to get up and running with Java sockets programming. Focused, tutorial-based instruction in key sockets programming techniques allows reader to quickly come up to speed on Java applications.

Concise and up-to-date coverage of the most recent platform 1. Additional Product Features Dewey Edition. Keith Edwards, Professor, Georgia Tech-- "In particular, it's definitely time for an update to this book, since so many changes to the Java platform have happened since the first edition. While I don't see the need to update most books every time there's a minor update, this book is definitely overdue for a revision. To me, one of the strongest points of the book is that it's concise enough to serve as a quick guide and reference to key?

Thus, I think the structure of the book serves audiences who are already good network programmers, or who need a good Java reference, quite well. It is not suited to be a main textbook for a class, and it does not try to do that, But it does do a nice job of succinctly hitting the major points, providing nice examples, as well as a reference for the major important topics. So I see this as a nice book for developers who want to quickly and cheaply master networking Java, as well as a supplemental book for courses in continuing education courses or colleges.

Show More Show Less. Pre-owned Pre-owned. People who bought this also bought. Nonfiction Books. No ratings or reviews yet. Be the first to write a review. Best Selling in Nonfiction See all. We begin with the Logger class, which represents a logging facility that may be local or remote. Through an instance of this class, we can record the various server activities as shown in EchoProtocol. For example, you may have separate loggers for operations, security, and error messages. To get an instance of Logger, call the static factory method Logger.

If a logger by that name does not exist, a new logger is created; otherwise, the existing logger instance is returned. Now that you have logging, what should you log? Well, it depends on what you are doing. If the server is operating normally, you may not want to log every single step the server takes because logging consumes resources such as space for storing log entries and server processor time for writing each entry.

On the other hand, if you are trying to debug, you may want to log each and every step. To deal with this, logging typically includes the notion of the level, or severity, of log entries. The Level class encapsulates the notion of the importance of messages. Each level has an associated integer value, so that levels are comparable and can be ordered. By default, a logger has a single ConsoleHandler that prints messages to System. You can change the handler or add additional handlers to a logger e.

Once we have the logger, we need to … well … log. The entering and exiting methods log entering and exiting the given method from the given class. Note that you may optionally specify additional information such as parameters and return values. The log methods provide a generic logging method where level, message, and optionally exception can be logged.

Note that many other logging methods exist; we are only noting the major types here. We may want to customize our logger by setting the minimum logging level or the handlers for logging messages. The isLoggable method returns true if the given level will be logged by the logger. It is very similar to the iterative server, using a single loop to receive and process client requests. This is possible because EchoProtocol implements the Runnable interface. Thus, when several 4.

Instead, they all appear to receive service albeit at a somewhat slower rate at the same time. ServerSocket; java. Thread Pool Every new thread consumes system resources: spawning a thread takes CPU cycles and each thread has its own data structures e. In addition, when one thread blocks, the JVM saves its state, selects another thread to run, and restores the state of the chosen thread in what is called a context switch.

As the number of threads increases, more and more system resources are consumed by thread overhead. Eventually, the system is spending more time dealing with context switching and thread management than with servicing connections. At that point, adding an additional thread may actually increase client service time. We can avoid this problem by limiting the total number of threads and reusing threads. When a new client connection arrives at the server, it is assigned to a thread from the pool. Connection requests that arrive when all threads in the pool are busy are queued to be serviced by the next available thread.

Like the thread-per-client server, a thread-pool server begins by creating a ServerSocket. Then it spawns N threads, each of which loops forever, accepting connections from the shared ServerSocket instance. When multiple threads simultaneously call accept on the same ServerSocket instance, they all block until a connection is established.

Then the system selects one thread, and the Socket instance for the new connection is returned only in that thread. The other threads remain blocked until the next connection is established and another lucky winner is chosen. Since each thread in the pool loops forever, processing connections one by one, a thread-pool server is really like a set of iterative servers.

Instead, it starts over again, blocking on accept. Logger; 4. Setup: lines 10—20 The port number to listen on and the number of threads are both passed as arguments to main. After parsing them we create the ServerSocket and Logger instances. Note that both have to be declared final, because they are referenced inside the anonymous class instance created below. Create and start threadPoolSize new threads: lines 23—38 For each loop iteration, an instance of an anonymous class that extends Thread is created.

When the start method of this instance is called, the thread executes the run method 84 Chapter 4: Beyond the Basics of this anonymous class. The run method loops forever, accepting a connection and then giving it to EchoProtocol for service. The system ensures that only one thread gets a Socket for any particular connection. If no threads are blocked on accept when a client connection is established that is, if they are all busy servicing other connections , the new connection is queued by the system until the next call to accept see Section 6.

Since we control the maximum number of simultaneously executing threads, we can control scheduling and resource overhead. Of course, if we spawn too few threads, we can still have clients waiting a long time for service; therefore, the size of the thread pool needs to be tuned to the load, so that client connection time is minimized. The ideal would be a dispatching facility that expands the thread pool up to a limit when the load increases, and shrinks it to minimize overhead during times when the load is light. It turns out that Java has just such a facility; we describe it in the next section.

In fact the same thing is true for the dispatching methods themselves. The interface Executor part of the java. For example, if a thread stops because of an uncaught exception or other failure, they automatically spawn a new thread to replace it. ExecutorService also allows for tasks to return a result, through the Callable interface, which is like Runnable, only with a return value. Instances of ExecutorService can be obtained by calling various static factory methods of the convenience class Executors.

TCP/IP Sockets in Java: Practical Guide for Programmers (The Practical Guides)

Executor; java. Executors; java. Setup: lines 11—20 The port is the only argument. We create the ServerSocket and Logger instances as before; they need not be declared final here, because we do not need an anonymous Thread subclass. When its execute method is invoked with a Runnable instance, the executor service creates a new thread to handle the task if necessary.

When a thread has been idle for at least 60 seconds, it is removed from the pool. Once we have a server designed to use Executor for dispatching clients, we can change dispatching strategies simply by changing the kind of Executor we instantiate. Also, tasks are queued inside the Executor, instead of being queued inside the networking system, as they were in our original server.

Data input methods read and receive block if data is not available.

Join our email club...

The accept method of ServerSocket and the Socket constructor both block until a connection has been established see Section 6. Meanwhile, long roundtrip times, high error rate connections, and slow or deceased servers may cause connection 4. Of course, a blocked method call halts progress of the application and makes the thread that is running it useless.

What about a program that has other tasks to perform while waiting for call completion e. These programs may have no time to wait on a blocked method call. What about lost UDP datagrams? Here we explore the various blocking methods and approaches for limiting blocking behavior. Unfortunately, the system-imposed timeout is long, and Java does not provide any means of shortening it. To establish a connection, call the connect method on the newly constructed socket and specify both a remote endpoint and timeout milliseconds.

Thus, the amount of time that a write may block is ultimately controlled by the receiving application. Unfortunately, Java currently does not provide any way to cause a write to time out, nor can it be interrupted by another thread. Therefore, any protocol that sends a large enough amount of data over a Socket instance can block for an unbounded amount of time. The protocol instance keeps track of the amount of time remaining, and uses setSoTimeout to ensure that 88 Chapter 4: Beyond the Basics no read call blocks for longer than that time.

Since there is no way to bound the duration of a write call, we cannot really guarantee that the time limit will hold. Nevertheless, TimelimitEchoProtocol. At the time the handleEchoClient method is invoked, a deadline is computed using the current time and the time bound. After each read , the time between the current time and the deadline is computed, and the socket timeout is set to the remaining time.

Such one-to-one communication is sometimes called unicast. Some information is of interest to multiple recipients. Unicasting multiple copies over a single network connection wastes bandwidth by sending the same information multiple times. For example, if our video server sends 1Mbps streams and its network connection is only 3Mbps a healthy connection rate , we can only support three simultaneous users. Instead of making the sender responsible for duplicating packets, we can give this job to the network. With this model of duplication, the server uses only 1Mbps across its connection to the network, irrespective of the number of clients.

There are two types of one-to-many service: broadcast and multicast. With broadcast, all hosts on the local network receive a copy of the message. With multicast, the message is sent to a multicast address, and the network delivers it only to those hosts that have indicated that they want to receive messages sent to that address. In general, only UDP sockets are allowed to broadcast or multicast.

Note that IPv6 does not explicitly provide broadcast addresses; however, there is a special all-nodes, link-local-scope multicast address, FFO, that multicasts to all nodes on a link. The IPv4 local broadcast address Local broadcast messages are never forwarded by routers. A host on an Ethernet network can send a message to all other hosts on that same Ethernet, but the message will not be forwarded by a router.

There is no networkwide broadcast address that can be used to send a message to all hosts. To see why, consider the impact of a broadcast to every host on the Internet. Sending a single datagram would result in a very, very large number of packet duplications by the routers, and bandwidth would be consumed on each and every network.

The consequences of misuse malicious or accidental are too great, so the designers of IP left such an Internetwide broadcast facility out on purpose. Even so, local broadcast can be very useful. Often, it is used in state exchange for network games where the players are all on the same local broadcast network.

In Java, the code for unicasting and broadcasting is the same. There is one problem. Hint: You cannot use connect with broadcast. Caveat: Some operating systems do not give regular users permission to broadcast, in which case this will not work. The designers of IP allocated a 4. With the exception of a few reserved multicast addresses, a sender can send datagrams addressed to any address in this range.

In Java, multicast applications generally communicate using an instance of MulticastSocket, a subclass of DatagramSocket. Our next examples implement a multicast sender and receiver of vote messages. Every IP datagram contains a TTL, initialized to some default value and decremented usually by one by each router that forwards the packet. When the TTL reaches zero, the packet is discarded. By setting the initial value of the TTL, we limit the distance a packet can travel from the sender. Receivers need some mechanism to notify the network of their interest in receiving data sent to a particular multicast address, so that the network can forward packets to them.

MulticastSocket; java. Multicast datagrams can, in fact, be sent from a DatagramSocket by simply using a multicast address. A multicast receiver, on the other hand, must use a MulticastSocket because it needs the ability to join a group. If we wish to receive any datagrams, we need to join a multicast group. A socket may be a member of multiple groups simultaneously.

Joining a group of which this socket is already a member or leaving a group of which this socket is not a member may generate an exception. A socket with loopback mode enabled will receive the datagrams it sends. The getLoopbackMode and setLoopbackMode methods set the loopback mode 4. The getInterface , setInterface , getNetworkInterface , setNetworkInterface methods set the outgoing interface used in sending multicast packets.

This is primarily used on hosts with multiple interfaces. The default multicast interface is platform dependent. The decision to use broadcast or multicast depends on several factors, including the network location of receivers and the knowledge of the communicating parties. The scope of a broadcast on the Internet is restricted to a local broadcast network, placing severe restrictions on the location of the broadcast receivers.

Multicast communication may include receivers anywhere in the network,2 so multicast has the advantage that it can cover a distributed set of receivers. The disadvantage of IP multicast is that receivers must know the address of a multicast group to join. Knowledge of an address is not required to receive broadcast. In some contexts, this makes broadcast a better mechanism than multicast for discovery.

The semantics of most implementations are such that a UDP datagram will be delivered to all sockets bound to the destination port of the packet. A receiver can use connect to limit the datagram source address and port. Also, a DatagramSocket can specify the local unicast address, which prevents delivery of multicast and broadcast packets. We have already seen an example in our UDP echo client.

Multicast should work if the sender and receivers are on the same LAN.

TCP/IP Sockets in Java: Practical Guide for Programmers (The Practical Guides)

TCP provides a keep-alive mechanism where, after a certain time of inactivity, a probe message is sent to the other endpoint. If the endpoint is alive and well, it sends an acknowledgment. Note that the application only sees keep-alive working if the probes fail. Call the setKeepAlive method with true to enable keep-alive. We talk about this in much greater detail in Section 6.

Why can you only set the 4. Unfortunately, the blocking time is not bounded. We can specify a maximum blocking time for the various operations. A timeout of 0 means the operation never times out. If the timeout expires, an exception is thrown. In the case of UDP multicast, you may have multiple applications on the same host participating in the same multicast group. Unfortunately, you may not be able to wait for the Time-Wait to expire. To enable this, you must allow address reuse. A value of true means that address reuse is enabled. Fortunately, you can disable this behavior.

If you send the data in the output stream, it gets queued up behind all of the regular data, and who knows when the receiver will see it? To deal with this TCP includes the concept of urgent data that can theoretically skip ahead. Such data is called out-of-band because it bypasses the normal stream.

To receive this byte, the receiver must enable out-of-band data by passing true to setOOBInline. The byte is received in the input stream of the receiver. If reception of out-of-band data is not enabled, the urgent byte is silently discarded. Note that Java can get little use from urgent data because urgent bytes are mixed in with regular bytes in the order of transmission. The problem is that your host could then fail at a later time without sending all of the data.

If the timeout expires, the TCP connection is forceably closed. You can control broadcast permissions. As you already know, DatagramSockets provide broadcast service. A value of true means that broadcast is permitted. By default, Java permits broadcast. The number and meaning of the bits depend on the version of IP you are using.

Socket, ServerSocket: Specifying Protocol Preferences void setPerformancePreferences int connectionTime, int latency, int bandwidth The performance preference for the socket is expressed by three integers representing connection time, delay, and bandwith. For example, if connectionTime and latency both equal 0 and bandwidth equals 1, the protocol maximizing bandwidth will be selected. In phone conversations, either side can start the process of terminating the call.

It typically goes something like this: "Well, I guess I'd better go. In the echo protocol, Figure 4. The server then calls close on its socket. Section 6. Once an endpoint client or server closes the socket, it can no longer send or receive data. Closed Compression Server Compression Client immediately closes. HTTP works the same way, except that the server is the terminator. Suppose you want a compression server that takes a stream of bytes, compresses them, and sends the compressed stream back to the client. Which endpoint should close the connection? Since the stream of bytes from the client is arbitrarily long, the client needs to close the connection so that the server knows when the stream of bytes to be compressed ends.

When should the client call close? If the client calls close on the socket immediately after it sends the last byte of data, it will not be able to receive the last bytes of compressed data. Perhaps the client could wait until it receives all of the compressed data before it closes, as the echo protocol does. Unfortunately, neither the server nor the client knows how many bytes to expect, so this will not work either. Fortunately, sockets provide a way to do this. Attempts to write to the stream throw an IOException. Any data written before the call to shutdownOutput may be read by the remote socket.

An application calling shutdownOutput can continue to read from the socket and, similarly, data can be written after calling shutdownInput. In the compression protocol see Figure 4. The server then closes the connection and exits. Our compression client, CompressClient. We discuss and correct this shortcoming in Section 6. FileInputStream; java. Application setup and parameter parsing: lines 17—23 2.

Invoke sendBytes to transmit bytes: line 33 4. To implement the compression server, we simply write a protocol for our threaded server architecture. Our protocol implementation, CompressProtocol. Logger; java. Variables and constructors: lines 10—17 2. The sequence of bytes written to this stream is compressed, using the GZIP algorithm, before being written to the underlying output stream.

Without such restrictions, unsuspecting Web browsers might execute malicious applets that could, for example, send fake email, attempt to hack other systems while the browser user gets the blame, and so on. These security restrictions are enforced by the Java security manager, and violations by the applet result in a SecurityException. Typically, browsers only allow applets to communicate with the host that served the applet.

This means that applets are usually restricted to communicating with applications executing on that host, usually a Web server originating the applet. The list of security restrictions and general applet programming is beyond the scope of this book. It is worth noting, however, that the default security restrictions can be altered, if allowed by the browser user. Suppose that you wanted to implement an applet that allowed users to type and save notes to themselves on their browser.

The server must execute on the Web server that serves the applet to the browser. In addition to these facilities, Java provides several mechanisms not discussed here that operate on top of TCP or UDP and attempt to hide the complexity of protocol development. The URL class and associated classes provide a framework for developing Web-related programs. Many other standard Java library Chapter 4: Beyond the Basics mechanisms exist, providing an amazing range of services.

State precisely the conditions under which an iterative server is preferable to a multiprocessing server. Would you ever need to implement a timeout in a client or server that uses TCP? Determine the minimums for your system. There are two important parts: the java. These are fairly advanced features, with a number of subtle details related to their usage. In Section 5. Then we go back and cover the details of usage of the main abstractions, each in its own section.

Finally, Section 5. Basic Java Sockets work well for small-scale systems. But when it comes to servers that have to deal with many thousands of clients simultaneously, certain issues arise. We saw signs of this in Chapter 4: the thread-per-client approach is limited in terms of scalability because of the overhead associated with creating, maintaining, and swapping between threads.

Using a thread Chapter 5: NIO pool saves on that kind of overhead while still allowing an implementor to take advantage of parallel hardware, but for protocols with long-lived connections, the size of the thread pool still limits the number of clients that can be handled simultaneously. Consider an instant messaging server that relays messages between clients.

Clients must be continuously connected to receive messages, so the thread pool size limits the total number of clients that can be served. Increasing the thread pool size increases the thread-handling overhead without improving performance, because most of the time clients are idle.

If this were all there is to it, NIO might not be needed. Unfortunately, there are other, more subtle challenges involved with using threads for scalability. One is that the programmer has very little control over which threads receive service when. Think of a service that allows citizens to reserve parking spaces for one-hour blocks in a big city, for example. The schedule of who gets which space for which time blocks must be kept consistent; the server may also need to ensure that the same user does not reserve more than one space at a time. These constraints require that some state information i.

This in turn requires that access to that state be carefully synchronized through the use of locks or other mutual exclusion mechanisms. Because of these complications, some programmers prefer to stick with a single-threaded approach, in which the server has only one thread, which deals with all clients—not sequentially, but all at once. We saw an example of this in Chapter 4, where we set a timeout on the accept operation via the setSoTimeout method of ServerSocket.

This allows a single thread to handle multiple connections. Channels can register an instance of class Selector. The other major feature introduced in NIO is the Buffer class. The bad thing is that implementing that illusion may require either lots of memory allocation or lots of context-switching, or both. As with threads, this overhead is buried in the implementation, and is therefore not controllable or predictable. That approach makes it easy to write programs, but harder to tune their performance.

There are two main advantages to using Buffer. This represents extra work, but you the programmer control how, whether, and when it happens. A smart programmer, who knows the application requirements well, can often reduce overhead by tweaking these choices. There are other types of channels for other devices e. For example, a call to accept can block waiting for a client to connect; a call to read can block until data arrives from the other end of a connection. A slow, lossy, or just plain broken network can cause an arbitrary delay.

The return value of such a call indicates the extent to which the requested operation was achieved. With a nonblocking channel, these operations return immediately. InetSocketAddress; java. ByteBuffer; java. Get and convert arguments: lines 9—16 2. Create nonblocking SocketChannel: lines 19—20 3. Connect to server: lines 23—27 Because the socket is nonblocking, the call to connect may return before the connection is established; the method returns true if the connection completes before it returns, false otherwise.

The print operation demonstrates that we can perform other tasks while waiting for the connection to complete. Such a busy wait is generally wasteful; we do it here to illustrate the use of the methods. The call to read does not block but rather returns 0 when no data is available to return.

Special order items

Again, the print operation demonstrates that we can perform other tasks while waiting for the communication to complete. Print the received data: lines 43—44 7. Close the channel: line 45 Like sockets, channels should be closed when they are no longer needed. Consider an Instant Messaging server, for example. Thousands of clients may be connected, but only a few possibly none have messages waiting to be read and relayed at any time. NIO selectors do all of this. To use a selector, create it using the static factory method open and register it with the channels that you wish to monitor note that this is done via a method of the channel, not the selector.

Suppose we want to implement an echo server using channels and a selector without using multiple threads or busy waiting. SelectionKey; import java. We create a selector and register it with a ServerSocketChannel for each socket on which the server listens for incoming client connections. SelectionKey; java. Selector; java. ServerSocketChannel; java. The returned key is ignored listnChannel. Setup: lines 14—19 Verify at least one argument, create a Selector instance.

Any argument other than a number in the appropriate range will result in an IOException. Create protocol handler: line 31 To get access to the handler methods for the Echo protocol, we create an instance of the EchoSelectorProtocol, which exports the required methods. It returns the number of ready channels; zero indicates that the timeout expired, in which case we print a dot to mark the passage of time and iterate.

Tcp/IP Sockets in C: Practical Guide for Programmers by Michael J. Donahoo

Therefore if we do not remove each key as we process it, it will remain in the set across the next call to select , and a useless operation may be invoked on it. All protocol details are contained in the implementation of the TCPProtocol interface. EchoSelectorProtocol provides an implementation of the handlers for the Echo protocol. You could easily write your own protocol handlers for other protocols or performance improvements on our Echo protocol handler implementation.

SocketChannel; java. Declaration of implementation of the TCPProtocol interface: line 6 2. The accept method returns a SocketChannel for the incoming connection. We create a new ByteBuffer of the required size, and pass it as argument to register. It will be associated as an attachment to the SelectionKey instance returned by the register method. This is explained in detail in the next section. We are now ready to delve into the details of the three main NIO abstractions.

So what good are IntBuffer, DoubleBuffer, and the others? Stay tuned! The answer will be revealed in Section 5. Java provides two convenience methods for evaluating this distance. ByteBuffer: Remaining Bytes boolean hasRemaining int remaining hasRemaining returns true if at least one element is available, and remaining returns the number of elements available. The static factory methods for creating a ByteBuffer are shown in Table 5. Fill array Such copying can get expensive, especially if there are many reads and writes requiring copying.

ByteBuffer: Getting and putting bytes Relative: byte get ByteBuffer get byte[] dst ByteBuffer get byte[] dst, int offset, int length ByteBuffer put byte b ByteBuffer put byte[] src ByteBuffer put byte[] src, int offset, int length ByteBuffer put ByteBuffer src Absolute: byte get int index ByteBuffer put int index, byte b There are two types of get and put : relative and absolute.

However, if doing so would cause position to go past limit, a get throws a BufferUnderflowException, while a put throws a BufferOverflowException. However, if doing so would cause position to exceed limit, a BufferUnderflowException get or BufferOverflowException put is thrown: partial gets and puts are not allowed. In fact, they return the same instance of ByteBuffer that was passed as an argument. By default Java uses big-endian. The second allows you to set the byte order used to write multibyte quantities.

  • The Colonial Rise of the Novel!
  • Our Bodies, Our Crimes: The Policing of Women’s Reproduction in America (Alternative Criminology).
  • When I Fall in Love (MacLeod series)!
  • SPSS Exact Tests 7.0 for Windows.
  • French Foreign Legion.
  • See a Problem?.
  • An ethic for enemies: forgiveness in politics.

Continuing the example above, after clear the situation looks like this: 5. The rewind method sets position to zero and invalidates the mark. When might you use this? This is exactly the problem compact is designed to solve. Think of this as an alternate perspective on the same data. Table 5. Channel reads and writes take only ByteBuffers; however, we may be interested in communicating using other primitive types. Suppose you have received via some Channel a message that consists of a single byte, followed by a number of two-byte integers i. You might call buf. This includes all forms of put , compact , etc.

To use this facility, you need to know about two additional classes in the java. If the input is completely consumed and the encoder is ready for more, CoderResult. Like its Socket counterpart, a SocketChannel is a communication channel for connected endpoints. The parameterless form of open creates an unconnected SocketChannel, which may be connected to an endpoint with the connect method.

This will allow you to call basic Socket methods to bind, 5. Like its ServerSocket counterpart, a ServerSocketChannel is a channel for listening for client connections. Each instance wraps an instance of ServerSocket, which you can access using the socket method. As illustrated in the earlier examples, you must access the underlying ServerSocket instance to bind it to a desired port, set any socket options, etc. After creating and binding, you are ready to accept client connections by calling the accept method, which returns the new, connected SocketChannel.

You will therefore almost always be setting your channels to be nonblocking. Consider setting up a connection for a SocketChannel. If you give the open factory method of SocketChannel a remote address, the call blocks until the connection completes. If the connection can be made without blocking, connect returns true ; otherwise, you need some way to determine when the socket becomes connected.

The finishConnect method provides a way to check the status of an in-progress connection attempt on a nonblocking socket, or to block until 5. The isConnected method allows you to determine whether the socket is connected so you can avoid having a NotYetConnectedException thrown say, by read or write. You can use isConnectionPending to check whether a connection has been initiated on this channel.

Here we consider some of the details. Selector: Creating and closing static Selector open boolean isOpen void close You create a selector by calling the open factory method. You can tell whether a selector has been closed yet by calling isOpen. The association between a Selector and a Channel is represented by an instance of SelectionKey.

Note that a Channel instance can register more than one Selector instance, and so can have more than one associated instance of SelectionKey. The SelectionKey maintains information about the kinds of operations that are of interest for a channel in a bitmap, which is just an int in which individual bits have assigned meanings.

In good all round condition. Book has minor flaw, like bent or scratched cover. Total Satisfaction Guaranteed. Quick Handling. We work every day but Sunday. Standard delivery takes business days. No International ShippingGood condition. Quick ship! Used book doesn't include any access code, CD or other supplements.

Calvert, Michael J. Ships with Tracking Number! May not contain Access Codes or Supplements. May be ex-library. Buy with confidence, excellent customer service! We're sorry - this copy is no longer available. More tools Find sellers with multiple copies Add to want list. Didn't find what you're looking for?

Add to want list.