C
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".
Exercise 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.
Show example solution
#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;
}
Test yourself
-
socket call
int fd = socket(AF_UNIX, SOCK_STREAM, ____);
Show answer
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
-
Match socket steps
- socket
- Create socket
- bind
- Bind to address
- listen
- Start listening
- unlink
- Remove old socket
-
What does bind do?
- Binds socket to address
- Creates a socket
- Accepts a connection
- Sends data
Show answer
Binds socket to address — bind links the socket to an address (e.g. a file path).
Next lesson: IPC and sync