0% found this document useful (0 votes)
2 views4 pages

Sockets

The document provides an overview of sockets in Linux, explaining their definition, components, and types of communication. It details the types of sockets (TCP and UDP), real-world applications, and includes examples for setting up a simple server and client. Additionally, it discusses advanced features, compares sockets with REST, and presents problems with solutions related to socket programming.

Uploaded by

delia.racu27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Sockets

The document provides an overview of sockets in Linux, explaining their definition, components, and types of communication. It details the types of sockets (TCP and UDP), real-world applications, and includes examples for setting up a simple server and client. Additionally, it discusses advanced features, compares sockets with REST, and presents problems with solutions related to socket programming.

Uploaded by

delia.racu27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sockets in Linux

1. Introduction to Sockets

●​ Definition: A socket is an endpoint for communication between two machines or


processes, enabling data exchange over a network or within the same system.
●​ Components:
○​ IP Address: Identifies the host.
○​ Port Number: Identifies the specific application or service.
●​ Types of Communication:
○​ Inter-process communication: Using 127.0.0.1 (localhost).
○​ Network communication: Between different machines.
●​ Supported Languages:
○​ Sockets can be implemented in C, Python, Java, Node.js, and others. In this
course, Linux Bash scripts are used.

2. Types of Sockets

●​ Stream (TCP) Sockets:


○​ Provides reliable, ordered, and error-checked delivery of data.
○​ Used for web browsing, emails, and file transfers.
●​ Datagram (UDP) Sockets:
○​ Focuses on speed; data delivery may be unreliable or unordered.
○​ Commonly used for video streaming and online gaming.

3. Real-World Applications

●​ Web Servers: Handle client requests and send responses.


●​ Chat Applications: Enable real-time messaging.
●​ FTP (File Transfer Protocol): Transfer files over a network.
●​ Online Gaming: Real-time player interactions.

1
4. Setting Up a Simple Server and Client

●​ Server:
○​ Listens for incoming connections on a specific port.

Example:​
nc -l -p 12345

●​ Client:
○​ Connects to a server’s IP and port.

Example:​
nc 127.0.0.1 12345

●​ Terminate the Connection:


○​ Use Ctrl + C or EOF (^D).

5. Scenarios in Socket Communication

●​ Single Connection:
○​ A single client connects to a server.
●​ Multiple Sequential Connections:
○​ A single client reconnects multiple times while the server remains active.
●​ Concurrent Connections:
○​ Multiple clients connect to the same server simultaneously.

6. Writing Scripts for Sockets

●​ File Transfer Example:

Client:​
#!/bin/bash
nc 127.0.0.1 12345 < file_to_send.txt

Server:​
#!/bin/bash
nc -l -p 12345 > received_file.txt

●​ Date and Time Exchange Example:

Client:​
#!/bin/bash
echo $(date) | nc 127.0.0.1 12345
Server:​
#!/bin/bash
nc -l -p 12345

2
7. Sockets vs. REST

●​ Sockets:
○​ Used for low-latency, real-time communication.
○​ Protocol: TCP/UDP.
○​ Suitable for chat applications, gaming, and live streams.
●​ REST:
○​ Used for structured data exchange over HTTP/HTTPS.
○​ Protocol: JSON or XML.
○​ Ideal for APIs and web services.

8. Advanced Socket Features

●​ Encrypted Sockets:

Use ncat with SSL for secure communication.​


ncat --ssl -l -p 12345

●​ Handling Multiple Connections:

Use the -k option to keep the server active.​


nc -k -l -p 12345

3
Problems with Solutions

Problem 1: Echo Server: Write a server that echoes back any message it receives.
Server:​
nc -l -p 12345

Client:​
echo "Hello, Server!" | nc 127.0.0.1 12345

Problem 2: File Transfer Transfer a file from client to server.


Client:​
nc 127.0.0.1 12345 < file.txt
Server:​
nc -l -p 12345 > received_file.txt

Problem 3: Chat Application Create a simple chat between two terminals.


Terminal 1 (Server):​
nc -l -p 12345
Terminal 2 (Client):​
nc 127.0.0.1 12345

Problem 4: Send System Info Client sends the current date and time to the server.
Client:​
echo $(date) | nc 127.0.0.1 12345
Server:​
nc -l -p 12345

Problem 5: Concurrent Clients Allow multiple clients to connect simultaneously.


Server:​
nc -k -l -p 12345
Clients:​
echo "Client 1" | nc 127.0.0.1 12345
echo "Client 2" | nc 127.0.0.1 12345

You might also like