Communication Enhancement Using Socket Programming: Prashant Soni
Communication Enhancement Using Socket Programming: Prashant Soni
Prashant Soni
Abstract
As a java programmer one might face network- 1.1 Java TCP Programming
ing using socket programming. A network-based
system consists of a server, client, and a media The programming model of TCP communication in
for communication and for such communication Java, rely completely on the sockets and ports. Be-
we use “Sockets”. cause TCP is a stream protocol, it allows to send
arbitrary amount of data rather rely on class to en-
Sockets are interfaces that can "plug into" each capsulate data within TCP packets.
other over a network. Once so "plugged in", the
programs so connected communicate. A socket is 1.2 Sockets
one end-point of a two-way communication link
between two programs running on the network. Java programs communicate through a program-
Socket classes are used to represent the connec- ming abstraction called socket. A socket is one
tion between a client program and a server pro- end-point of a two-way communication link
gram. Such programming is called socket pro- between two computers (or programs) running on a
gramming. network. A socket is bound to a port number so that
the TCP layer can identify the application that data
The java.net package of “JAVA” provides two is destined to be sent. Mostly, applications do not
classes—Socket and ServerSocket, that imple- care how data is actually transmitted in the net-
ment the client side of the connection and the work. Applications identify the address of the peer
server side of the connection, respectively. entity and then use sockets interface to read and
write data from and to the peer.
Keywords –Client, Java.net, Sockets, Socket Pro-
gramming, Socket (Class), Server and Server- Sockets include the implementation of network and
Socket (Class). transport layer protocols providing applications
with a simple read/write interface. Because sockets
1. Introduction are just programming abstractions for network pro-
tocols, the other side of the connection does not
There are many codes developed for socket pro- have to use them. Sockets don’t use any additional
gramming which were previously being implemen- communication mechanism other than that
ted but some drawbacks like slow speed of execu- provided by the encapsulated protocol [1].
tion, they were not user friendly, uneven flow of
data, etc hindered their best possible use and I have 1.3 Ports
tried to patch them.
The mechanism, commonly used by network proto-
As far as security is concerned, the ‘class’ file of cols, and particularly by the Internet transport layer
java plays its role and thus provide much security protocols, is port addressing [2]. For example, tcp:23
for code on being copied to any other operating port number is assigned for FTP, tcp:80 for HTTP,
system. etc.
Typically, integer numbers are used to identify dif-
ferent ports. In order to contact a network service,
it is therefore necessary to provide both the IP ad-
dress of its host, as well as port number it is using.
2.2 Receiving Data through a Socket The main task of server socket is to receive incom-
ing connection requests and generate a
For receiving the data also we shall use socket and java.net.Socket object that encapsulates each re-
in same way as data was sent to stream, we will re- quest. Incoming connections are queued until the
ceive it. The data received through socket is read program retrieves them one at a time by invoking
and printed on output screen. the accept() method. The accept() method takes no
arguments, and returns the next connection in the
2.3 Client-Side TCP Programming queue.
In order to accept network connections a Java pro- Typically, programs running on client machines
gram must create an instance of java.net.Server- make requests to programs on a server machine.
Socket. Server sockets are not directly used to per- These involve networking services provided by the
form any network communication. Instead, they act transport layer. The most widely used transport
as factories that create a java.net.Socket object for protocols on the Internet are TCP (Transmission
every incoming TCP communication request. Pro- control Protocol) and UDP (User Datagram Pro-
grams create the server socket, bind to a specific tocol). TCP is a connection-oriented protocol
providing a reliable flow of data between two com-
puters. It is used by applications such as the World
Wide Web, e-mail, IP, and secure shell. [7] Kathy Sierra & Bert Bates, Head First
Java (2nd edition, O’Reilly Media, 2005)
Sockets provide an interface for programming net-
works at the transport layer. Using sockets, net-
work communication is very much similar to per-
forming fi le I/O. A socket is an endpoint of a two-
way communication link between two programs
running on the network. The source and destination
IP address, and the port numbers constitute a net-
work socket.
4 Future Work
References
Articles:
[1] http://www.buyya.com/java/Chapter13.pdf
[2] http://www.scribd.com/doc/29858538/Jav
a-TCP-Programming
[3] http://www.pcvr.nl/tcpip/tcp_conn.htm
[4] http://download.oracle.com/javase/1,5.0/d
ocs/api/ (Java API(Application Program-
ming Interface) 5.0)
[5] http://edn.embarcadero.com/article/31995
Books: