Loading...
Loading...

C Network Programming Tutorial

Network programming in C enables you to create server-client applications and handle communication across networks. This tutorial covers the basics of using sockets to establish connections between computers.

1. Introduction to Sockets

Sockets are used to establish communication between two machines over a network. A socket is an endpoint for sending or receiving data across a computer network.

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main() {
    int sockfd;
    struct sockaddr_in server_addr;
    
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        perror("Socket creation failed");
        exit(EXIT_FAILURE);
    }
    
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(8080);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
    printf("Connected to the server.\n");

    return 0;
}
                

This code demonstrates how to create a socket and connect to a server at IP address `127.0.0.1` on port `8080`.

2. Server-Side Program

The server listens for incoming connections from clients and responds to requests. The server must bind to a specific port and wait for client connections.

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
    int sockfd, new_sock;
    struct sockaddr_in server_addr, client_addr;
    socklen_t addr_size;
    
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        perror("Socket creation failed");
        exit(EXIT_FAILURE);
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(8080);
    server_addr.sin_addr.s_addr = INADDR_ANY;

    bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
    listen(sockfd, 5);
    addr_size = sizeof(client_addr);
    new_sock = accept(sockfd, (struct sockaddr*)&client_addr, &addr_size);
    
    printf("Connection established.\n");

    return 0;
}
                

This server code binds to port 8080 and listens for incoming client connections. When a connection is established, it accepts the client and prints a message.

3. Communication Between Client and Server

Once the connection is established, data can be exchanged between the client and server using the `send()` and `recv()` functions.

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main() {
    int sockfd;
    struct sockaddr_in server_addr;
    char *message = "Hello, Server!";

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        perror("Socket creation failed");
        exit(EXIT_FAILURE);
    }

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(8080);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
    
    send(sockfd, message, strlen(message), 0);
    printf("Message sent to server: %s\n", message);

    return 0;
}
                

This client code sends a message to the server using the `send()` function. The server can receive and process this message accordingly.

4. Conclusion

Network programming in C using sockets enables you to create client-server applications, allowing communication over the internet. By understanding how to create sockets, bind, listen, and accept connections, you can develop complex networked applications.

0 Interaction
524 Views
Views
50 Likes
×
×
🍪 CookieConsent@Ptutorials:~

Welcome to Ptutorials

Note: We aim to make learning easier by sharing top-quality tutorials.

We kindly ask that you refrain from posting interactions unrelated to web development, such as political, sports, or other non-web-related content. Please be respectful and interact with other members in a friendly manner. By participating in discussions and providing valuable answers, you can earn points and level up your profile.

$ Allow cookies on this site ? (y/n)

top-home