Thread: WiFi Basics in C programm (socket)

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    92

    WiFi Basics in C programm (socket)

    Hello,

    I'm curious about the data transmission aspects of WiFi programming in C. Could someone please share a basic code snippet or example that illustrates how to send data from one device to another using WiFi in a C program? I'm a beginner in WiFi programming, and this example would be immensely helpful in understanding the fundamentals.

    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Start here -> Useful Links And Good Books

    Network programming is the same API whether you're using wired or wireless.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103

  4. #4
    Registered User
    Join Date
    Aug 2023
    Posts
    3
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    
    
    #define PORT 8080
    #define MAX_BUFFER_SIZE 1024
    
    
    int main() {
        int server_socket, client_socket;
        struct sockaddr_in server_addr, client_addr;
        socklen_t client_addr_len = sizeof(client_addr);
    
    
        char buffer[MAX_BUFFER_SIZE] = "Hello, WiFi World!"; // Data to send
    
    
        // Create a socket
        server_socket = socket(AF_INET, SOCK_STREAM, 0);
        if (server_socket == -1) {
            perror("Socket creation failed");
            exit(EXIT_FAILURE);
        }
    
    
        // Initialize server address structure
        memset(&server_addr, 0, sizeof(server_addr));
        server_addr.sin_family = AF_INET;
        server_addr.sin_addr.s_addr = INADDR_ANY;
        server_addr.sin_port = htons(PORT);
    
    
        // Bind the socket to the server address
        if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
            perror("Binding failed");
            exit(EXIT_FAILURE);
        }
    
    
        // Listen for incoming connections
        if (listen(server_socket, 5) == -1) {
            perror("Listening failed");
            exit(EXIT_FAILURE);
        }
    
    
        printf("Server listening on port %d...\n", PORT);
    
    
        // Accept a client connection
        client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_len);
        if (client_socket == -1) {
            perror("Accepting client connection failed");
            exit(EXIT_FAILURE);
        }
    
    
        printf("Client connected\n");
    
    
        // Send data to the client
        send(client_socket, buffer, strlen(buffer), 0);
        printf("Data sent to the client: %s\n", buffer);
    
    
        // Close the sockets
        close(client_socket);
        close(server_socket);
    
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wierd wifi issue
    By geek@02 in forum Tech Board
    Replies: 1
    Last Post: 07-06-2017, 05:19 AM
  2. wifried sp3 wifi issue
    By kryptkat in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-10-2010, 06:33 AM
  3. Wifi boost....!!!!
    By bablu1988 in forum Tech Board
    Replies: 2
    Last Post: 08-14-2009, 09:37 AM
  4. I love WiFi =)
    By Hunter2 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 05-14-2006, 03:27 PM
  5. ah WiFi
    By axon in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-13-2003, 03:55 PM

Tags for this Thread