Created
June 14, 2010 05:17
-
-
Save fictorial/437301 to your computer and use it in GitHub Desktop.
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 <errno.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <sys/un.h> | |
#define SOCK_PATH "/tmp/test.sock" | |
int main(void) { | |
int s, t, len; | |
struct sockaddr_un remote; | |
char str[100]; | |
if ((s = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1) { | |
perror("socket"); | |
exit(1); | |
} | |
printf("Trying to connect...\n"); | |
remote.sun_family = AF_UNIX; | |
strcpy(remote.sun_path, SOCK_PATH); | |
len = strlen(remote.sun_path) + sizeof(remote.sun_family); | |
if (connect(s, (struct sockaddr *)&remote, len) == -1) { | |
perror("connect"); | |
exit(1); | |
} | |
printf("Connected.\n"); | |
while(printf("> "), fgets(str, 100, stdin), !feof(stdin)) { | |
if (send(s, str, strlen(str)-1, 0) == -1) { | |
perror("send"); | |
exit(1); | |
} | |
if ((t=recv(s, str, 100, 0)) > 0) { | |
str[t] = '\0'; | |
printf("echo> %s", str); | |
} else { | |
if (t < 0) perror("recv"); | |
else printf("Server closed connection\n"); | |
exit(1); | |
} | |
} | |
close(s); | |
return 0; | |
} |
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
$ node -v | |
v0.1.98-5-g02da5ed | |
$ node server.js | |
unix socket open: /tmp/test.sock | |
In another terminal: | |
$ gcc -o client client.c | |
$ ./client | |
Trying to connect... | |
Connected. | |
> hi | |
In first terminal: | |
server got: hi | |
server got: hi | |
server got: hi | |
server got: hi | |
server got: hi | |
server got: hi | |
server got: hi | |
^C | |
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
#!/usr/bin/env node | |
var sys = require('sys'), | |
dgram = require("dgram"), | |
sock_path = '/tmp/test.sock'; | |
var server = dgram.createSocket(function (msg) { | |
sys.puts("server got: " + msg); // msg is a Buffer | |
server.send(sock_path, null, msg); | |
}); | |
server.addListener("error", function (e) { | |
sys.error('got an error: ' + e); | |
throw e; | |
}); | |
server.addListener("listening", function () { | |
sys.puts("unix socket open: " + sock_path); | |
}); | |
server.addListener("close", function (e) { | |
sys.puts("unix socket closed: " + sock_path); | |
}); | |
server.bind(sock_path); | |
[ 'SIGTERM', 'SIGINT', 'SIGKILL', 'SIGQUIT' ].forEach(function (sig) { | |
process.addListener(sig, function () { | |
sys.puts(sig + ": closing unix socket"); | |
server.close(); | |
}); | |
}); |
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
diff --git a/test/simple/test-dgram-pingpong.js b/test/simple/test-dgram-pingpong.js | |
index 61a736a..070d32a 100644 | |
--- a/test/simple/test-dgram-pingpong.js | |
+++ b/test/simple/test-dgram-pingpong.js | |
@@ -14,11 +14,11 @@ function pingPongTest (port, host) { | |
puts("server got: " + msg); | |
- if (/PING/.exec(msg)) { | |
+// if (/PING/.exec(msg)) { | |
var buf = new Buffer(4); | |
buf.write('PONG'); | |
- server.send(rinfo.port, rinfo.address, buf, 0, buf.length); | |
- } | |
+ server.send(port /*rinfo.port*/, rinfo.address, buf, 0, buf.length); | |
+// } | |
}); | |
@@ -71,11 +71,11 @@ function pingPongTest (port, host) { | |
} | |
/* All are run at once, so run on different ports */ | |
+pingPongTest("/tmp/pingpong.sock"); | |
pingPongTest(20989, "localhost"); | |
pingPongTest(20990, "localhost"); | |
pingPongTest(20988); | |
pingPongTest(20997, "::1"); | |
-//pingPongTest("/tmp/pingpong.sock"); | |
process.addListener("exit", function () { | |
assert.equal(4, tests_run); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The answer here (for the record) was that the communication is unidirectional.
Just use 2 pipes for bidirectional.