BIT401 ยท 2082 ยท 5 marks
Mention the steps to write the socket program using TCP.
Mention the steps to write the socket program using TCP. [5]
Quick answer
Since reference notes are not provided, this answer follows standard TCP socket programming methodology taught in BSc CSIT curricula: Create a Socket; Use socket() system call to create a socket with domain (AFINET), type (SOCKSTREAM), and protocol (0 or IPPROTOTCP); Bind the Socket.
Model Answer: Steps to Write a Socket Program Using TCP
Since reference notes are not provided, this answer follows standard TCP socket programming methodology taught in BSc CSIT curricula:
Server-Side Steps:
-
Create a Socket
- Use
socket()system call to create a socket with domain (AF_INET), type (SOCK_STREAM), and protocol (0 or IPPROTO_TCP)
- Use
-
Bind the Socket
- Use
bind()to associate the socket with a specific port number and IP address - Define server address structure with port and address family
- Use
-
Listen for Connections
- Use
listen()to mark the socket as passive and specify the backlog queue size for pending connections
- Use
-
Accept Client Connection
- Use
accept()to accept incoming client connections - Creates a new socket for communication with the connected client
- Use
-
Send/Receive Data
- Use
send()orwrite()to transmit data to the client - Use
recv()orread()to receive data from the client
- Use
-
Close the Socket
- Use
close()to terminate the connection and release resources
- Use
Client-Side Steps:
-
Create a Socket
- Use
socket()with same parameters as server
- Use
-
Connect to Server
- Use
connect()to establish connection to server's IP address and port
- Use
-
Send/Receive Data
- Use
send()orwrite()to send data to server - Use
recv()orread()to receive data from server
- Use
-
Close the Socket
- Use
close()to terminate connection
- Use
Key Point: TCP ensures reliable, ordered, connection-oriented communication between client and server.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.