Socket Programming Basics

Socket Programming

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.

Image of table showing the OSI network model.

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

1 import java.io.*;
2 import java.net.*;
3 4 public class SocketServer {
5    public static void main(String[] args) {
6 7 try {
8            ServerSocket serverSocket = new ServerSocket(6666);
9            Socket socket = serverSocket.accept();
10 11            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
12 13 String str = (String) dataInputStream.readUTF();
14            System.out.println("message= " + str);
15 16            socket.close();
17            serverSocket.close();
18 19        } catch (Exception ex) {
20            System.out.println(ex);
21        }
22    }
23 }Code language: JavaScript (javascript)

 File: SocketClient.java

1 mport java.io.*;
2 import java.net.*;
3 4 public class SocketClient {
5    public static void main(String[] args) {        
6 try {   
7            Socket socket = new Socket("localhost", 6666);
8            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
9 10            dataOutputStream.writeUTF("Hello Server");
11            dataOutputStream.flush();
12 13            dataOutputStream.close();
14            socket.close();
15 16        } catch(Exception ex)  {
17            System.out.println(ex);
18        }
19    }
20 }Code language: JavaScript (javascript)

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

Screenshots that display Compile and run Socket files in command prompt.

To learn about WebSocket Programming, check out this blog.

Add a Comment

Your email address will not be published. Required fields are marked *