C Closing Socket (close())

beginner
11 min

C Closing Socket (close())

Welcome to our comprehensive guide on C Closing Socket using the close() function! In this lesson, we will delve into the world of sockets in C programming, explaining how to create, use, and finally close them. Let's get started!

Understanding Sockets

šŸ’” Pro Tip: A socket is an endpoint in a network communication system that can be either a network connection or a network protocol port. In simpler terms, it's like a door in a network for data transfer.

In C programming, sockets are used for both client and server communication. They allow your program to communicate with other computers over a network.

Socket Lifecycle

A socket goes through various stages during its lifetime: creation, connection, data transfer, and closing. In this lesson, we'll focus on the last stage – closing the socket.

Closing a Socket

To close a socket in C, we use the close() function. This function terminates the socket connection and frees up system resources associated with it.

c
#include <stdio.h> #include <sys/socket.h> #include <unistd.h> int main() { int sockfd, connfd; sockfd = socket(AF_INET, SOCK_STREAM, 0); connfd = connect(sockfd, ...); // Connect to server or wait for client connection // Your code here... close(connfd); // Close the connection close(sockfd); // Close the socket return 0; }

šŸ“ Note: In the above example, connfd is the connection socket, and sockfd is the socket descriptor. Close both of them in reverse order of creation to ensure that connections are closed before the socket itself.

Why Close a Socket?

Closing a socket is crucial for efficient resource management. When a socket is closed, any associated resources like memory, file descriptors, and network connections are freed up, allowing your program to run smoothly and without any resource leaks.

Advanced Example: Server and Client Connection

Let's look at a more advanced example of a server and client communicating over a socket, and then closing the connection when they're done.

Server

c
#include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> int main() { int sockfd, connfd; struct sockaddr_in serv_addr, cli_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(54000); bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); listen(sockfd, 5); while (1) { connfd = accept(sockfd, (struct sockaddr *)&cli_addr, NULL); // Your code here to handle client request... close(connfd); } close(sockfd); return 0; }

Client

c
#include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> int main() { int sockfd; struct sockaddr_in serv_addr; sockfd = socket(AF_INET, SOCK_STREAM, 0); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(54000); serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); // Your code here to send/receive data... close(sockfd); return 0; }

šŸŽÆ Exercise: Modify the server and client examples to send/receive a simple message between them. Can you make it so that the server keeps running and can handle multiple client connections simultaneously?

Quiz

Quick Quiz
Question 1 of 1

What is the purpose of the `close()` function in C programming?

We hope you found this lesson informative! With practice, you'll be able to handle sockets like a pro in no time. Keep learning and coding! šŸ’Ŗ