You are on page 1of 6

Familiarization of Network Programming

We have to use 2 packages for our network programming namely

net package is enough for network programming. But when we go for connection oriented
service, then we have to make use of streams which are available in io package.
Socket Communication Overview
There are two forms of socket communication, connection oriented and connectionless.
The TCP/IP protocol suite supports these using two different protocols, TCP (Transmission
Control Protocol) UDP (User Datagram Protocol).
TCP (Transmission Control Protocol): TCP is a connection oriented, transport layer
protocol that works on top of IP. TCP packets are encapsulated inside IP packets when data is
transferred across the network. TCP provides a connection oriented virtual circuit, which is
permanent throughout the session between the local and remote hosts. The circuit has to be
established before data transfer can begin. A connection is based on the host IP address and the
"port" or channel that the server is listening on. The client will send out a send a number of
initialization packets to the server so that the circuit path through the network can be established,
when communication is over additional packets are used to release the circuit. These additional
packets are the overhead associated with TCP. However, the overhead is quickly regained because
of the built-in features of TCP.
Once established all packets are sent along the same path, as a stream of data, each packet
does not need to carry destination details with it. A host will usually establish two streams, one for
incoming and one for outgoing data. TCP provides other mechanisms to ensure that data integrity
is maintained. Packets are numbered to avoid lost packets and incorrect ordering. Flow control is
used, allowing variable data transmission rates to avoid buffer overflow and full-duplex
transmission is also provided. These features reflect the fact that TCP is considered a reliable
transmission protocol, unlike UDP, which is considered unreliable.
UDP (User Datagram Protocol): UDP is a connectionless transport layer protocol that
also sits on top of IP. Unlike TCP, UDP provides no data integrity mechanisms except for a single
checksum, and unlike TCP does not establish a connection to the destination before sending any
data. UDP, simply packages it’s data into, what is known as a Datagram, along with the destination
address and port number and sends it out onto the network. If the destination host is alive and
listening it will receive the Datagram if not it will be discarded. Because there is no guaranteed
delivery, as there is with TCP, there is a possibility that datagram will be lost corrupted or delivered
in the wrong order. If such facilities are required they must be added to the application manually
by the programmer. The minimal implementation of UDP quality of service has led it to be referred
to as Unreliable Datagram Protocol.
However, UDP does have it’s advantages, because the protocol has few built-in facilities it has a
very low management overhead. The lack of "permanent" connection also means that UDP can
adapt better to network failures, as packets can be routed elsewhere to get to their destination.
There are some well-known examples of UDP, such as DNS lookups, PING and SNMP. The
common factor with these examples is small message length, to keep packets small, and "query
based" applications that are not dependent on data reliability.
Java uses many stream-based mechanisms throughout the language to achieve I/O. For example
all file I/O and memory I/O in Java is achieved through streams. Most messaging is also carried
out using streams although basic datagram implementations are also supported.
Class InetAddress
This class provides methods to access host names and IP addresses.
public final class InetAddress extends Object implements Serializable
The following methods are provided to create InetAddress objects:
Static InetAddress getLocalHost() throws UnknownHostException This method returns an
InetAddress object for the local machine.
Static InetAddress getByName(String host) throws UnknownHostException This method returns
an InetAddress object for the specified host name. The host name can be either a pneumonic
identifier such as "" or an IP address such as This is the only method
that can be used by a client to get remote hosts details.
The following method is provided to extract information from and InetAddress object.
String getHostName() Returns the host name held in the InetAddress object. If the host name is
not already known an attempt is made to look it up, if this fails the IP address is returned as a
Exception Errors
UnknownHostException This is a subclass of IOException and indicates that the host name could
not successfully be identified.
SecurityException This error is thrown if the Java security manager has restricted the desired
action from taking place.
Class Socket
A Socket object is a TCP socket used for communicating between 2 hosts. A socket is created
generally with host name and port address of the application to which we need to connect.
public class Socket extends Object
protected Socket()
protected Socket(InetAddress addr, int port)
void close()
InetAddress getInetAddress()
InputStream getInputStream()
int getPort()
void setSoTimeout(int timeout)
Class ServerSocket
A ServerSocket object is the server-side socket used in TCP/IP client/server applications. When
we create a ServerSocket, we have to specify a particular port and then use an accept on that
port. The accept() call blocks until a client connects to it.
public class ServerSocket extends Object
ServerSocket(int port)
Socket accept()
void close()
InetAddress getInetAddress()
void setSoTimeout(int timeout)
Class DatagramSocket
This class represents a socket for sending and receiving datagram packets.
public class DatagramSocket extends Object
DatagramSocket(int port)
DatagramSocket(int port, InetAddress addr)
void close()
int getPort()
void setSoTimeout(int timeout)
InetAddress getInetAddress()
Class DatagramPacket
This class is used to send information from one datagram socket to another. Datagram sockets
and packets provide for connectionless communication.
public final class DatagramPacket extends Object
DatagramPacket(byte[],int length)
DatagramPacket(byte[],int length,InetAddress addr, int port)
InetAddress getAddress()
byte[] getData()
int getLength()
int getPort()

System Calls in Operating System

The interface between a process and an operating system is provided by system calls. In general,
system calls are available as assembly language instructions. They are also included in the
manuals used by the assembly level programmers. System calls are usually made when a process
in user mode requires access to a resource. Then it requests the kernel to provide the resource via
a system call.
Types of System Calls
There are mainly five types of system calls. These are explained in detail as follows:
Process Control
These system calls deal with processes such as process creation, process termination etc.
File Management
These system calls are responsible for file manipulation such as creating a file, reading a file,
writing into a file etc.
Device Management
These system calls are responsible for device manipulation such as reading from device buffers,
writing into device buffers etc.
Information Maintenance
These system calls handle information and its transfer between the operating system and the user
These system calls are useful for interprocess communication. They also deal with creating and
deleting a communication connection.

CreateProcess() fork()
Process Control ExitProcess() exit()
WaitForSingleObject() wait()

CreateFile() open()
ReadFile() read()
File Manipulation
WriteFile() write()
CloseHandle() close()

SetConsoleMode() ioctl()
Device Manipulation ReadConsole() read()
WriteConsole() write()
GetCurrentProcessID() getpid()
Information Maintenance SetTimer() alarm()
Sleep() sleep()

CreatePipe() pipe()
Communication CreateFileMapping() shmget()
MapViewOfFile() mmap()

SetFileSecurity() chmod()
Protection InitlializeSecurityDescriptor() umask()
SetSecurityDescriptorGroup() chown()