



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Business Management is one of the most important subject in Management Sciences.Following are the key points discussed in these Lecture Slides : Java Network Programming, Overview of Networking, Communication Over The Network, Transmission Control Protocol, Definition, Datagrams, Understanding Ports, Networking Classes in The Jdk, Urlconnection, Datagrampacket
Typology: Slides
1 / 7
This page cannot be seen from the preview
Don't miss anything!




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. It allows you to write programs that focus on solving the problem at hand.
Overview of Networking
The Java platform suitable for writing programs that use and interact with the resources on the Internet and the World Wide Web. Java-compatible browsers use this ability of the Java platform to the extreme to transport and run applets over the Internet. You may be already using the networking capabilities of the Java platform without realizing that you are using the network. Ex: Applets
Communication over the Network
Computers running on a Network communicate to each other using either
Communication over the Network
When you write Java programs that communicate over the network, you are programming at the application layer. Typically, you don't need to concern yourself with the TCP and UDP layers. You can use the classes in the java. net package. These classes provide system-independent network communication. However, to decide which Java classes your programs should use, you do need to understand how TCP and UDP differ.
Transmission Control Protocol
Definition: TCP is a connection-based protocol that provides a reliable flow of data between two computers
When two applications want to communicate to each other reliably, they establish a connection and send data back and forth over that connection. TCP guarantees that data sent from one end of the connection actually gets to the other end and in the same order it was sent. Analogues to a telephone conversation The Hypertext Transfer Protocol (HTTP), File Transfer Protocol (FTP), and Telnet are all examples of applications that require a reliable communication channel.
The order in which the data is sent and received over the network using these protocols is critical Otherwise you will get a jumbled HTML file
UDP – User Datagram Protocol
Definition: UDP is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. UDP is not connection-based like TCP. The UDP protocol provides for communication, that is not guaranteed between two applications on the network. Rather, it sends independent packets of data, called datagrams Analogues to sending a letter through the postal service The order of delivery is not important and is not guaranteed, and each message is independent of any other. For many applications, the guarantee of reliability is critical to the success of the transfer of information from one end of the connection to the other. However, other forms of communication don't require such strict standards. In fact, they may be slowed down by the extra overhead or the reliable connection may invalidate the service altogether.
Understanding Ports
Definition: The TCP and UDP protocols use ports to map incoming data to a particular process running on a computer. Computer has a single physical connection to the network. All data destined for a particular computer arrives through that connection. However, the data may be intended for different applications running on the computer. So how does the computer know to which application to forward the data? That is through the use of ports. Data transmitted over the Internet is accompanied by addressing information that identifies the computer and the port for which it is destined. The computer is identified by its 32-bit IP address, which IP uses to deliver data to the right computer on the network. Ports are identified by a 16-bit number, which TCP and UDP use to deliver the data to the right application.
Networking Classes in the JDK
Through the classes in java. net , Java programs can use TCP or UDP to communicate over the Internet. URL , URLConnection , Socket , and ServerSocket classes all use TCP to communicate over the network.
The DatagramPacket , DatagramSocket , and MulticastSocket classes are for use with UDP.
Working with URLs
URL is the short form of Uniform Resource Locator. It is a reference (an address) to a resource on the Internet.
TCP provides a reliable, point-to-point communication channel those 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.
What is a Socket?
A socket is one end-point of a two-way communication link between two programs running on the network. Socket classes are used to represent the connection between a client program and a server program.
The java.net package provides two classes Socket – Implements the client side of the connection ServerSocket--Implement the server side of the connection Sockets provide an interface for programming networks at the transport layer. A socket is bound to a port number so that the TCP layer can identify the application that data destined to be sent. Network communication using Sockets is very much similar to performing file I/O
In fact, socket handle is treated like file handle. The streams used in file I/O operation are also applicable to socket-based I/O On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server. The client and server can now communicate by writing to or reading from their sockets.
Socket Communication
A server (program) runs on a specific computer and has a socket that is bound to a specific port. The server waits and listens to the socket for a client to make a connection request. If everything goes well, the server accepts the connection.
Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.
Receive from client: String line = is.readLine(); Send to client: os.writeBytes("Hello\n");
A datagram is an independent, self-contained message sent over the network whose arrival, arrival time, and content are not guaranteed. Applications send and receive completely independent packets of information. Clients and servers do not have and do not need a dedicated point-to-point channel. The delivery of datagrams to their destinations is not guaranteed. Nor is the order of their arrival. The java.net package contains three classes to help you write Java programs that use datagrams to send and receive packets over the network:
DatagramSocket , DatagramPacket , and MulticastSocket An application can send and receive DatagramPackets through a DatagramSocket. In addition, DatagramPackets can be broadcast to multiple recipients all listening to a MulticastSocket
This class represents a socket for sending and receiving datagram packets. A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed.
Multiple packets sent from one machine to another may be routed differently, and may arrive in any order.
Constructs a datagram socket and binds it to the specified port on the local host machine.
Creates a datagram socket, bound to the specified local address.
This class represents a datagram packet. Datagram packets are used to implement a connectionless packet delivery service. Each message is routed from one machine to another, solely based on information contained within that packet. Multiple packets sent from one machine to another might be routed differently, and might arrive in any order. Packet delivery is not guaranteed.
Constructs a DatagramPacket for receiving packets of length length.
DatagramPacket (byte[] buf, int offset, int length, InetAddress address, int port) Constructs a datagram packet for sending packets of length length with offset ioffsetto the specified port number on the specified host.