mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-26 07:46:21 +02:00
* Benchmarking Unix domain sockets.
This commit is contained in:
parent
fe15f991e3
commit
5f0b9de6d8
3 changed files with 121 additions and 0 deletions
7
socket/Makefile
Normal file
7
socket/Makefile
Normal file
|
@ -0,0 +1,7 @@
|
|||
all: server client
|
||||
|
||||
server: server.c
|
||||
gcc -Wall -o server server.c
|
||||
|
||||
client: client.c
|
||||
gcc -Wall -o client client.c
|
43
socket/client.c
Normal file
43
socket/client.c
Normal file
|
@ -0,0 +1,43 @@
|
|||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
|
||||
#define SOCKET_PATH "/tmp/nix-daemon"
|
||||
|
||||
|
||||
int main(int argc, char * * argv)
|
||||
{
|
||||
int res;
|
||||
|
||||
int sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
||||
assert(sock != -1);
|
||||
|
||||
struct sockaddr_un addr;
|
||||
addr.sun_family = AF_UNIX;
|
||||
strcpy(addr.sun_path, SOCKET_PATH);
|
||||
|
||||
res = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
|
||||
assert(res != -1);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < 100000; i++) {
|
||||
int len = send(sock, &i, sizeof(i), 0);
|
||||
assert(len == sizeof(i));
|
||||
|
||||
int j;
|
||||
len = recv(sock, &j, sizeof(j), 0);
|
||||
if (len < sizeof(j)) break;
|
||||
assert(i * 2 == j);
|
||||
}
|
||||
|
||||
close(sock);
|
||||
|
||||
return 0;
|
||||
}
|
71
socket/server.c
Normal file
71
socket/server.c
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/un.h>
|
||||
|
||||
|
||||
#define SOCKET_PATH "/tmp/nix-daemon"
|
||||
|
||||
|
||||
int main(int argc, char * * argv)
|
||||
{
|
||||
int res;
|
||||
|
||||
int sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
||||
assert(sock != -1);
|
||||
|
||||
unlink(SOCKET_PATH);
|
||||
|
||||
struct sockaddr_un addr;
|
||||
addr.sun_family = AF_UNIX;
|
||||
strcpy(addr.sun_path, SOCKET_PATH);
|
||||
|
||||
res = bind(sock, (struct sockaddr *) &addr, sizeof(addr));
|
||||
assert(res != -1);
|
||||
|
||||
res = listen(sock, 5);
|
||||
if (res == -1)
|
||||
fprintf(stderr, "%s\n", strerror(errno));
|
||||
assert(res != -1);
|
||||
|
||||
while (1) {
|
||||
|
||||
struct sockaddr_un remoteAddr;
|
||||
socklen_t remoteAddrLen = sizeof(remoteAddr);
|
||||
|
||||
int remote = accept(sock,
|
||||
(struct sockaddr *) &remoteAddr, &remoteAddrLen);
|
||||
if (remote == -1)
|
||||
fprintf(stderr, "%s\n", strerror(errno));
|
||||
assert(remote != -1);
|
||||
|
||||
fprintf(stderr, "connection %d\n", remote);
|
||||
|
||||
while (1) {
|
||||
int i;
|
||||
ssize_t len;
|
||||
|
||||
len = recv(remote, &i, sizeof(i), 0);
|
||||
if (len < sizeof(i)) break;
|
||||
|
||||
// printf("%d\n", i);
|
||||
|
||||
int j = i * 2;
|
||||
len = send(remote, &j, sizeof(j), 0);
|
||||
if (len == -1)
|
||||
fprintf(stderr, "%s\n", strerror(errno));
|
||||
assert(len == sizeof(j));
|
||||
}
|
||||
|
||||
close(remote);
|
||||
}
|
||||
|
||||
close(sock);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue