Last active
August 28, 2022 04:03
-
-
Save jweinst1/59728ef16a9b90db1441b8be2919e22a to your computer and use it in GitHub Desktop.
sample unix client and server program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <assert.h> | |
#include <signal.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <sys/un.h> | |
#include <sys/socket.h> | |
const char* SOCKET_FILE = "/tmp/fgfgdgdgff"; | |
#define BUFFER_SPACE 100 | |
static void do_client(void) { | |
pid_t child = fork(); | |
if (child != 0) { | |
return; | |
} | |
struct sockaddr_un addr; | |
int sfd = socket(AF_UNIX, SOCK_STREAM, 0); | |
if (sfd == -1) { | |
fprintf(stderr, "Could not create socket, err=%d\n", errno); | |
exit(4); | |
} | |
memset(&addr, 0, sizeof(struct sockaddr_un)); | |
addr.sun_family = AF_UNIX; | |
strncpy(addr.sun_path, SOCKET_FILE, sizeof(addr.sun_path) - 1); | |
while (connect(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) { | |
if (errno == ECONNREFUSED) { | |
fprintf(stderr, "connection refused, retrying\n"); | |
continue; | |
} | |
fprintf(stderr, "Failed to connect to socket, err=%d\n", errno); | |
exit(4); | |
} | |
if (write(sfd, "Foody", 5) != 5) { | |
fprintf(stderr, "Failed to write to socket, err=%d\n", errno); | |
exit(5); | |
} | |
if (write(sfd, "Foody", 5) != 5) { | |
fprintf(stderr, "Failed to write to socket, err=%d\n", errno); | |
exit(5); | |
} | |
if (write(sfd, "Foody", 5) != 5) { | |
fprintf(stderr, "Failed to write to socket, err=%d\n", errno); | |
exit(5); | |
} | |
exit(0); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
puts("Unix Socket"); | |
signal(SIGPIPE, SIG_IGN); | |
// Make sure to handle removal of socket | |
if (remove(SOCKET_FILE) == -1 && errno != ENOENT) { | |
fprintf(stderr, "Failed to remove socket path %s\n", SOCKET_FILE); | |
exit(4); | |
} | |
struct sockaddr_un addr; | |
int sfd; | |
int cfd; | |
ssize_t numRead; | |
char buf[BUFFER_SPACE]; | |
sfd = socket(AF_UNIX, SOCK_STREAM, 0); | |
if (sfd == -1) { | |
fprintf(stderr, "Failed to create socket, err=%d\n", errno); | |
exit(4); | |
} | |
if (strlen(SOCKET_FILE) > sizeof(addr.sun_path) - 1) { | |
fprintf(stderr, "Socket name too long, socket sun path is size %zu\n", sizeof(addr.sun_path) - 1); | |
exit(1); | |
} | |
memset(&addr, 0, sizeof(struct sockaddr_un)); | |
addr.sun_family = AF_UNIX; | |
strncpy(addr.sun_path, SOCKET_FILE, sizeof(addr.sun_path) - 1); | |
if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) { | |
fprintf(stderr, "Failed to bind, err=%d\n", errno); | |
exit(3); | |
} | |
if (listen(sfd, 5) == -1) { | |
fprintf(stderr, "Failed to listen err=%d\n", errno); | |
exit(2); | |
} | |
do_client(); | |
while (1) { | |
cfd = accept(sfd, NULL, NULL); | |
if (cfd == -1) { | |
fprintf(stderr, "Cannot accept, err=%d\n", errno); | |
exit(5); | |
} | |
while ((numRead = read(cfd, buf, BUFFER_SPACE)) > 0) { | |
if (write(STDOUT_FILENO, buf, numRead) != numRead) { | |
fprintf(stderr, "Cannot write to stdout, err=%d\n", errno); | |
exit(6); | |
} | |
} | |
if (numRead == -1) { | |
fprintf(stderr, "Error while reading, err=%d\n", errno); | |
exit(8); | |
} | |
if (close(cfd) == -1) { | |
fprintf(stderr, "Cannot close connected fd, err=%d\n", errno); | |
exit(9); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment