14 #include "../../include/ecl/io/socketpair.hpp" 26 struct sockaddr_in inaddr;
29 socklen_t addrlen =
sizeof(a.inaddr);
35 listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
36 if (listen_socket == INVALID_SOCKET) {
42 if (setsockopt(listen_socket, SOL_SOCKET, SO_REUSEADDR, (
char*) &reuse, (socklen_t)
sizeof(reuse)) == SOCKET_ERROR ) {
43 ::closesocket(listen_socket);
47 memset(&a, 0,
sizeof(a));
48 a.inaddr.sin_family = AF_INET;
49 a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
52 a.inaddr.sin_port = 0;
54 if (bind(listen_socket, &a.addr,
sizeof(a.inaddr)) == SOCKET_ERROR) {
55 ::closesocket(listen_socket);
59 if (getsockname(listen_socket, &a.addr, &addrlen) == SOCKET_ERROR) {
60 ::closesocket(listen_socket);
64 if (listen(listen_socket, 1) == SOCKET_ERROR) {
65 ::closesocket(listen_socket);
72 socket_fd_pair[0] = socket_fd_pair[1] = INVALID_SOCKET;
75 DWORD overlapped_flag = 0;
76 socket_fd_pair[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, overlapped_flag);
77 if (socket_fd_pair[0] == INVALID_SOCKET) {
78 ::closesocket(listen_socket);
79 ::closesocket(socket_fd_pair[0]);
83 if (connect(socket_fd_pair[0], &a.addr,
sizeof(a.inaddr)) == SOCKET_ERROR) {
84 ::closesocket(listen_socket);
85 ::closesocket(socket_fd_pair[0]);
91 socket_fd_pair[1] = accept(listen_socket, NULL, NULL);
92 if (socket_fd_pair[1] == INVALID_SOCKET) {
93 ::closesocket(listen_socket);
94 ::closesocket(socket_fd_pair[0]);
95 ::closesocket(socket_fd_pair[1]);
102 unsigned long non_blocking_flag = 0;
103 if ( non_blocking ) {
104 non_blocking_flag = 1;
105 if(ioctlsocket( socket_fd_pair[0], FIONBIO, &non_blocking_flag ) != 0 ) {
106 ::closesocket(listen_socket);
107 ::closesocket(socket_fd_pair[0]);
108 ::closesocket(socket_fd_pair[1]);
111 if(ioctlsocket( socket_fd_pair[1], FIONBIO, &non_blocking_flag ) != 0 ) {
112 ::closesocket(listen_socket);
113 ::closesocket(socket_fd_pair[0]);
114 ::closesocket(socket_fd_pair[1]);
122 ::closesocket(listen_socket);
127 if ( non_blocking ) {
128 #if defined(SOCK_NONBLOCK) 129 result =
::socketpair(AF_LOCAL, SOCK_STREAM|SOCK_NONBLOCK, 0, socket_fd_pair);
131 result =
::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fd_pair);
ecl_io_PUBLIC SocketError socketpair(socket_descriptor socket_fd_pair[2], const bool non_blocking=false)
Creates a socket pair internal to the current process.
int socket_descriptor
Cross-platform typedef for a socket file descriptor.
Extends the generic error handler with socket specific error strings.