Пример #1
0
NETWORK_API void NetworkUtility::GetIPAndPortFromString( const rString& input, rString& outIP, unsigned short& outPort )
{
	if ( input == "" )
		return;

	outIP	= "";
	outPort = INVALID_PORT;

	size_t colonCount = std::count( input.begin(), input.end(), ':' );

	if ( colonCount == 1 )
	{
		outIP = GetIPFromString( input );
		size_t colonPosition = input.find( ':' );
		rString portString = input.substr( colonPosition + 1 );
		if ( StringUtility::IsDigitsOnly( portString ) )
		{
			outPort = GetPortFromString( portString );
		}
	}
	else if ( StringUtility::IsDigitsOnly( input ) )
	{
		outPort = GetPortFromString( input );
	}
	else
	{
		outIP = GetIPFromString( input );
	}
}
Пример #2
0
/* returns fd or <0 on error */
int ConnectToServer(struct net *n, char *server, int port)
{
  int sock, err;
  struct sockaddr_in s;

  sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
		perror("couldn't get socket");
		return -1;
  }

  s.sin_family = PF_INET;
  s.sin_port = htons(port);
  s.sin_addr.s_addr = GetIPFromString(server);

  err = connect(sock, (struct sockaddr *) &s, sizeof(s));
  if (err) {
		perror("couldn't connect to server");
		return -1;
  }

  fcntl(sock, F_SETFL, O_NDELAY);

  n->sock = sock;
  n->readfp = fdopen(sock, "r");
  setlinebuf(n->readfp);   /* line buffering makes for better parsing */

  return sock;
}