Socket programming is used for network communication using TCP/IP protocols. Network communication is made possible by developing application programming interfaces (API). A socket interface is developed which defines a varied function for application development to be used in TCP/IP networks. The Unix Operating system is commonly used in the development of socket interfaces. Microsoft Windows is also used to develop socket interfaces.
Socket programming is language independent, that is socket programming can be done in the desired programming but the condition is that language must support network communication. The most preferred language for socket programming in Java. Java is preferred over other programming languages as it is platform-independent.
Java has a strong exception handling mechanism to handle errors that arise during I/O and networking operations. The Java threads are used to implement servers, Java has inbuilt libraries that are used to develop client-server applications to be used in different networks. Java libraries are developed to support the Internet which makes it suitable for internet-based applications. Apart from this Java is also used to do socket programming since it has well-developed security features which makes it suitable for network programming. Thus Java has a number of options that can be used to develop socket programs.
A network is developed connecting computers, in this network few computers act as clients, and few acts as servers. Servers provide different services to clients. The client requests a particular service to its server and if the requested service is available then the server replies back with the desired response. There exist different types of servers such as file servers and print servers. Servers are also responsible for providing, maintaining, and managing different network services. The server delivers its response to clients. Response delivered by servers is in the form of files, devices and enhances processing power.
Client-server communication is made possible by using socket programming. Socket programming uses TCP and UDP protocols. TCP/UDP protocols are used to make communication using streams and datagrams. Any application that is developed to run on a client-server network is developed for both the client and the server. The client-server program is developed for both the client and server and the developer ensures that RFC port numbers are not used.
Socket acts as an interface that makes communication between client and network possible. The socket is an application program that makes communication across computer networks possible. The socket program uses IP address and Port number.
The socket programming is done to do four basic types of operations. First, socket programs are used to connect to remote machines, Second, socket programs are used to forward data from client to server, third, socket programs are used to receive requested data from the server, fourth when the communication between client and server is complete then close the requested connection. A socket program is used to develop a one-to-one connection and send and receive data from the connected computer that may be a client or server. The java.net.Socket class is used in Java language to develop applications that are used to carry out these four basic socket operations.
Client-server communicate using socket applications and these applications work on a specific port. Ports are often application-specific or process-specific. Ports are decided on the basis of host IP address and on the type of protocol used. Applications running on the client or on the server are identified using a port. The Transmission control protocol and user Datagram protocol use ports. TCP and UDP use ports to make communication between specific applications running on the client and server. Thus the purpose of the port is to transfer specific messages from the client and the server to a specific message.
On a computer network there may exist many servers and to identify a particular server ports are used. Ports are 16-bit integer numbers and are logical.
Socket programming is done using Java. Java is used as it has well-developed libraries that support network programming. For example, java.net uses classes that represent URLs and sockets. In Java, networking applications can be written using the java.io class library. In Java-based socket programming, the service of BSD-style socket is used. BSD style socket is used to do Interprocess communication.
Inter-process communication using BSD is done to do socket programming using TCP/IP or UDP/IP. Sockets use Internet protocols to communicate. Socket programming can be done in two modes first, connection-oriented, second connectionless. When connection-oriented socket programming is done it is done using TCP and when connectionless socket programming is done it uses UDP.
UDP is a protocol used to do connectionless socket programming. It is a datagram protocol and makes communication with the server without developing a connection. In socket programming using UDP instead of making communication with the client, the server waits for the client to send data. In a connectionless socket, programming handshaking is not done. Connectionless socket programming done using User Datagram Protocol is not reliable as there is no mechanism that ensures that communication is successful. The message transferred using User Datagram Protocol may get corrupted on the way to the receiver.
User Datagram Protocol used to conduct communication between client and server has following functions:
- In this approach, both the client and server have socket ( ) functions.
- Connectionless socket programming using User Datagram Protocol is mostly server-side.
- This is mostly server-side socket programming and waits for the client to send data.
- Connectionless socket programming is done to send and receive data from client to server and server to client.
- When communication is done, the connection established is closed.
UDP sockets are developed by DatagramSocket. Java has an inbuilt library used for connectionless socket programming is java.net.DatagramSocket. java.net.DatagramSocket has the following functions:
DatagramSocket() | This function makes datagram sockets and establishes connections using a free UDP port. |
DatagramSocket(int port) | Makes a datagram socket and establishes connection with the port. |
void receive(DatagramPacket p) | This function is used to receive data packets. |
void send(DatagramPacket p) | This function is used to send packets. |
An example code that explains the use of datagram socket is as follows:
import java.io.*; import java.net.*; public class UDPReceiver { public static void usage( ) { System.out.println(“This represent java UDP Receiver port number”); System.exit(0); } public static void main( String [ ] args) outputStream out = null; try { String text_in_use = “test_program”’; if(args) if(args.length!=3) { throw new exception(“Bad_number_of_arguments”)’; } Datagramsocket ss = new DatagramSocket(Integer.parseInt(args[0])); ss.connect(InetAddress.getByName(args[1]), Integer.parseInt(args[2])); ss.send(new DatagramPacket(text.getBytes(), text.length( ))); } catch(Exception E) { usage ( ); } } }
Leave a Reply