Book Image

Learning Network Programming with Java

Book Image

Learning Network Programming with Java

Overview of this book

Network-aware applications are becoming more prevalent and play an ever-increasing role in the world today. Connecting and using an Internet-based service is a frequent requirement for many applications. Java provides numerous classes that have evolved over the years to meet evolving network needs. These range from low-level socket and IP-based approaches to those encapsulated in software services. This book explores how Java supports networks, starting with the basics and then advancing to more complex topics. An overview of each relevant network technology is presented followed by detailed examples of how to use Java to support these technologies. We start with the basics of networking and then explore how Java supports the development of client/server and peer-to-peer applications. The NIO packages are examined as well as multitasking and how network applications can address practical issues such as security. A discussion on networking concepts will put many network issues into perspective and let you focus on the appropriate technology for the problem at hand. The examples used will provide a good starting point to develop similar capabilities for many of your network needs
Table of Contents (16 chapters)
Learning Network Programming with Java
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

The world is becoming interconnected on an unprecedented scale with more services being provided on the Internet. Applications ranging from business transactions to embedded applications, such as those found in refrigerators, are connecting to the Internet. With isolated applications no longer being the norm, it is becoming increasingly important for applications to be network enabled.

The goal of this book is to provide the reader with the necessary skills to develop Java applications that connect and work with other applications and services across a network. You will be introduced to a wide range of networking options that are available using Java, which will enable you to develop applications using the appropriate technology for the task at hand.

What this book covers

Chapter 1, Getting Started with Network Programming, introduces the essential network terminology and concepts. The networking support that Java provides is illustrated with brief examples. A simple client/server application is presented along with a threaded version of the server.

Chapter 2, Network Addressing, explains how nodes on a network use addresses. How Java represents these addresses is introduced along with support for IPv4 and IPv6. This chapter also covers how Java can configure various network properties.

Chapter 3, NIO Support for Networking, explains how the NIO package provides support for communication using buffers and channels. These techniques are illustrated with a client/server application. The support that NIO provides for asynchronous communication is also demonstrated.

Chapter 4, Client/Server Development, covers how HTTP is an important and widely-used protocol. Java provides support for this protocol in a variety of ways. These techniques are illustrated along with a demonstration of how cookies are handled in Java.

Chapter 5, Peer-to-Peer Networks, discusses how peer-to-peer networks provide a flexible alternative to the traditional client/server architecture. The basic peer-to-peer concepts are introduced along with demonstrations of how Java supports this architecture. FreePastry is used to illustrate one open source peer-to-peer solution framework.

Chapter 6, UDP and Multicasting, explains how UDP is an alternative to TCP. It provides a less reliable but more efficient way for applications to communicate across the Internet. Java's extensive support for this protocol is demonstrated, including NIO support, and how UDP can support streaming media.

Chapter 7, Network Scalability, explains how, as more demands are placed on a server, systems need to scale to address these demands. Several threading techniques supporting this need are demonstrated, including thread pools, futures, and the NIO's selector.

Chapter 8, Network Security, discusses how applications need to protect against a variety of threats. This is supported in Java using encryption and secure hashing techniques. Symmetric and asymmetric encryption techniques are illustrated. In addition, the use of TLS/SSL is demonstrated.

Chapter 9, Network Interoperability, covers how Java applications may need to exchange information with other applications that are written in different languages. The issues that impact an application's interoperability are examined, including byte order. Communication between different implementations is demonstrated using sockets and middleware.

What you need for this book

Java SDK 1.8 is needed for the network programming examples that are encountered in the book. An IDE, such as NetBeans or Eclipse, is recommended. NetBeans IDE 8.0.2 EE edition is used to illustrate the development of a web service.

Who this book is for

This book is for developers who are already proficient in Java and want to learn how to develop network-enabled Java applications. Familiarity with basic Java and object-oriented programming concepts is all that is needed. You will learn the basics of network programming and how to use a multitude of different sockets to create secure and scalable applications.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The SSLSocketFactory class' getDefault returns an SSLSocketFactory instance whose createSocket creates a socket that is connected to the secure echo server."

A block of code is set as follows:

public class ThreadedEchoServer implements Runnable {
    private static Socket clientSocket;

    public ThreadedEchoServer(Socket clientSocket) {
        this.clientSocket = clientSocket;
    }
    ...
}

Any command-line input or output is written as follows:

Enter keystore password:
Re-enter new password:
What is your first and last name?
  [Unknown]:  First Last
What is the name of your organizational unit?
  [Unknown]:  packt
What is the name of your organization?
  [Unknown]:  publishing
What is the name of your City or Locality?
  [Unknown]:  home
What is the name of your State or Province?
  [Unknown]:  calm
What is the two-letter country code for this unit?
  [Unknown]:  me
Is CN=First Last, OU=packt, O=publishing, L=home, ST=calm, C=me correct?
  [no]:  y

Enter key password for <mykey>
        (RETURN if same as keystore password):

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Once NetBeans has been installed, start it and then create a new project from the File | New Project… menu item."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from: https://www.packtpub.com/sites/default/files/downloads/LearningNetworkProgrammingwithJava_Graphics.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.