Revision 4dda199d
Added by David Sorber over 10 years ago
| software/misc/unix_socket/Makefile | ||
|---|---|---|
|
CC = gcc
|
||
|
|
||
|
HEADERS = $(wildcard *.h)
|
||
|
SOURCES = $(wildcard *.c)
|
||
|
OBJECTS = $(patsubst %.c,%.o,$(SOURCES))
|
||
|
|
||
|
CFLAGS += -O2 -Wall -std=c99
|
||
|
CWD := $(shell pwd)
|
||
|
|
||
|
all: server client
|
||
|
|
||
|
server: server.c
|
||
|
$(CC) $(CFLAGS) server.c -o server
|
||
|
|
||
|
client: client.c
|
||
|
$(CC) $(CFLAGS) client.c -o client
|
||
|
|
||
|
clean:
|
||
|
@rm -f *.o
|
||
|
@rm -f server
|
||
|
@rm -f client
|
||
| software/misc/unix_socket/client.c | ||
|---|---|---|
|
#include <stdio.h>
|
||
|
#include <sys/socket.h>
|
||
|
#include <sys/un.h>
|
||
|
#include <unistd.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#define UNIX_PATH_MAX 108
|
||
|
|
||
|
int main(void)
|
||
|
{
|
||
|
struct sockaddr_un address;
|
||
|
int socket_fd, nbytes;
|
||
|
char buffer[256];
|
||
|
|
||
|
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
||
|
if (socket_fd < 0)
|
||
|
{
|
||
|
printf("socket() failed\n");
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
/* start with a clean address structure */
|
||
|
memset(&address, 0, sizeof(struct sockaddr_un));
|
||
|
|
||
|
address.sun_family = AF_UNIX;
|
||
|
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
|
||
|
|
||
|
if (connect(socket_fd, (struct sockaddr *)&address,
|
||
|
sizeof(struct sockaddr_un)) != 0)
|
||
|
{
|
||
|
printf("connect() failed\n");
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
nbytes = snprintf(buffer, 256, "hello from a client");
|
||
|
write(socket_fd, buffer, nbytes);
|
||
|
|
||
|
nbytes = read(socket_fd, buffer, 256);
|
||
|
buffer[nbytes] = 0;
|
||
|
|
||
|
printf("MESSAGE FROM SERVER: %s\n", buffer);
|
||
|
|
||
|
close(socket_fd);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
| software/misc/unix_socket/server.c | ||
|---|---|---|
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#include <sys/socket.h>
|
||
|
#include <sys/un.h>
|
||
|
#include <sys/types.h>
|
||
|
|
||
|
#define UNIX_PATH_MAX 108
|
||
|
|
||
|
|
||
|
int connection_handler(int connection_fd)
|
||
|
{
|
||
|
int nbytes;
|
||
|
char buffer[256];
|
||
|
|
||
|
nbytes = read(connection_fd, buffer, 256);
|
||
|
buffer[nbytes] = 0;
|
||
|
|
||
|
printf("MESSAGE FROM CLIENT: %s\n", buffer);
|
||
|
nbytes = snprintf(buffer, 256, "hello from the server");
|
||
|
write(connection_fd, buffer, nbytes);
|
||
|
|
||
|
close(connection_fd);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int main(void)
|
||
|
{
|
||
|
struct sockaddr_un address;
|
||
|
int socket_fd, connection_fd;
|
||
|
socklen_t address_length;
|
||
|
pid_t child;
|
||
|
|
||
|
socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
||
|
if (socket_fd < 0)
|
||
|
{
|
||
|
printf("socket() failed\n");
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
unlink("./demo_socket");
|
||
|
|
||
|
/* start with a clean address structure */
|
||
|
memset(&address, 0, sizeof(struct sockaddr_un));
|
||
|
|
||
|
address.sun_family = AF_UNIX;
|
||
|
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
|
||
|
|
||
|
if (bind(socket_fd, (struct sockaddr *)&address,
|
||
|
sizeof(struct sockaddr_un)) != 0)
|
||
|
{
|
||
|
printf("bind() failed\n");
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
if (listen(socket_fd, 5) != 0)
|
||
|
{
|
||
|
printf("listen() failed\n");
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
while ((connection_fd = accept(socket_fd, (struct sockaddr *)&address,
|
||
|
&address_length)) > -1)
|
||
|
{
|
||
|
child = fork();
|
||
|
if (child == 0)
|
||
|
{
|
||
|
/* now inside newly created connection handling process */
|
||
|
return connection_handler(connection_fd);
|
||
|
}
|
||
|
|
||
|
/* still inside server process */
|
||
|
close(connection_fd);
|
||
|
}
|
||
|
|
||
|
close(socket_fd);
|
||
|
unlink("./demo_socket");
|
||
|
return 0;
|
||
|
}
|
||
Adding a very simple unix domain socket example client and server.