My code:
when run as
C:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/procdesc.h>
#define FALSE 0
#define TRUE 1
static const char *socket_path = "/tmp/test_socket";
static const unsigned int nIncomingConnections = 5;
int client(const int socket_var, const struct sockaddr_un *socket_address);
int server(const int socket_var, const struct sockaddr_un *socket_address);
int main(int argc, char **argv) {
int option = -1;
int socket_var = 0;
if (argc < 2) {
fprintf(stderr, "Nicht genug Funktionsargumente\n");
return EXIT_FAILURE;
}
if (strstr(argv[1], "dienster") != 0) {
printf("Dienster gewählt\n");
option = 2;
} else if (strstr(argv[1], "emptor") != 0) {
printf("Emptor gewählt\n");
option = 1;
} else {
fprintf(stderr, "Gewählt nicht existiert\n");
return EXIT_FAILURE;
};
if ((socket_var = socket(AF_UNIX, SOCK_STREAM, 0) == -1)) {
fprintf(stderr, "Fehler im socket() anrufen\n");
return EXIT_FAILURE;
}
struct sockaddr_un *socket_address = (struct sockaddr_un*) calloc(1, sizeof(struct sockaddr_un));
socket_address->sun_family = AF_UNIX;
strcpy(socket_address->sun_path, socket_path);
socket_address->sun_len = SUN_LEN(socket_address);
int casertn = 0;
switch (option) {
case 1:
casertn = client(socket_var, socket_address);
break;
case 2:
casertn = server(socket_var, socket_address);
break;
default:
free(socket_address);
return EXIT_FAILURE;
};
free(socket_address);
if (casertn != 0) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int client(const int socket_var, const struct sockaddr_un *socket_address) {
int return_code = 0; // 0 für erfolgreich
printf("Versucht zu socket verbinden\n");
int data_len = strlen(socket_address->sun_path) + sizeof(socket_path);
if (connect(socket_var, (struct sockaddr*) socket_address, data_len) == -1) {
fprintf(stderr, "Fehler im connect() anruhfen\n");
return_code = -1;
goto fazit;
}
printf("Emptor: Verbindet\n");
fazit:
return return_code;
}
int server(const int socket_var, const struct sockaddr_un *socket_address) {
unlink(socket_path);
int return_code = 0;
printf("Versucht zu bind() anruhfen\n");
if (bind(socket_var, (struct sockaddr*) socket_address, socket_address->sun_len) != 0) {
fprintf(stderr, "Fehler im socket bind() anruhfen\n");
return_code = -1;
goto fazit;
}
if (listen(socket_var, nIncomingConnections) != 0) {
fprintf(stderr, "Fehler im listen() anruhfen\n");
return_code = -2;
goto fazit;
}
char bWaiting = TRUE;
while (bWaiting) {
int s2 = 0;
unsigned int sock_len = 0;
printf("Warten auf Verbindung\n");
if ((s2 = accept(socket_var, NULL, NULL)) == -1) {
fprintf(stdout, "Fehler accept() anruhfen\n");
break;
}
printf("Dienster im Verbindung\n");
int data_recv = 0;
char recv_buf[100];
do {
memset(recv_buf, 0, 100*sizeof(char));
data_recv = recv(s2, recv_buf, 100, 0);
if (data_recv > 0) {
printf("Data received: %d : %s \n", data_recv, recv_buf);
if (strstr(recv_buf, "quit") != 0) {
printf("Exit command received -> quitting \n");
bWaiting = FALSE;
break;
}
} else {
fprintf(stderr, "Fehler im recv() anruhfen\n");
break;
}
} while (data_recv > 0);
close(s2);
}
fazit:
return return_code;
}
cc main.c -o main && ./main dienster
is giving informing me that is failing to bind the Unix socket.