iXora Custom Software Development Blog

Read | Practice | Advance

Socket Programming

Posted by on in Blog
  • Font size: Larger Smaller
  • Hits: 174
  • 0 Comments
  • Print

What is Socket?

  • A Socket is on of the most fundamental technologies of computer network programming.
  • It allows network software applications to communicate using standard mechanisms build into network hardware and operating system.
  • Sockets allow communication between two different processes on the same or different machines.
  • More than tow pieces of software can communicate with client/server or distributed systems by using multiple sockets.
  • Sockets are bidirectional, meaning that either side of the connection is capable of both sending and receiving data.
  • Sometimes the one application that initiates communication is termed the "client" and the other application the "server“.

Before we go further let us understand the OSI(Open Systems Interconnection) Model.

OSI Model

The OSI Model is a conceptual and logical layout that defines network communication used by systems open to interconnection and communication with other systems.

b2ap3_thumbnail_OSI-network-model-1024x469.png

 

Where Socket Used?

  • Socket is used in a client-server application framework.
  • A server is a process that performs some functions on request from a client.
  • Most of the application-level protocols like FTP, SMTP, and POP3 make use of sockets to establish connection between client and server and then for exchanging data.
  • Modern network sockets are typically used in conjunction with the Internet protocols -- IP, TCP, and UDP.
  • Libraries implementing sockets for Internet Protocol use TCP for streams, UDP for datagrams, and IP itself for raw sockets.

Socket Types

  • Stream sockets:
    The most common type, requires that the two communicating parties first establish a socket connection, after which any data passed through that connection will be guaranteed to arrive in the same order in which it was sent - so-called connection-oriented programming model.
  • Datagram sockets:
    It offer "connection-less" semantics. With datagrams, connections are implicit rather than explicit as with streams. Either party simply sends datagrams as needed and waits for the other to respond; messages can be lost in transmission or received out of order, but it is the application's responsibility and not the sockets to deal with these problems. Implementing datagram sockets can give some applications a performance boost and additional flexibility compared to using stream sockets, justifying their use in some situations..
  • Raw sockets:
    The raw socket -- bypasses the library's built-in support for standard protocols like TCP and UDP. Raw sockets are used for custom low-level protocol development.

Socket Support In Network Protocols

  • Modern network sockets are typically used in conjunction with the Internet protocols -- IP, TCP, and UDP. Libraries implementing sockets for Internet Protocol use TCP for streams, UDP for datagrams, and IP itself for raw sockets.
  • To communicate over the Internet, IP socket libraries use the IP address to identify specific computers. Many parts of the Internet work with naming services, so that the users and socket programmers can work with computers by name (e.g., "thiscomputer.wireless.about.com") instead of by address (e.g., 208.185.127.40).
  • Stream and datagram sockets also use IP port numbers to distinguish multiple applications from each other. For example, Web browsers on the Internet know to use port 80 as the default for socket communications with Web servers.

Lets Do Some Coding

We are using Java code for this example

File: SocketServer.java
import java.io.*;
import java.net.*;

public class SocketServer {
	public static void main(String[] args) {

		try {
			ServerSocket serverSocket = new ServerSocket(6666);
			Socket socket = serverSocket.accept();

			DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());

			String str = (String) dataInputStream.readUTF();
			System.out.println("message= " + str);

			socket.close();
			serverSocket.close();

		} catch (Exception ex) {
			System.out.println(ex);
		}
	}
}
 File: SocketClient.java
import java.io.*;
import java.net.*;

public class SocketClient {
	public static void main(String[] args) {		
		try {	
			Socket socket = new Socket("localhost", 6666);
			DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

			dataOutputStream.writeUTF("Hello Server");
			dataOutputStream.flush();

			dataOutputStream.close();
			socket.close();

		} catch(Exception ex)  {
			System.out.println(ex);
		}
	}
}

Now first compile and run the ServerSocket and ClientSocket java files using command prompt respectively. You will see "message= Hello Server" in SocketServer console.

b2ap3_thumbnail_SocketProgramming.png

Rate this blog entry:
0

Mohammad Shafiqul Islam has not set their biography yet

Author's recent posts

Comments

  • No comments made yet. Be the first to submit a comment

Leave your comment

Guest
Guest Wednesday, 15 August 2018