Definition and Importance of Computer Networks

Welcome to the exciting world of computer networks, where computers connect and communicate. In this article, we’ll explore the definition, examine a simple server-client C program, and discuss the types, importance, and real-life examples of computer networks.

Defining Computer Networks

Computer networks are like magical pathways allowing computers to talk, share information, and work together. They are made up of unique connections and rules that enable computers to send messages, pictures, and even videos across different places.

Let us see how two programs can connect using concepts of computer networks. To keep it simple, we will examine client and server programs that can run on a single machine.

Simple Networking Program in C

To understand computer networks better, let’s dive into a simple program written in the C programming language. This program will demonstrate how computers can communicate with each other over a network.

C program for server-side

This simple C program showcases a basic server that listens for incoming network connections. It creates a socket, binds it to a specific port, and listens for incoming connections. Once a client connects, it reads a message from the client, prints it, and sends a response back.

s$ cat simple-socket-server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>  // Add this line to include the unistd.h header file

#define PORT 8080

int main() {
   int server_fd, new_socket, valread;
   struct sockaddr_in address;
   int opt = 1;
   int addrlen = sizeof(address);
   char buffer[1024] = {0};
   char *hello = "Hello from the server!";

   // Create a socket
   if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
       perror("socket failed");
       exit(EXIT_FAILURE);
   }

   address.sin_family = AF_INET;
   address.sin_addr.s_addr = INADDR_ANY;
   address.sin_port = htons( PORT );

   // Bind the socket to a specific port
   if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
       perror("bind failed");
       exit(EXIT_FAILURE);
   }

   // Listen for incoming connections
   if (listen(server_fd, 3) < 0) {
       perror("listen failed");
       exit(EXIT_FAILURE);
   }

   // Accept incoming connection
   if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) {
       perror("accept failed");
       exit(EXIT_FAILURE);
   }

   // Read data from the client
   valread = read(new_socket , buffer, 1024);
   printf("%s\n",buffer );

   // Send a message back to the client
   send(new_socket , hello , strlen(hello) , 0 );
   printf("Hello message sent\n");
   return 0;
}

C program for client-side

The client code establishes a connection with the server code in the previous section over a computer network. It sends a message to the server saying, “Hello from the client!” and waits for a response. Once the server receives the message, it sends back a reply. The client then reads the response and displays it on the screen.

$ cat simple-socket-client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>  // Add this line to include the unistd.h header file

#define PORT 8080

int main() {
    int sock = 0, valread;
    struct sockaddr_in serv_addr;
    char *hello = "Hello from the client!";
    char buffer[1024] = {0};

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket creation error");
        return -1;
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    // Convert IP address from string to binary format
    if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
        perror("invalid address or address not supported");
        return -1;
    }

    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        perror("connection failed");
        return -1;
    }

    send(sock, hello, strlen(hello), 0);
    printf("Hello message sent\n");

    valread = read(sock, buffer, 1024);
    printf("%s\n", buffer);

    return 0;
}

We will compile and run the two executables – one for the server and one for the client and see how they connect and demonstrate the power of computer networks.

$ gcc -o simple-socket-server simple-socket-server.c
$ gcc -o simple-socket-client simple-socket-client.c

ubuntu@ip-172-31-10-60:~/network-programs

Server Side program execution

$ ./simple-socket-server
Hello from the client!
Hello message sent

Client Side program execution

$ ./simple-socket-client
Hello message sent
Hello from the server!

This code demonstrates how computers can communicate with each other using network connections, enabling the exchange of information and facilitating collaboration between different systems.

Now that you have a taste of a basic networking program let us delve into types of computer networks.

Types of Computer Networks

Two common types of computer networks are Wide Area Networks (WAN) and Local Area Networks (LAN). WANs span large geographic areas and connect computers across cities, countries, and continents. The Internet is a prime example of a global WAN, providing a vast network infrastructure that facilitates worldwide communication and access to information. On the other hand, LANs are localized networks, often found in homes, offices, or educational institutions. They enable the seamless sharing of resources within a limited area, such as printers, file servers, and collaborative tools.

Although these are the common types of computer networks, refer to these links to get details of more elaborate classifications of network hardware – classification based on transmission technology and classification based on the scale of the networks.

Importance of Computer Networks

Computer networks have become the backbone of our digital society, shaping how we work, communicate, and access information. They facilitate global collaboration, empower businesses, and drive innovation. Without computer networks, our day-to-day tools and applications would lose their power and reach, severely limiting our ability to connect, share, and learn.

To know more clearly and clearly understand the importance, let us see some everyday situations in daily life where we use computer networks consciously or unconsciously.

Real-life examples of computer networks in day-to-day life

Browsing the Internet: When we open our web browsers and embark on a virtual journey through the Internet, we are immersing ourselves in the power of computer networks. With a few clicks, we can access a wealth of knowledge, stream multimedia content, and connect with people from around the globe. Websites, hyperlinks, and search engines act as gateways, guiding us through this interconnected information landscape.

Email Communication: Sending and receiving emails has become integral to our personal and professional lives. As we compose messages and click the “Send” button, our words traverse computer networks, instantly crossing physical boundaries. Email protocols and servers handle transmitting and delivering these messages, ensuring they reach their intended recipients regardless of their geographical location.

Social Media Networks: Social media platforms have revolutionized how we connect and communicate with others. These networks rely heavily on computer networks to facilitate real-time interactions, content sharing, and the formation of online communities. As we scroll through our feeds, leave comments, and share updates, we utilize computer networks’ power to bridge distances and foster connections.

Online Gaming: The immersive realm of online gaming demonstrates computer networks’ collaborative and competitive nature. Thanks to the underlying network infrastructure, gamers from across the world join forces or compete against each other in virtual worlds. Multiplayer games leverage computer networks to enable real-time communication and synchronization, delivering interactive and engaging experiences.

Conclusion

As we navigate our daily lives, we often take for granted the invisible marvels of computer networks that enable our digital interactions. Computer networks have become indispensable for browsing the web, sending emails, participating in social media, and engaging in online gaming. Understanding the definition and importance of computer networks allows us to appreciate the vast opportunities and endless possibilities they offer, connecting us to the world and shaping our collective future.