本文整理汇总了C++中SockaddrFromIpEndpointName函数的典型用法代码示例。如果您正苦于以下问题:C++ SockaddrFromIpEndpointName函数的具体用法?C++ SockaddrFromIpEndpointName怎么用?C++ SockaddrFromIpEndpointName使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SockaddrFromIpEndpointName函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Bind
void Bind( const IpEndpointName& localEndpoint )
{
struct sockaddr_in bindSockAddr;
SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );
// KEVIN: allow for multicast
SetAllowReuse(true);
struct ip_mreq group;
if (localEndpoint.IsMulticastAddress()) {
group.imr_interface.s_addr = htonl(INADDR_ANY);
group.imr_multiaddr.s_addr = bindSockAddr.sin_addr.s_addr;
if (setsockopt(socket_, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) {
throw std::runtime_error("error adding multicast group\n");
closesocket(socket_); // winsock2 equivalent of close()
exit(1);
}
}
bindSockAddr.sin_addr.s_addr = INADDR_ANY;
// KEVIN: the above line needs to be added for winsock2, otherwise bind
// will fail with error 10049 WSAEADDRNOTAVAIL
// solution suggested from: http://www.sockets.com/err_lst1.htm#WSAEADDRNOTAVAIL
if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
throw std::runtime_error("unable to bind udp socket\n");
}
isBound_ = true;
}
示例2: Connect
void Connect( const IpEndpointName& remoteEndpoint )
{
SockaddrFromIpEndpointName( connectedAddr_, remoteEndpoint );
if (connect(socket_, (struct sockaddr *)&connectedAddr_, sizeof(connectedAddr_)) < 0) {
throw std::runtime_error("unable to connect udp socket\n");
}
isConnected_ = true;
}
示例3: LocalEndpointFor
IpEndpointName LocalEndpointFor( const IpEndpointName& remoteEndpoint ) const
{
assert( isBound_ );
// first connect the socket to the remote server
struct sockaddr_in connectSockAddr;
SockaddrFromIpEndpointName( connectSockAddr, remoteEndpoint );
if (connect(socket_, (struct sockaddr *)&connectSockAddr, sizeof(connectSockAddr)) < 0) {
throw std::runtime_error("unable to connect udp socket\n");
}
// get the address
struct sockaddr_in sockAddr;
memset( (char *)&sockAddr, 0, sizeof(sockAddr ) );
socklen_t length = sizeof(sockAddr);
if (getsockname(socket_, (struct sockaddr *)&sockAddr, &length) < 0) {
throw std::runtime_error("unable to getsockname\n");
}
if ( isConnected_ ) {
// reconnect to the connected address
if (connect(socket_, (struct sockaddr *)&connectedAddr_, sizeof(connectedAddr_)) < 0) {
throw std::runtime_error("unable to connect udp socket\n");
}
} else{
// unconnect from the remote address
struct sockaddr_in unconnectSockAddr;
SockaddrFromIpEndpointName( unconnectSockAddr, IpEndpointName() );
if( connect(socket_, (struct sockaddr *)&unconnectSockAddr, sizeof(unconnectSockAddr)) < 0
&& WSAGetLastError() != WSAEADDRNOTAVAIL ){
throw std::runtime_error("unable to un-connect udp socket\n");
}
}
return IpEndpointNameFromSockaddr( sockAddr );
}
示例4: Bind
void Bind( const IpEndpointName& localEndpoint )
{
struct sockaddr_in bindSockAddr;
SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );
if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
throw std::runtime_error("unable to bind udp socket\n");
}
isBound_ = true;
}
示例5: Bind
void Bind( const IpEndpointName& localEndpoint )
{
struct sockaddr_in bindSockAddr;
SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );
if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
std::string sErr = std::string("Unable to bind UDP socket. ") +
strerror(errno);
throw std::runtime_error(sErr.c_str());
}
isBound_ = true;
}
示例6: BoundPort
// oscit [
int BoundPort() const {
struct sockaddr_in localAddr;
SockaddrFromIpEndpointName( localAddr, IpEndpointName() );
socklen_t addrLen = sizeof(localAddr);
// Ask getsockname to fill in this socket's local
// address.
if (getsockname(socket_, (struct sockaddr *)&localAddr, &addrLen) < 0) {
throw std::runtime_error("unable to getsockname\n");
}
return ntohs(localAddr.sin_port);
}
示例7: LocalEndpointFor
IpEndpointName LocalEndpointFor(const IpEndpointName &remoteEndpoint) const
{
assert(isBound_);
// first connect the socket to the remote server
struct sockaddr_in connectSockAddr;
SockaddrFromIpEndpointName(connectSockAddr, remoteEndpoint);
if (connect(socket_, (struct sockaddr *)&connectSockAddr, sizeof(connectSockAddr)) < 0)
{
throw std::runtime_error("unable to connect udp socket\n");
}
// get the address
struct sockaddr_in sockAddr;
memset((char *)&sockAddr, 0, sizeof(sockAddr));
socklen_t length = sizeof(sockAddr);
if (getsockname(socket_, (struct sockaddr *)&sockAddr, &length) < 0)
{
throw std::runtime_error("unable to getsockname\n");
}
if (isConnected_)
{
// reconnect to the connected address
if (connect(socket_, (struct sockaddr *)&connectedAddr_, sizeof(connectedAddr_)) < 0)
{
throw std::runtime_error("unable to connect udp socket\n");
}
}
else
{
// unconnect from the remote address
struct sockaddr_in unconnectSockAddr;
memset((char *)&unconnectSockAddr, 0, sizeof(unconnectSockAddr));
unconnectSockAddr.sin_family = AF_UNSPEC;
// address fields are zero
int connectResult = connect(socket_, (struct sockaddr *)&unconnectSockAddr, sizeof(unconnectSockAddr));
if (connectResult < 0 && errno != EAFNOSUPPORT)
{
throw std::runtime_error("unable to un-connect udp socket\n");
}
}
return IpEndpointNameFromSockaddr(sockAddr);
}
示例8: Connect
void Connect( const IpEndpointName& remoteEndpoint, bool multicast )
{
SockaddrFromIpEndpointName( connectedAddr_, remoteEndpoint );
if (connect(socket_, (struct sockaddr *)&connectedAddr_, sizeof(connectedAddr_)) < 0) {
throw std::runtime_error("unable to connect udp socket\n");
}
if( multicast ) {
char enable = 1;
setsockopt( socket_, SOL_SOCKET, SO_BROADCAST, &enable, sizeof(enable) );
}
isConnected_ = true;
}
示例9: Bind
void Bind( const IpEndpointName& localEndpoint )
{
struct sockaddr_in bindSockAddr;
SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );
{
IpEndpointName temp = IpEndpointNameFromSockaddr(bindSockAddr);
char address[30];
temp.AddressAndPortAsString(address);
}
if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
throw std::runtime_error("unable to bind udp socket\n");
}
isBound_ = true;
}
示例10: Bind
void Bind( const IpEndpointName& localEndpoint )
{
struct sockaddr_in bindSockAddr;
SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );
if (localEndpoint.IsMulticastAddress()) {
group.imr_interface.s_addr = htonl(INADDR_ANY);
group.imr_multiaddr.s_addr = bindSockAddr.sin_addr.s_addr;
if (setsockopt(socket_, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) {
throw std::runtime_error("error adding multicast group\n");
close(socket_);
}
}
if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
throw std::runtime_error("unable to bind udp socket\n");
}
isBound_ = true;
}