70 unsigned long nonblocking = 1;
71 WORD wVersionRequested;
73 static bool started=
false;
77 wVersionRequested = MAKEWORD( 1, 1 );
78 iretn = WSAStartup( wVersionRequested, &wsaData );
91 if ((channel.
cudp = socket(AF_INET,SOCK_DGRAM,IPPROTO_IP)) <0)
100 if ((channel.
cudp = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) <0)
108 if ((channel.
cudp = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)) <0)
123 if (ioctlsocket(channel.
cudp, FIONBIO, &nonblocking) != 0)
125 iretn = -WSAGetLastError();
128 if (fcntl(channel.
cudp, F_SETFL,O_NONBLOCK) < 0)
135 CLOSE_SOCKET(channel.
cudp);
145 int msectimeo = usectimeo/1000;
146 iretn = setsockopt(channel.
cudp,SOL_SOCKET,SO_RCVTIMEO,(
const char *)&msectimeo,
sizeof(msectimeo));
149 tv.tv_sec = usectimeo/1000000;
150 tv.tv_usec = usectimeo%1000000;
151 iretn = setsockopt(channel.
cudp,SOL_SOCKET,SO_RCVTIMEO,(
char*)&tv,
sizeof(tv));
155 memset(&channel.
caddr,0,
sizeof(
struct sockaddr_in));
156 channel.
caddr.sin_family = AF_INET;
163 if (setsockopt(channel.
cudp,SOL_SOCKET,SO_REUSEPORT,(
char*)&on,
sizeof(on)) < 0)
165 if (setsockopt(channel.
cudp,SOL_SOCKET,SO_REUSEADDR,(
char*)&on,
sizeof(on)) < 0)
168 CLOSE_SOCKET(channel.
cudp);
169 channel.
cudp = -errno;
179 channel.
caddr.sin_addr.s_addr = htonl(INADDR_ANY);
180 if (::bind(channel.
cudp,(
struct sockaddr *)&channel.
caddr,
sizeof(
struct sockaddr_in)) < 0)
182 CLOSE_SOCKET(channel.
cudp);
183 channel.
cudp = -errno;
190 channel.
caddr.sin_addr.s_addr = htonl(INADDR_ANY);
192 if (::bind(channel.
cudp,(
struct sockaddr *)&channel.
caddr,
sizeof(
struct sockaddr_in)) < 0)
194 CLOSE_SOCKET(channel.
cudp);
195 channel.
cudp = -errno;
199 if (listen(channel.
cudp, 1) < 0)
201 CLOSE_SOCKET(channel.
cudp);
202 channel.
cudp = -errno;
215 inet_pton(AF_INET,
address,&mreq.imr_multiaddr.s_addr);
218 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
219 if (setsockopt(channel.
cudp, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
char*)&mreq,
sizeof(mreq)) < 0)
221 CLOSE_SOCKET(channel.
cudp);
222 channel.
cudp = -errno;
231 if ((iretn=setsockopt(channel.
cudp,SOL_SOCKET,SO_BROADCAST,(
char*)&on,
sizeof(on))) < 0)
233 CLOSE_SOCKET(channel.
cudp);
234 channel.
cudp = -errno;
237 channel.
caddr.sin_addr.s_addr = 0xffffffff;
254 if (connect(channel.
cudp,(
struct sockaddr *)&channel.
caddr,
sizeof(
struct sockaddr_in)) < 0 && errno != EINPROGRESS)
256 CLOSE_SOCKET(channel.
cudp);
257 channel.
cudp = -errno;
266 channel.
baddr.sin_addr.s_addr |= 0xff;
271 channel.
cport = ntohs(channel.
caddr.sin_port);
274 iretn = sendto(channel.
cudp, (
const char *)
nullptr, 0, 0, (
struct sockaddr *)&channel.
baddr,
sizeof(
struct sockaddr_in));
275 sockaddr_in taddr = channel.
caddr;
276 socklen_t namelen =
sizeof(
struct sockaddr_in);
277 if ((iretn = getsockname(channel.
cudp, (sockaddr*)&channel.
caddr, &namelen)) == -1)
279 CLOSE_SOCKET(channel.
cudp);
280 channel.
cudp = -errno;
283 channel.
cport = ntohs(channel.
caddr.sin_port);
284 channel.
caddr = taddr;
290 setsockopt(channel.
cudp, SOL_SOCKET, SO_RCVBUF, (
char *)&rcvbuf, 4);
295 setsockopt(channel.
cudp, SOL_SOCKET, SO_SNDBUF, (
char *)&sndbuf, 4);
300 channel.
addrlen =
sizeof(
struct sockaddr_in);
static string port
Definition: add_radio.cpp:16
Agent socket using Unicast UDP.
Agent socket using Unicast TCP.
#define SOCKET_ERROR_PROTOCOL
Definition: cosmos-errno.h:230
int iretn
Definition: rw_test.cpp:37
#define SOCKET_ERROR_OPEN
Definition: cosmos-errno.h:232
struct sockaddr_in baddr
Definition: socketlib.h:124
char address[17]
Definition: socketlib.h:134
#define SOCKET_NONBLOCKING
Non-blocking Agent.
Definition: socketlib.h:80
#define SOCKET_TALK
Talk followed by optional listen (sendto address)
Definition: socketlib.h:82
uint16_t cport
Definition: socketlib.h:130
char address[]
Definition: netperf_listen.cpp:69
int32_t cudp
Definition: socketlib.h:120
NetworkType type
Definition: socketlib.h:118
struct sockaddr_in caddr
Definition: socketlib.h:122
int addrlen
Definition: socketlib.h:128
Agent socket using Broadcast UDP.
NetworkType ntype
Definition: agent_node.cpp:50
#define SOCKET_LISTEN
Listen followed by optional talk (recvfrom INADDRANY)
Definition: socketlib.h:84
#define SOCKET_JABBER
Talk over multiple interfaces.
Definition: socketlib.h:88
Agent socket using Multicast UDP.