mirror of
https://github.com/Hizenberg469/Inter-Process-Communication-IPC-.git
synced 2026-04-20 02:12:25 +03:00
88 lines
1.8 KiB
C
88 lines
1.8 KiB
C
#include <error.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/un.h>
|
|
#include <unistd.h>
|
|
|
|
#define SOCKET_NAME "/tmp/DemoSocket"
|
|
#define BUFFER_SIZE 128
|
|
|
|
int
|
|
main(int argc, char* argv[]) {
|
|
|
|
struct sockaddr_un addr;
|
|
int i;
|
|
int ret;
|
|
int data_socket;
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
/* Create data socket */
|
|
/* Client side don't have any concept of Master socket.
|
|
It directly use data socket to communicate with server. */
|
|
|
|
data_socket = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (data_socket == -1) {
|
|
perror("socket");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
/*
|
|
For portability clear the whole structure, since some implementation
|
|
have additional (nonstandard) fields in the structure.
|
|
*/
|
|
|
|
memset(&addr, 0, sizeof(struct sockaddr_un));
|
|
|
|
/* Connect socket to socket address. */
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path) - 1);
|
|
|
|
ret = connect(data_socket, (const struct sockaddr*)&addr,
|
|
sizeof(struct sockaddr_un));
|
|
|
|
if (ret == -1) {
|
|
fprintf(stderr, "The server is down.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
/* Send arguments */
|
|
do {
|
|
|
|
printf("Enter number to send to server :\n");
|
|
scanf("%d", &i);
|
|
ret = write(data_socket, &i, sizeof(int));
|
|
if (ret == -1) {
|
|
perror("write");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
printf("No of bytes sent = %d, data sent = %d\n", ret, i);
|
|
} while (i);
|
|
|
|
/* Request result */
|
|
|
|
memset(buffer, 0, BUFFER_SIZE);
|
|
strncpy(buffer, "RES", strlen("RES"));
|
|
buffer[strlen(buffer)] = '\0';
|
|
|
|
ret = read(data_socket, buffer, BUFFER_SIZE);
|
|
if (ret == -1) {
|
|
perror("read");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
/* Ensure buffer is 0 - terminated */
|
|
|
|
buffer[BUFFER_SIZE - 1] = 0;
|
|
printf("%s\n", buffer);
|
|
|
|
/* Close socket */
|
|
|
|
close(data_socket);
|
|
exit(EXIT_SUCCESS);
|
|
return 0;
|
|
} |