Open UDP socket.
Open a UDP socket and configure it for the specified use. Various flags are set, and the socket is bound, if necessary. Support is provided for the extra steps necessary for MS Windows.
774 unsigned long nonblocking = 1;
775 struct sockaddr_storage ss;
777 WORD wVersionRequested;
779 static bool started=
false;
782 if (debug){std::cout <<
"\nInitialising Winsock...";}
785 wVersionRequested = MAKEWORD( 1, 1 );
789 if ( (iretn=WSAStartup(wVersionRequested,&wsaData)) != 0)
791 if (debug){printf(
"Failed. Error Code : %d",WSAGetLastError());
795 if (debug){std::cout <<
"Initialised." << std::endl;}
801 if ((
sok.
handle = socket(AF_INET, SOCK_STREAM,0)) <0){
805 printf(
"Could not create socket stream: %d" , WSAGetLastError());
807 printf(
"Could not create socket stream : %d" , errno);
815 if ((
sok.
handle = socket(AF_INET, SOCK_DGRAM,0)) <0){
819 printf(
"Could not create socket : %d" , WSAGetLastError());
821 printf(
"Could not create socket : %d" , errno);
829 if (debug){std::cout <<
"Socket created" << std::endl;}
835 if (ioctlsocket(
sok.
handle, FIONBIO, &nonblocking) != 0)
837 iretn = -WSAGetLastError();
840 if (fcntl(
sok.
handle, F_SETFL,O_NONBLOCK) < 0)
858 iretn = setsockopt(
sok.
handle,SOL_SOCKET,SO_RCVTIMEO,(
const char *)&msectimeo,
sizeof(msectimeo));
863 iretn = setsockopt(
sok.
handle,SOL_SOCKET,SO_RCVTIMEO,(
char*)&tv,
sizeof(tv));
868 memset(&
sok.
server, 0,
sizeof(
struct sockaddr_in));
876 if (setsockopt(
sok.
handle,SOL_SOCKET,SO_REUSEPORT,(
char*)&on,
sizeof(on)) < 0)
878 if (setsockopt(
sok.
handle,SOL_SOCKET,SO_REUSEADDR,(
char*)&on,
sizeof(on)) < 0)
886 sok.
server.sin_addr.s_addr = htonl(INADDR_ANY);
893 printf(
"Bind failed with error code : %d" , WSAGetLastError());
901 if (debug){std::cout <<
"Bind done" << std::endl;}
906 namelen =
sizeof(
struct sockaddr_in);
924 mreq.imr_multiaddr.s_addr = inet_addr(
sok.
address.c_str());
925 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
926 if (setsockopt(
sok.
handle, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
char*)&mreq,
sizeof(mreq)) < 0)
940 if ((iretn=setsockopt(
sok.
handle,SOL_SOCKET,SO_BROADCAST,(
char*)&on,
sizeof(on))) < 0)
949 #ifndef COSMOS_WIN_OS 953 WSAStringToAddressA((LPSTR)
sok.
address.c_str(),AF_INET,NULL,(
struct sockaddr*)&ss,&sslen);
954 sok.
server.sin_addr = ((
struct sockaddr_in *)&ss)->sin_addr;
966 #ifndef COSMOS_WIN_OS 970 WSAStringToAddressA((LPSTR)
sok.
address.c_str(),AF_INET,NULL,(
struct sockaddr*)&ss,&sslen);
971 sok.
server.sin_addr = ((
struct sockaddr_in *)&ss)->sin_addr;
980 iretn = WSAGetLastError();
981 if (iretn != WSAEWOULDBLOCK)
987 if (iretn != EINPROGRESS)
bool connect
Definition: socketlib.h:178
Agent socket using Unicast UDP.
int32_t iretn
Definition: socketlib.h:221
#define SOCKET_ERROR_PROTOCOL
Definition: cosmos-errno.h:230
NetworkType type
Definition: socketlib.h:171
string address
Definition: socketlib.h:181
#define SOCKET_NONBLOCKING
Non-blocking Agent.
Definition: socketlib.h:80
#define SOCKET_TALK
Talk followed by optional listen (sendto address)
Definition: socketlib.h:82
int addrlen
Definition: socketlib.h:186
int32_t handle
Definition: socketlib.h:173
bool blocking
Definition: socketlib.h:189
bool stream
Definition: socketlib.h:192
uint16_t role
Definition: socketlib.h:188
uint16_t port
Definition: socketlib.h:183
uint32_t timeout
Definition: socketlib.h:190
static bool debug
Definition: agent_antenna.cpp:164
#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.
struct sockaddr_in server
Definition: socketlib.h:175
SocketOptions sok
Definition: socketlib.h:218