Sobes.tech
Junior — Middle

Have you had experience working with network components and protocols in Linux?

sobes.tech AI

Answer from AI

Yes, experience with network components and protocols in Linux includes using system calls and libraries for socket programming (socket API).

Key points:

  • Creating a socket with socket().
  • Configuring address and port using sockaddr_in structures.
  • Binding the socket to an address with bind().
  • Listening for incoming connections with listen().
  • Accepting a connection with accept().
  • Sending and receiving data via send(), recv().

Example of a simple TCP server in C:

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

int main() {
    int server_fd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr = {0};
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8080);
    addr.sin_addr.s_addr = INADDR_ANY;

    bind(server_fd, (struct sockaddr*)&addr, sizeof(addr));
    listen(server_fd, 3);

    int client_fd = accept(server_fd, NULL, NULL);
    char buffer[1024] = {0};
    read(client_fd, buffer, 1024);
    printf("Received: %s\n", buffer);
    close(client_fd);
    close(server_fd);
    return 0;
}

Also familiar with TCP, UDP protocols, and their features in Linux environment.