CodeBlanks Logo Icon CodeBlanks

Socket layer

Write a network communication layer with sockets.

Socket layer

In this lesson you'll define the server_setup function. It creates an AF_UNIX socket, binds it to /tmp/cb_p.sock and starts listening. On success it returns the descriptor; on failure -1.

Steps: socket(AF_UNIX, SOCK_STREAM, 0), zero the address struct with memset, set sun_family = AF_UNIX, copy the path with strncpy, remove the old socket with unlink, bind, listen(fd, 1). This is the classic Unix socket server setup. The main from previous lessons is included in the starting code.

Address struct

You declare struct sockaddr_un addr;. Zero it with memset(&addr, 0, sizeof(addr)); set addr.sun_family = AF_UNIX; and strncpy(addr.sun_path, "/tmp/cb_p.sock", sizeof(addr.sun_path) - 1);.

Remove the old socket file with unlink("/tmp/cb_p.sock");; otherwise bind fails. Then bind(fd, (struct sockaddr *)&addr, sizeof(addr)) and listen(fd, 1). return fd;. In main add int sfd = server_setup(); and printf("soket: %d\n", sfd >= 0 ? 1 : 0);. The output gains "soket: 1".

Practice Add server_setup

Write int server_setup(void): int fd = socket(AF_UNIX, SOCK_STREAM, 0);, struct sockaddr_un addr;, memset(&addr, 0, sizeof(addr));, addr.sun_family = AF_UNIX;, strncpy(addr.sun_path, "/tmp/cb_p.sock", sizeof(addr.sun_path) - 1);, unlink("/tmp/cb_p.sock");, bind(fd, (struct sockaddr *)&addr, sizeof(addr));, listen(fd, 1);, return fd;. Add int sfd = server_setup(); and printf("soket: %d\n", sfd >= 0 ? 1 : 0); to main.

Starter code

main.c
#include <stdio.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>

int worker_init(void);
int server_setup(void);
int ipc_setup(void);

int worker_count = 0;
pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;

void *worker_fn(void *arg) {
    (void)arg;
    pthread_mutex_lock(&worker_lock);
    worker_count++;
    pthread_mutex_unlock(&worker_lock);
    return NULL;
}

int worker_init(void) {
    pthread_t t;
    pthread_create(&t, NULL, worker_fn, NULL);
    pthread_join(t, NULL);
    return 1;
}

// server_setup fonksiyonunu yaz: AF_UNIX soket oluştur, bind, listen yap
// soket tanıtıcısı döndür (>= 0 ise başarılı)

int main(void) {
    printf("Proje hazır\n");
    worker_init();
    printf("sayaç: %d\n", worker_count);
    // server_setup çağır ve "soket: 1" yazdır
    return 0;
}

Sample solution

main.c
#include <stdio.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <unistd.h>

int worker_init(void);
int server_setup(void);
int ipc_setup(void);

int worker_count = 0;
pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;

void *worker_fn(void *arg) {
    (void)arg;
    pthread_mutex_lock(&worker_lock);
    worker_count++;
    pthread_mutex_unlock(&worker_lock);
    return NULL;
}

int worker_init(void) {
    pthread_t t;
    pthread_create(&t, NULL, worker_fn, NULL);
    pthread_join(t, NULL);
    return 1;
}

int server_setup(void) {
    int fd = socket(AF_UNIX, SOCK_STREAM, 0);
    struct sockaddr_un addr;
    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, "/tmp/cb_p.sock", sizeof(addr.sun_path) - 1);
    unlink("/tmp/cb_p.sock");
    bind(fd, (struct sockaddr *)&addr, sizeof(addr));
    listen(fd, 1);
    return fd;
}

int main(void) {
    printf("Proje hazır\n");
    worker_init();
    printf("sayaç: %d\n", worker_count);
    int sfd = server_setup();
    printf("soket: %d\n", sfd >= 0 ? 1 : 0);
    return 0;
}

Expected output

Proje hazır
sayaç: 1
soket: 1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 socket call

Question
int fd = socket(AF_UNIX, SOCK_STREAM, ____);
Answer
int fd = socket(AF_UNIX, SOCK_STREAM, 0);

Explanation: The third parameter is 0; the default protocol.

2 Match socket steps

Term Meaning
socket Create socket
bind Bind to address
listen Start listening
unlink Remove old socket

3 What does bind do?

  • Binds socket to address Correct answer
  • Creates a socket
  • Accepts a connection
  • Sends data

Explanation: bind links the socket to an address (e.g. a file path).

All lessons in Advanced