What is a Socket?
A socket is a communication endpoint used in networking to send and receive data between two devices (often referred to as a client and a server) over a network, such as the internet. A socket is identified by an IP address and a port number.
Key Concepts:
- IP Address: A unique address that identifies a device on a network.
- Port Number: A numerical identifier within the IP address that distinguishes different services or applications on the same device.
- TCP/UDP: Sockets can use either the TCP (Transmission Control Protocol) for reliable, connection-oriented communication or UDP (User Datagram Protocol) for faster, connectionless communication.
Use Cases for Sockets
-
Web Servers and Clients:
- Sockets are used in web servers to listen for incoming connections from clients (such as browsers) and in clients to initiate connections to servers.
-
Real-Time Communication:
- Applications like chat servers, VoIP (Voice over IP), and online gaming use sockets to handle real-time data transmission between users.
-
File Transfer:
- Protocols like FTP (File Transfer Protocol) use sockets to transfer files between a client and a server.
-
Distributed Systems:
- Sockets facilitate communication between different components of a distributed system, such as microservices.
-
IoT Devices:
- Internet of Things (IoT) devices use sockets to communicate with servers or other devices for data collection and control.
Simple Socket Application in Go (Golang)
Let’s create a basic client-server application using sockets in Go.
Server Code (TCP Echo Server)
The server will listen on a port, accept incoming connections, and echo back any data it receives from the client.
Client Code (TCP Client)
The client will connect to the server, send a message, and display the server’s response.
How to Run the Application
-
Run the Server:
- Save the server code in a file called
server.go
. - Run the server using the command:
go run server.go
. - The server will start and listen for incoming connections on port 8080.
- Save the server code in a file called
-
Run the Client:
- Save the client code in a file called
client.go
. - Run the client using the command:
go run client.go
. - Enter a message when prompted. The client will send the message to the server, and the server will echo it back.
- Save the client code in a file called