Java Network Programming

See RMI (Remote Method Invocation) on this website


Reilly, D. and Reilly, M. (2002) Java Network Programming and Distributed Computing, Addison-Wesley

Download eBook PDF (PDF 2,372KB)
Download Source Code (ZIP 121KB)
Companion Web Site: http://www.davidreilly.com/jnpbook/

Java’s rich, comprehensive networking interfaces make it an ideal platform for building today’s networked, Internet-centered applications, components, and Web services. Now, two Java networking experts demystify Java’s complex networking API, giving developers practical insight into the key techniques of network development, and providing extensive code examples that show exactly how it’s done. David and Michael Reilly begin by reviewing fundamental Internet architecture and TCP/IP protocol concepts all network programmers need to understand, as well as general Java features and techniques that are especially important in network programming, such as exception handling and input/output. Using practical examples, they show how to write clients and servers using UDP and TCP; how to build multithreaded network applications; and how to utilize HTTP and access the Web using Java. The book includes detailed coverage of server-side application development; distributed computing development with RMI and CORBA; and email-enabling applications with the powerful JavaMail API. For all beginning to intermediate Java programmers, network programmers who need to learn to work with Java.

What you’ll learn

Java provides a rich and comprehensive networking interface, to a range of network protocols. Navigating through the networking API, and learning the theory that accompanies successful network programming, can be tricky though. In Java Network Programming & Distributed Computing, you’ll learn the easy way with easy-to-follow coverage of essential concepts, diagrams, and sample source code.

Along the way, you’ll learn:

how the Internet works, its architecture, and the TCP/IP protocol stack
an overview of the Java programming language, and a refresher course on topics such as exception handling
how Java’s input/output system works
how to write clients and servers using the User Datagram Protocol (UDP) and Transport Control Protocol (TCP)
how to write multi-threaded applications
how to implement network protocols, and see examples of client/server implementations
about the Hyper-Text Transfer Protocol (HTTP), and how to access the World Wide Web using Java
how to write server-side Java applications for the WWW
about distributed computing technologies like Remote Method Invocation (RMI) and CORBA
how to access e-mail using the extensive JavaMail API

Table of Contents

1. Networking Theory.

What Is a Network?How Do Networks Communicate?Communication across Layers.Advantages of Layering.Internet Architecture.Internet Application Protocols.TCP/IP Protocol Suite Layers.Security Issues: Firewalls and Proxy Servers.Summary.

2. Java Overview.
What Is Java?The Java Programming Language.The Java Platform.The Java Application Program Interface.Java Networking Considerations.Applications of Java Network Programming.Java Language Issues.System Properties.Development Tools.Summary.

3. Internet Addressing.
Local Area Network Addresses.Internet Protocol Addresses.Beyond IP Addresses: The Domain Name System.Internet Addressing with Java.Summary.

4. Data Streams.
Overview.How Streams Work.Filter Streams.Readers and Writers.Object Persistence and Object Serialization.Summary.

5. User Datagram Protocol.
Overview.DatagramPacket Class.DatagramSocket Class.Listening for UDP Packets.Sending UDP Packets.User Datagram Protocol Example.Building a UDP Client/Server.Additional Information on UDP.Summary.

6. Transmission Control Protocol.
Overview.TCP and the Client/Server Paradigm.TCP Sockets and Java.Socket Class.Creating a TCP Client.ServerSocket Class.Creating a TCP Server.Exception Handling: Socket Specific Exceptions.Summary.

7. Multi-threaded Applications.
Overview.Multi-threading in Java.Synchronization.Interthread Communication.Thread Groups.Thread Priorities.Summary.

8. Implementing Application Protocols.
Overview.Application Protocol Specifications.Application Protocols Implementation.Summary.

9. HyperText Transfer Protocol.
Overview.HTTP and Java.The Common Gateway Interface (CGI).Summary.

10. Java Servlets.
Overview.How Servlets Work.Using Servlets.Running Servlets.Writing a Simple Servlet.SingleThreadModel.ServletRequest and HttpServletRequest.ServletResponse and Http Response.ServletConfig.ServletContext.Servlet Exceptions.Cookies.HTTP Session Management in Servlets.Summary.

11. Remote Method Invocation (RMI).
Overview.How Does Remote Method Invocation Work?Defining an RMI Service Interface.Implementing an RMI Service Interface.Creating Stub and Skeleton Classes.Creating an RMI Server.Creating an RMI Client.Running the RMI System.Remote Method Invocation Packages and Classes.Remote Method Invocation Deployment Issues.Using Remote Method Invocation to Implement Callbacks.Remote Object Activation.Summary.

12. Java IDL and CORBA.
Overview.Architectural View of CORBA.Interface Definition Language (IDL).From IDL to Java.Summary.

13. JavaMail.
Overview.Installing the JavaMail API.Testing the JavaMail Installation.Working with the JavaMail API.Advanced Messaging with JavaMail.Summary.


24.10 Case Study – DeitelMessenger Server and Client

From Deitel, H. and Deitel, P.  (2012) Java – How To Program, Ninth Edition, Pearson Education

Download PDF
Download Example Code
(ZIP)


The Java™ Tutorials – Oracle Java Documentation – Trail: Custom Networking
(https://docs.oracle.com/javase/tutorial/networking/index.html)

The Java platform is highly regarded in part because of its suitability for writing programs that use and interact with the resources on the Internet and the World Wide Web. In fact, Java-compatible browsers use this ability of the Java platform to the extreme to transport and run applets over the Internet.

This trail walks you through the complexities of writing Java applications and applets that can be used on the Internet.

Trail icon Overview of Networking has two sections. The first describes the networking capabilities of the Java platform that you may already be using without realizing that you are using the network. The second provides a brief overview of networking to familiarize you with terms and concepts that you should understand before reading how to use URLs, sockets, and datagrams.

Trail icon Working With URLs discusses how your Java programs can use URLs to access information on the Internet. A URL (Uniform Resource Locator) is the address of a resource on the Internet. Your Java programs can use URLs to connect to and retrieve information over a network. This lesson provides a more complete definition of a URL and shows you how to create and parse a URL, how to open a connection to a URL, and how to read from and write to that connection.

Trail icon All About Sockets explains how to use sockets so that your programs can communicate with other programs on the network. A socket is one endpoint of a two-way communication link between two programs running on the network. This lesson shows you how a client can connect to a standard server, the Echo server, and communicate with it via a socket. It then walks you through the details of a complete client/server example, which shows you how to implement both the client side and the server side of a client/server pair.

Trail icon All About Datagrams takes you step by step through a simple client/server example that uses datagrams to communicate. It then challenges you to rewrite the example using multicast socket instead.

Trail icon Programmatic Access to Network Parameters explains why you might want to access network interface parameters and how to do so. It gives examples of how to list all the IP addresses assigned to the machine as well as other useful information such as whether the interface is running.

Trail icon Working With Cookies discusses how cookies are used to create a session between a client and server, and how you can take advantage of cookies in your HTTP URL connections.


tutorialspointJava – Networking
(http://www.tutorialspoint.com/java/java_networking.htm)

Download the tutorial: Java – Networking (Socket Programming) (PDF)

See example of GreetingClient / GreetingServer in this tutorial.

The term network programming refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network.
The java.net package of the J2SE APIs contains a collection of classes and interfaces that provide the low-level communication details, allowing you to write programs that focus on solving the problem at hand.
The java.net package provides support for the two common network protocols:

  • TCP: TCP stands for Transmission Control Protocol, which allows for reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP.
  • UDP: UDP stands for User Datagram Protocol, a connection-less protocol that allows for packets of data to be transmitted between applications.

This tutorial gives good understanding on the following two subjects:

  • Socket Programming: This is most widely used concept in Networking and it has been explained in very detail.
  • URL Processing: This would be covered separately. Click here to learn about URL Processing in Java language.

Lesson: All About Sockets – The Java™ Tutorials
(https://docs.oracle.com/javase/tutorial/networking/sockets/)

URLs and URLConnections provide a relatively high-level mechanism for accessing resources on the Internet. Sometimes your programs require lower-level network communication, for example, when you want to write a client-server application.
In client-server applications, the server provides some service, such as processing database queries or sending out current stock prices. The client uses the service provided by the server, either displaying database query results to the user or making stock purchase recommendations to an investor. The communication that occurs between the client and the server must be reliable. That is, no data can be dropped and it must arrive on the client side in the same order in which the server sent it.
TCP provides a reliable, point-to-point communication channel that client-server applications on the Internet use to communicate with each other. To communicate over TCP, a client program and a server program establish a connection to one another. Each program binds a socket to its end of the connection. To communicate, the client and the server each reads from and writes to the socket bound to the connection.


Java Socket Programming Examples
(http://cs.lmu.edu/~ray/notes/javanetexamples/)

The four applications are

  • A trivial date server and client, illustrating simple one-way communication. The server sends data to the client only.
  • A capitalize server and client, illustrating two-way communication. Since the dialog between the client and server can comprise an unbounded number of messages back and forth, the server is threaded to service multiple clients efficiently.
  • A two-player tic tac toe game, illustrating a server that needs to keep track of the state of a game, and inform each client of it, so they can each update their own displays.
  • A multi-user chat application, in which a server must broadcast messages to all of its clients.

HOW-TO: Sockets programming in Java: A tutorial
(http://www.javaworld.com/article/2077322/core-java/core-java-sockets-programming-in-java-a-tutorial.html)

Write your own client/server applications seamlessly in Java.
This classic JavaWorld tutorial presents an introduction to sockets programming over TCP/IP networks and demonstrates how to write client/server applications in Java.


Javatpoint LogoJava Socket Programming
(http://www.javatpoint.com/socket-programming)

Java Socket programming is used for communication between the applications running on different JRE.
Java Socket programming can be connection-oriented or connection-less.
Socket and ServerSocket classes are used for connection-oriented socket programming and DatagramSocket and DatagramPacket classes are used for connection-less socket programming.
The client in socket programming must know two information:

  1. IP Address of Server, and
  2. Port number.

Complete Java Networking Explained with Simple Examples
(http://www.go4expert.com/articles/complete-java-networking-explained-t21100/)

In Java, networking is performed using Transmission Control Protocol/Internet Protocol (TCP/IP) or the User Datagram Protocol (UDP).

Java provides a low-level control on the connection, allowing you to customize it according to your requirements. The package containing networking classes, called java.net, is shipped as part of the Java Development Kit (JDK). In Java, TCP connections are established using sockets and server sockets. These connections allow you to send and receive data to a Web Site. On the other hand, broadcast messages known as datagrams use the UDP. Datagram’s are encapsulated in a datagram packet and sent using a datagram socket. URLs can be created and modified using the URL class methods of Java.


A Tutorial on Socket Programming in Java

We present a tutorial on socket programming in Java. This tutorial illustrates several examples on the two types of socket APIs: connectionless datagram sockets and connection-oriented stream-mode sockets. With datagram sockets, communication occurs in the form of discrete messages sent from the sender to receiver; whereas with stream-mode sockets, data is transferred using the concept of a continuous data stream flowing from a source to a destination. For both kinds of sockets, we illustrate examples for simplex (one-way) and duplex (bi-directional) communication. We also explain in detail the difference between a concurrent server and iterative server and show example programs on how to develop the two types of servers, along with an example. The last section of the tutorial describes the Multicast Socket API in Java and illustrates examples for multicast communication. We conclude by providing descriptions for several practice programming exercises that could be attempted at after reading the tutorial.

Download PDF


Manuel Oriol, Techniques of Java Programming: Sockets in Java, May 10, 2006

Sockets are probably one of the features that is most used in current world. As soon as people want to deal with the network in a program, sockets are used. As Java is a post-Internet language, sockets have been integrated in the standard API and their use is very simple. Not that sockets-related classes are located in the java.net package. In this chapter, we explain how it works in Java and show examples of sockets. Section 2 gives some background on network. Section 3 shows how to use and build TCP sockets. Section 4 shows how to use and build UDP sockets.

Download PDF


Network Programming Notes
(http://www.cs.usfca.edu/~parrt/doc/java/Sockets-notes.pdf)

Topics in this section include:
  • What a socket is
  • What you can do with a socket
  • The difference between TCP/IP, UDP/IP and Multicast sockets
  • How servers and clients communicate over sockets
  •  How to create a simple server
  •  How to create a simple client
  •  How to create a multithreaded server

Download PDF


Socket Programming (Chapter 13)
(http://www.buyya.com/java/Chapter13.pdf)

From: Buyya, Selvi and Chu (2009) “Object Oriented Programming with Java: Essentials and Applications”, TBS

This chapter presents key concepts of intercommunication between programs running on different computers in the network. It introduces elements of network programming and concepts involved in creating network applications using sockets. The chapter introduces the java.net package containing various classes re-quired for creating sockets and message communication using two different protocols. It provides several example programs demonstrating various capabilities supported by Java for creating network applications.

Afer learning the contents of this chapter, the reader will be able to:
  • understand fundamental concepts of computer communication
  • understand sockets and ports
  • understand java.net package features
  • program Java Sockets
  • create comprehensive network applications using sockets

Download PDF


Cover ImageCalvert, K.L., Donahoo, M, J. (2001) TCP/IP Sockets in Java – Practical Guide for Programmers, Morgan Kaufmann

Download PDF

Most Internet applications use sockets to implement network communication protocols. TCP/IP Sockets in Java: Practical Guide for Programmers, with its focused, tutorial-based coverage, helps you master the tasks and techniques essential to virtually all client-server projects using sockets in Java. Later chapters teach you to implement more specialized functionality; incisive discussions of programming constructs and protocol implementations equip you with a deeper understanding that is invaluable for meeting future challenges. No other resource presents so concisely or so effectively the exact material you need to get up and running with Java sockets programming right away.


YouTube Video -Java – Sockets – Introduction – 1 of 3
(https://www.youtube.com/watch?v=6G_W54zuadg)

Published on 12 Mar 2012
Java – Sockets programming – an introduction. A simple client and server example 1 of 3.

Java – Sockets – Coding a Client-Server CHAT Room – 2 of 3
(https://www.youtube.com/watch?v=Uo5DY546rKY)

Published on 12 Mar 2012
Java – Sockets – Coding a Client-Server CHAT Room – 2 of 3.

Java – Sockets – Creating a Port Scanner and Ping Sweeper – 3 of 3
(https://www.youtube.com/watch?v=FvycwyAat6s)

Published on 12 Mar 2012
Java – Sockets – Creating a Port Scanner and Ping Sweeper – 3 of 3. Coding network tools.

Java – Networking – DatagramSocket, DatagramPacket and MulticastSocket Classes
(https://www.youtube.com/watch?v=yre19cIpYrQ&list=PL28D4CB13F443964B&index=80)

Published on 15 Mar 2012
Java – Networking – DatagramSocket, DatagramPacket and MulticastSocket Classes. Sending UDP traffic and creating multicast groups and connections with Java.

Java – Network Programming – NetworkInterface and InetAddress classes
(https://www.youtube.com/watch?v=QbcTJ9TdA_k&list=PL28D4CB13F443964B&index=81)

Published on 16 Mar 2012
Java – Network Programming – NetworkInterface and InetAddress classes