28
2011
Getting Started with Socket Programming in Java
The world today has probably more than a good billion computers in operation right now and almost all of them are connected to each other at one time or other. Networking between computers gives an additional dimension to using this amazing creation. Network Programming in Java is really simple; it uses something called sockets to facilitate communication between any two machines. In this series of stone I will focus on network programming in java. To start up with in this particular stone we won’t be doing any socket programming as such but will be clearing out all the basics. It is usually a good idea to have a firm grasp of all the basics before jumping into the actual coding process which will be done in later stones. So let’s get started with:
Sockets what are they??
Think of Sockets in Java like an electric socket, if you were to look up the exact definition of it you would find something like this, “Sockets are endpoints of a two-way communication link between any two programs running on a network.” It is through sockets that application accesses the network to receive and transmit data. Java facilitates socket programming through its java.net package. Sockets in the world of networking are varied as the type of application using it. Basically put there are 3 types of Sockets:
- Unix Domain Sockets
- Internet Domain Sockets
- NS Domain Sockets
Java only deals with Internet Domain sockets and as such we will be only discussing this type of socket. An internet socket is a communication end-point unique to a machine communicating on an Internet Protocol-based network. The feature that distinguishes a network sockets from other sockets is the protocols that it supports. The supported protocols are:
1. UDP
2. TCP
3. Raw IP
The difference between them is based on whether the protocol is connection oriented or not. Here are the details.
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. The data is sent in a specific order and involves various types of acknowledgements. It 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. Although very reliable this protocol tends to be a bit slow due to the involvement of various acknowledgement requests.
UDP, like TCP, is one of the core protocols of the IP suite. However, unlike TCP, it does not guarantee in-order delivery or confirmed delivery of data. It follows the philosophy of, “transmit and forget”. To put it simply, UDP is an unreliable and connectionless protocol. It does not have any acknowledgements associated with it and sends the data in a random order. Sockets associated with UDP are known as Datagram Sockets. Though unreliable this protocol tends to be the protocol of choice when a fast method of communication is needed.
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.
Now that we know what sockets are exactly let’s see how Java uses them:
Sockets in Java:
There are basically 2 types of sockets in Java.
ServerSocket: These types of sockets are listeners they are the sockets at server side and as such 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.
Socket: The Socket class provides client-side sockets or simply sockets. They are at the client side handling the connection with 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.
That covers the basics of sockets in java. Watch this space for more detailed description regarding the use of sockets paired with the various protocols along with a step by step guide on creating socket based applications.
Subscribe to fortystones.
Follow @fortystones on Twitter.
Get updated from our Facebook Fanpage.
Related Posts
Leave a comment
Fortystones Lab Projects
Categories
- Articles (43)
- Idea (2)
- Review (5)
- Social Media (29)
- Trending Topics (13)
- Collection (29)
- How To (27)
- Linux (26)
- News (15)
- PHP (6)
- Project (1)
- Tutorials (35)
- Java (4)
- Programming (10)
- Wordpress (7)
Popular Posts
- 40 Basic Linux Command-line Tips and Tricks
- Tips and Tricks for Facebook Chat (Save History/ Video Chat/ Send Files)
- The First on the World Wide Web
- 40 Linux Shell Commands for Beginners
- Online Coding Zones for Programmers
- Special: Facebook Smiley, Special Text Symbols and ASCII Arts
- 13 years of Google: 1997- Present

An article by Prerak Pradhan





