当前位置: 首页>>代码示例>>C++>>正文


C++ SocketName类代码示例

本文整理汇总了C++中SocketName的典型用法代码示例。如果您正苦于以下问题:C++ SocketName类的具体用法?C++ SocketName怎么用?C++ SocketName使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了SocketName类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: memset

SocketName Win32Socket::get_remote_name() const
{
	sockaddr_in addr;
	memset(&addr, 0, sizeof(sockaddr_in));
	int size = sizeof(sockaddr_in);
	int result = getpeername(handle, (sockaddr *) &addr, &size);
	throw_if_failed(result);

	SocketName name;
	name.from_sockaddr(AF_INET, (sockaddr *) &addr, sizeof(sockaddr_in));
	return name;
}
开发者ID:Cassie90,项目名称:ClanLib,代码行数:12,代码来源:win32_socket.cpp

示例2: memset

	SocketName TCPConnectionImpl::remote_name()
	{
		sockaddr_in addr;
		memset(&addr, 0, sizeof(sockaddr_in));
		int size = sizeof(sockaddr_in);
		int result = getpeername(handle, (sockaddr *)&addr, &size);
		if (result == SOCKET_ERROR)
			throw Exception("Error retrieving remote socket name");

		SocketName name;
		name.from_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));
		return name;
	}
开发者ID:rombust,项目名称:UICore,代码行数:13,代码来源:tcp_connection.cpp

示例3: send

	void UDPSocketImpl::send(const void *data, int size, const SocketName &endpoint)
	{
		sockaddr_in addr;
		endpoint.to_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));

		sendto(handle, static_cast<const char*>(data), size, 0, (const sockaddr *)&addr, sizeof(sockaddr_in));
	}
开发者ID:rombust,项目名称:UICore,代码行数:7,代码来源:udp_socket.cpp

示例4: Exception

	TCPConnectionImpl::TCPConnectionImpl(const SocketName &endpoint)
	{
		int receive_buffer_size = 600 * 1024;
		int send_buffer_size = 600 * 1024;
		//int result = setsockopt(handle, SOL_SOCKET, SO_RCVBUF, (const char *) &receive_buffer_size, sizeof(int));
		int result = setsockopt(handle, SOL_SOCKET, SO_SNDBUF, (const char *)&send_buffer_size, sizeof(int));

		sockaddr_in addr;
		endpoint.to_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));
		result = ::connect(handle, (const sockaddr *)&addr, sizeof(sockaddr_in));
		if (result == SOCKET_ERROR)
			throw Exception("Connect to server failed");

		int value = 1;
		result = setsockopt(handle, IPPROTO_TCP, TCP_NODELAY, (const char *)&value, sizeof(int));

		result = WSAEventSelect(handle, wait_handle, FD_READ | FD_WRITE | FD_CLOSE);
		if (result == SOCKET_ERROR)
			throw Exception("WSAEventSelect failed");

		//int receive_buffer_size = 0;
		//int send_buffer_size = 0;
		//int len = sizeof(int);
		//getsockopt(handle, SOL_SOCKET, SO_RCVBUF, (char *) &receive_buffer_size, &len);
		//getsockopt(handle, SOL_SOCKET, SO_SNDBUF, (char *) &send_buffer_size, &len);
	}
开发者ID:rombust,项目名称:UICore,代码行数:26,代码来源:tcp_connection.cpp

示例5: sizeof

	int UDPSocketImpl::read(void *data, int size, SocketName &endpoint)
	{
		sockaddr_in addr;
		int addr_len = sizeof(sockaddr_in);

		int result = recvfrom(handle, static_cast<char*>(data), size, 0, (sockaddr *)&addr, &addr_len);
		if (result == SOCKET_ERROR)
		{
			int last_error = WSAGetLastError();
			// To do: maybe return different return values for sending errors
			if (last_error == WSAEWOULDBLOCK || last_error == WSAEMSGSIZE || last_error == WSAECONNRESET || last_error == WSAENETRESET)
			{
				endpoint = SocketName();
				return -1;
			}
			else
			{
				throw Exception("Error reading from udp socket");
			}
		}

		endpoint = SocketName();
		endpoint.from_sockaddr(AF_INET, (sockaddr *)&addr, addr_len);
		return result;
	}
开发者ID:rombust,项目名称:UICore,代码行数:25,代码来源:udp_socket.cpp

示例6: bind

	void UDPSocketImpl::bind(const SocketName &endpoint)
	{
		sockaddr_in addr;
		endpoint.to_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));
		int result = ::bind(handle, (const sockaddr *)&addr, sizeof(sockaddr_in));
		if (result == SOCKET_ERROR)
			throw Exception("Could not bind socket to end point");
	}
开发者ID:rombust,项目名称:UICore,代码行数:8,代码来源:udp_socket.cpp

示例7: sizeof

int Win32Socket::peek_from(void *data, int size, SocketName &out_socketname)
{
	sockaddr_in new_addr;
	memset(&new_addr, 0, sizeof(sockaddr_in));
	int addr_size = sizeof(sockaddr_in);
	int result = ::recvfrom(handle, (char *) data, size, MSG_PEEK, (sockaddr *) &new_addr, &addr_size);
	throw_if_failed(result);
	out_socketname.from_sockaddr(AF_INET, (sockaddr *) &new_addr, size);
	reset_receive();
	return result;
}
开发者ID:Cassie90,项目名称:ClanLib,代码行数:11,代码来源:win32_socket.cpp

示例8: Exception

void Win32Socket::connect(const SocketName &socketname)
{
	sockaddr_in addr;
	socketname.to_sockaddr(AF_INET, (sockaddr *) &addr, sizeof(sockaddr_in));
	int result = ::connect(handle, (const sockaddr *) &addr, sizeof(sockaddr_in));
	if (result == SOCKET_ERROR)
	{
		int errorcode = WSAGetLastError();
		if ( ! ( (errorcode ==  WSAEWOULDBLOCK) || (errorcode ==  WSAEINPROGRESS) ) )
			throw Exception(error_to_string(errorcode));
	}
}
开发者ID:Cassie90,项目名称:ClanLib,代码行数:12,代码来源:win32_socket.cpp

示例9: setsockopt

void Win32Socket::bind(const SocketName &socketname, bool reuse_address)
{
	if (reuse_address)
	{
		int value = 1;
		int result = setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, (const char *) &value, sizeof(int));
		throw_if_failed(result);
	}

	sockaddr_in addr;
	socketname.to_sockaddr(AF_INET, (sockaddr *) &addr, sizeof(sockaddr_in));
	int result = ::bind(handle, (const sockaddr *) &addr, sizeof(sockaddr_in));
	throw_if_failed(result);
}
开发者ID:Cassie90,项目名称:ClanLib,代码行数:14,代码来源:win32_socket.cpp

示例10: accept

	TCPConnection TCPListen::accept(SocketName &out_end_point)
	{
		sockaddr_in peer_address = { 0 };
		socklen_t peer_address_length = sizeof(sockaddr_in);

		int result = ::accept(impl->handle, reinterpret_cast<sockaddr*>(&peer_address), &peer_address_length);
		if (result == -1)
		{
			if (errno == EWOULDBLOCK)
				return TCPConnection();
			else
				throw Exception("Error accepting from socket");
		}

		out_end_point.from_sockaddr(AF_INET, reinterpret_cast<sockaddr*>(&peer_address), peer_address_length);
		return TCPConnection(std::make_shared<TCPSocket>(result));
	}
开发者ID:ARMCoderCHS,项目名称:ClanLib,代码行数:17,代码来源:tcp_listen.cpp

示例11: sizeof

	TCPConnection TCPListen::accept(SocketName &out_end_point)
	{
		sockaddr_in peer_address = { 0 };
		int peer_address_length = sizeof(sockaddr_in);

		SOCKET result = ::accept(impl->handle, reinterpret_cast<sockaddr*>(&peer_address), &peer_address_length);
		if (result == INVALID_SOCKET)
		{
			if (WSAGetLastError() == WSAEWOULDBLOCK)
				return TCPConnection();
			else
				throw Exception("Error accepting from socket");
		}

		out_end_point = SocketName();
		out_end_point.from_sockaddr(AF_INET, reinterpret_cast<sockaddr*>(&peer_address), peer_address_length);
		return TCPConnection(std::make_shared<TCPSocket>(result));
	}
开发者ID:ARMCoderCHS,项目名称:ClanLib,代码行数:18,代码来源:tcp_listen.cpp

示例12: Exception

	TCPListen::TCPListen(const SocketName &endpoint, int backlog, bool reuse_address)
		: impl(new TCPSocket())
	{
		if (reuse_address)
		{
			int value = 1;
			int result = setsockopt(impl->handle, SOL_SOCKET, SO_REUSEADDR, (const char *)&value, sizeof(int));
			if (result == SOCKET_ERROR)
				throw Exception("Could not set reuse address socket option");
		}

		int receive_buffer_size = 600 * 1024;
		int send_buffer_size = 600 * 1024;

		//int result = setsockopt(impl->handle, SOL_SOCKET, SO_RCVBUF, (const char *) &receive_buffer_size, sizeof(int));
		//if (result == SOCKET_ERROR)
		//	throw Exception("Could not set socket receive buffer size");

		int result = setsockopt(impl->handle, SOL_SOCKET, SO_SNDBUF, (const char *)&send_buffer_size, sizeof(int));
		if (result == SOCKET_ERROR)
			throw Exception("Could not set socket send buffer size");

		int value = 1;
		result = setsockopt(impl->handle, IPPROTO_TCP, TCP_NODELAY, (const char *)&value, sizeof(int));

		sockaddr_in addr;
		endpoint.to_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));
		result = ::bind(impl->handle, (const sockaddr *)&addr, sizeof(sockaddr_in));
		if (result == SOCKET_ERROR)
			throw Exception("Could not bind socket to end point");

		result = ::listen(impl->handle, backlog);
		if (result == SOCKET_ERROR)
			throw Exception("Unable to set socket into listen state");

		result = WSAEventSelect(impl->handle, impl->wait_handle, FD_ACCEPT);
		if (result == SOCKET_ERROR)
			throw Exception("WSAEventSelect failed");
	}
开发者ID:ARMCoderCHS,项目名称:ClanLib,代码行数:39,代码来源:tcp_listen.cpp


注:本文中的SocketName类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。