AF_LOCAL和AF_INET两个套接口同时监听
(2013-02-28 16:38:17)
标签:
af_localaf_inet |
分类: 网络编程 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #include <unistd.h>
- #include <netdb.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/types.h>
- #include <arpa/inet.h>
- #define SA struct sockaddr
- int main(int argc, char **argv)
- {
-
int use_tcp = 1; -
int use_unix = 1; -
int fdmax = 0; -
int rt = 0; -
int fdtcp, fdunix; -
if (!getenv("USEUNIX")) -
use_unix = 0; -
if (!getenv("USETCP")) -
use_tcp = 0; -
if (use_tcp) { -
// Enable TCP server socket -
fdtcp = socket(AF_INET, SOCK_STREAM, 0); -
if (fdtcp < 0) { -
perror("socket( TCP )"); -
} -
fdmax = fdmax > fdtcp ? fdmax : fdtcp; -
struct sockaddr_in servaddr; -
bzero(&servaddr, sizeof(servaddr)); -
servaddr.sin_family = AF_INET; -
servaddr.sin_port = htons(1235); -
int reuseaddr = 1; -
socklen_t reuseaddr_len = sizeof(reuseaddr); -
rt = setsockopt(fdtcp, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, -
reuseaddr_len); -
rt = bind(fdtcp, (SA *) & servaddr, sizeof(servaddr)); -
if (rt < 0) { -
perror("bind( TCP )"); -
exit(1); -
} -
listen(fdtcp, 20); -
} -
if (use_unix) { -
// Get ready the UNIX Domain Socket -
fdunix = socket(AF_UNIX, SOCK_STREAM, 0); -
if (fdunix < 0) { -
perror("socket( UNIX )"); -
exit(1); -
} -
fdmax = fdmax > fdunix ? fdmax : fdunix; -
struct sockaddr_un unixaddr; -
bzero(&unixaddr, sizeof(unixaddr)); -
unixaddr.sun_family = AF_UNIX; -
strncpy(&unixaddr.sun_path[0], "/tmp/test-unix.0", 17); -
rt = bind(fdunix, (SA *) & unixaddr, sizeof(unixaddr)); -
//unlink( "/tmp/test-unix.0" ); -
if (rt < 0) { -
perror("bind( UNIX )"); -
exit(1); -
} -
rt = listen(fdunix, 20); -
if (rt < 0) { -
perror("listen( UNIX )"); -
exit(1); -
} -
} -
int selectrt = 0; -
while (use_tcp || use_unix) { -
fd_set ssset; -
FD_ZERO(&ssset);
-
if (use_tcp) -
FD_SET(fdtcp, &ssset); -
if (use_unix) -
FD_SET(fdunix, &ssset); -
selectrt = select(fdmax + 1, &ssset, NULL, NULL, NULL); -
while (selectrt > 0) { -
if (use_tcp) { -
if (FD_ISSET(fdtcp, &ssset)) { -
printf("TCP Server is ready.\n"); -
struct sockaddr_in caddr; -
bzero(&caddr, sizeof(caddr)); -
socklen_t clen = sizeof(caddr); -
int client = accept(fdtcp, (SA *) & caddr, &clen); -
write(client, "HELO", 5); -
close(client);
-
--selectrt;
-
} -
} -
if (use_unix) { -
if (FD_ISSET(fdunix, &ssset)) { -
printf("UNIX Server is ready.\n"); -
struct sockaddr_un caddr; -
bzero(&caddr, sizeof(caddr)); -
socklen_t clen = sizeof(caddr); -
int client = accept(fdunix, (SA *) & caddr, &clen); -
rt = write(client, "HELO", 5); -
printf("UNIX Socket wrote %d bytes off.\n", rt); -
close(client);
-
--selectrt;
-
} -
} -
} -
if ( selectrt < 0 ) { -
perror( "select()" ); -
exit( 1 ); -
} -
FD_ZERO(&ssset);
-
} - }
- $ make sockd
- cc
sockd.c -o sockd - $ env USETCP='' USEUNIX='' ./sockd
- UNIX Server is ready.
- UNIX Socket wrote 5 bytes off.
- UNIX Server is ready.
- UNIX Socket wrote 5 bytes off.
- TCP Server is ready.
- $ ./sockclient /tmp/test-unix.0
- UNIX Socket read 5 bytes.
- HELO
- $ ./sockclient /tmp/test-unix.0
- UNIX Socket read 5 bytes.
- HELO
- $ nc localhost 1235 -vvv
- gydebian [127.0.0.1] 1235 (?) open
- HELO sent 0, rcvd 5
前一篇:socket编程中一些重要知识点
后一篇:Linux文件类型或目录的属性