1
I NTRODUCTION TO N ETWORKS Network programming is surprisingly easy in Java ◦ Most of the classes relevant to network programming are in the java.net package Sending data out onto a network involves attaching a stream to a network connection (socket) and using the stream I/O functions The main issues to consider are client/server architectures and attaching multiple clients to a server First we will look an introduction to networking and the TCP/IP protocol 2
I NTERNET P ROTOCOL (IP) Data is transmitted between computers in packets ◦ Each packet is marked with a destination address data Internet Client Server Server Ports Network packet
Each 4 byte address is the IP address Normally we refer to computers with domain names java.sun.com The translation from domain name to IP address is carried out using DNS ( Domain Name Service) IP has no provision for re-transmission in case of failure to deliver packets This is the job of the Transmission Control Protocol (TCP) Most internet programs (eg the WWW, etc) are based on TCP/IP
5
S OCKETS : WHAT ARE THEY ? If one looks up the definition, the most common one would be "A socket is one endpoint of a two-way communication link between two programs running on the network." To put it differently, it is through sockets that applications access the network and transmit data. The types of sockets are as varied as the purposes and platforms of applications. There are three types of sockets: Unix Domain Sockets Internet Domain Sockets NS Domain Sockets 6
Of these only Internet Domain Sockets are supported across all platforms. So to maintain the cross-platform characteristic intact, Java supports only Internet Domain Sockets. The next question that arises is what are the characteristics of an Internet Domain Socket and what protocols are supported by it? 7 S OCKETS : WHAT ARE THEY ?
I NTERNET D OMAIN S OCKETS By definition "An Internet socket (or commonly, a socket or network socket), is a communication end-point unique to a machine communicating on an Internet Protocol-based network, such as the Internet." All applications communicating through the Internet use a network socket. The feature that distinguishes a network sockets from other sockets is the protocols that it supports. The supported protocols are: TCP UDP Raw IP The difference between them is based on whether the protocol is connection oriented or not. 8
TCP is one of the core protocols of the Internet protocol suite. The protocol guarantees reliable and in-order (correct order of packets) delivery of data from sender to receiver. To put it simply, it's reliable. The second aspect of TCP is that it is connection oriented. That means TCP requires that a connection be made between the sender and receiver before data is sent. The socket associated with TCP is known as the Stream Socket. 9
TCP/IP A TCP/IP socket enables a java program running on a client machine to open a connection with a web server There is a socket on both the client and server sides. Client server communication is carried out through input and output streams 10
UDP like TCP, is one of the core protocols of the IP suite. However, unlike TCP, it neither guarantees in-order delivery of data nor does it requires a connection to be established for sending the data. To put it simply, UDP is an unreliable and connectionless protocol. Sockets associated with UDP are known as Datagram Sockets. 11
R AW IP Raw IP is a non-formatted protocol, unlike TCP and UDP. It works at network and transport layers. A socket associated with Raw IP is known as a Raw Socket. UDP and TCP sockets just receive the payload or the data, whereas Raw Sockets receive the header info of the packet along with the data. The downside of Raw Sockets is that they are tightly coupled with the implementation provided by the underlying host operating system. 12
13
HOW J AVA PLACES THE DIFFERENT TYPES OF SOCKETS IN ITS LIBRARIES. Sockets in Java Like all other functionalities provided by Java, functionalities to work with sockets are also "packaged" as a package and its classes. The following are the package and its main classes that help in accessing sockets: java.net package ServerSocket Socket Among the above, Java abstracts out most of the low-level aspects of socket programming. 14
S OCKETS IN J AVA The java.net package contains all the classes required to create network enabled applications. ServerSocket and Socket are also part of this package. Apart from these classes, it also contains classes to connect to the web server, create secured sockets, and so forth.web server The ServerSocket class provides server sockets or sockets at server side. Such sockets wait for requests over the network. Once such requests arrive, a server socket performs operations based on the request and may return a result. The ServerSocket class wraps most of the options required to create server-side sockets. 15
The Socket class provides client-side sockets or simply sockets. They are at the client side connecting to the server, sending the request to the server and accepting the returned result. Just as ServerSocket exposes only the compulsory parameters required to create a server-side socket, similarly, Socket asks the user to provide only those parameters that are most necessary. Next: Socket programming, step by step >> 16 S OCKETS IN J AVA