---
title: "Short note: Socket programming"
subject: "Computer Networks and Security"
chapter: 6
key: "semester_7/computer_networks_and_security:socket-programming-note"
source: https://www.bctengineer.com/q/semester_7/computer_networks_and_security/socket-programming-note
site: BCT Engineering
---

# Short note: Socket programming

Computer Networks and Security · Chapter 6: Application Layer

- Asked in: 2073 Shrawan, 2 marks; 2074 Chaitra, 4 marks (asked 2 times)
- Video: [Lec-86: Socket Programming in Computer Networks](https://www.youtube.com/watch?v=XTVTlEhGS6w) — Gate Smashers
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_7/computer_networks_and_security/socket-programming-note
- Whole chapter: https://www.bctengineer.com/md/semester_7/computer_networks_and_security/chapter-6

## Question

Define socket programming. (Short note: Socket programming.)

## Short answer

Socket programming uses the OS socket API (IP address + port) for network communication via TCP or UDP. TCP follows client-server sequence: socket, bind, listen, accept, send/recv, close. UDP uses SOCK_DGRAM with sendto/recvfrom without connection setup. Python example shows simple TCP echo server and client.

## Answer

**Socket programming** is writing network applications that communicate through sockets, the endpoints of a connection (IP address + port). The program uses the socket API provided by the operating system to send and receive data using TCP or UDP. It follows the **client-server** model.

#### TCP socket call sequence
```text
Server                         Client
socket()                       socket()
bind(port)
listen()
accept()  <---- connection --- connect()
recv()    <------ data ------- send()
send()    ------- reply -----> recv()
close()                        close()
```

#### Small example (Python, TCP)
```python
# server.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("0.0.0.0", 5000))
s.listen(1)
conn, addr = s.accept()          # waits for a client
data = conn.recv(1024)           # receive bytes
conn.sendall(data.upper())       # reply
conn.close()

# client.py
import socket
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.connect(("127.0.0.1", 5000))
c.sendall(b"hello")
print(c.recv(1024))              # b'HELLO'
c.close()
```

For UDP, `SOCK_DGRAM` is used with `sendto()` and `recvfrom()`, and no `listen/accept/connect` is needed.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is a socket?
   A. An endpoint of a connection (IP address + port)
   B. A cable
   C. A MAC address
   D. A routing table entry
   Answer: A. A socket is IP address plus port. (Key point: Socket)

2. Which is the correct server-side TCP call order?
   A. connect, send, recv
   B. socket, bind, listen, accept
   C. socket, accept, bind, listen
   D. listen, socket, connect
   Answer: B. Server: socket, bind, listen, accept, then recv/send. (Key point: Server call order)

3. How do UDP sockets differ in the API?
   A. SOCK_STREAM with listen/accept
   B. SOCK_DGRAM with sendto/recvfrom and no listen/accept/connect
   C. They need a three-way handshake
   D. They require bind only on clients
   Answer: B. UDP uses SOCK_DGRAM, sendto() and recvfrom(). (Key point: UDP sockets)
