예제 #1
0
/** @return false and set error code and description string on failure */
bool ArSocket::connectTo(const char *host, int port)
{
  char localhost[maxHostNameLen()];
  myError = NoErr;
  myErrorStr.clear();
  if (myFD < 0)
    return(false);

  if (!host)
  {
    if (gethostname(localhost, sizeof(localhost)) == 1)
    {
      myErrorStr="Failure to locate host '";
      myErrorStr+=localhost;
      myErrorStr+="'";
      perror("gethostname");
      return(false);
    }
    host=localhost;
  }

  char useHost[1024];
  int usePort;
  separateHost(host, port, useHost, sizeof(useHost), &usePort);

  bzero(&mySin, sizeof(mySin));
  if (!hostAddr(useHost, mySin.sin_addr))
    return(false);
  setRawIPString();
  mySin.sin_family=AF_INET;
  mySin.sin_port=hostToNetOrder(usePort);

  return(connectTo(&mySin));
}
예제 #2
0
/*
 * 得到本地主机名
 * */
std::string RobotSocket::getHostName()   
{
	char localhost[maxHostNameLen()];

	if (gethostname(localhost, sizeof(localhost)) == 1)
		return("");
	else
		return(localhost);
}
예제 #3
0
/** @return false and set error code and description string on failure  */
bool ArSocket::connect(const char *host, int port, Type type,
		       const char *openOnIP)
{
  char localhost[maxHostNameLen()];
  myError=NoErr;
  myErrorStr.clear();
  if (!host)
  {
    if (gethostname(localhost, sizeof(localhost)) == 1)
    {
      myError=ConBadHost;
      myErrorStr="Failure to locate host '";
      myErrorStr+=localhost;
      myErrorStr+="'";
      perror("gethostname");
      return(false);
    }
    host=localhost;
  }

  bzero(&mySin, sizeof(mySin));
  // MPL taking out this next code line from the if since it makes
  // everything we can't resolve try to connect to localhost
  // &&  !hostAddr("localhost", mySin.sin_addr))

  char useHost[1024];
  int usePort;
  separateHost(host, port, useHost, sizeof(useHost), &usePort);

  if (!hostAddr(useHost, mySin.sin_addr))
    return(false);
  setRawIPString();
  mySin.sin_family=AF_INET;
  mySin.sin_port=hostToNetOrder(usePort);

  if ((type == TCP) && ((myFD=socket(AF_INET, SOCK_STREAM, 0)) < 0))
  {
    myError=NetFail;
    myErrorStr="Failure to make TCP socket";
    perror("socket");
    return(false);
  }
  else if ((type == UDP) && ((myFD=socket(AF_INET, SOCK_DGRAM, 0)) < 0))
  {
    myError=NetFail;
    myErrorStr="Failure to make UDP socket";
    perror("socket");
    return(false);
  }

  ArUtil::setFileCloseOnExec(myFD);

  if (openOnIP != NULL)
  {
    struct sockaddr_in outSin;
    if (!hostAddr(openOnIP, outSin.sin_addr))
    {
      myError = NameLookup;
      myErrorStr = "Name lookup failed";
      ArLog::log(ArLog::Normal, "Couldn't find ip of %s to open on", openOnIP);
      return(false); 
    }
    outSin.sin_family=AF_INET;
    outSin.sin_port=hostToNetOrder(0);
    if (bind(myFD, (struct sockaddr *)&outSin, sizeof(outSin)) < 0)
    {
      ArLog::log(ArLog::Normal, "Failure to bind socket to port %d", 0);
      perror("socket");
      return(false);
    }
  }

  myType=type;

  if (::connect(myFD, (struct sockaddr *)&mySin,
		sizeof(struct sockaddr_in)) < 0)
  {
    myErrorStr="Failure to connect socket";
    switch (errno)
    {
    case ECONNREFUSED:
      myError=ConRefused;
      myErrorStr+="; Connection refused";
      break;
    case ENETUNREACH:
      myError=ConNoRoute;
      myErrorStr+="; No route to host";
      break;
    default:
      myError=NetFail;
      break;
    }
    //perror("connect");
    ::close(myFD);
    myFD = -1;
    return(false);
  }

  return(true);
}
예제 #4
0
/** @return false and set error code and description string on failure  */
bool ArSocket::open(int port, Type type, const char *openOnIP)
{
  int ret;
  char localhost[maxHostNameLen()];

  myError=NoErr;
  myErrorStr.clear();
  if ((type == TCP) && ((myFD=socket(AF_INET, SOCK_STREAM, 0)) < 0))
  {
    myErrorStr="Failure to make TCP socket";
    perror("socket");
    return(false);
  }
  else if ((type == UDP) && ((myFD=socket(AF_INET, SOCK_DGRAM, 0)) < 0))
  {
    myErrorStr="Failure to make UDP socket";
    perror("socket");
    return(false);
  }

  ArUtil::setFileCloseOnExec(myFD);

  setLinger(0);
  setReuseAddress();

  myType=type;

  /* MPL removed this since with what I Took out down below months ago 
  if (gethostname(localhost, sizeof(localhost)) == 1)
  {
    myErrorStr="Failure to locate localhost";
    perror("gethostname");
    return(false);
  }
  */
  bzero(&mySin, sizeof(mySin));
  /* MPL took this out since it was just overriding it with the
     INADDR_ANY anyways and it could cause slowdowns if a machine wasn't
     configured so lookups are quick
  if (!hostAddr(localhost, mySin.sin_addr) && 
      !hostAddr("localhost", mySin.sin_addr))
    return(false); */

  if (openOnIP != NULL)
  {
    
    if (!hostAddr(openOnIP, mySin.sin_addr))
    {
      ArLog::log(ArLog::Normal, "Couldn't find ip of %s to open on", openOnIP);
      myError = NameLookup;
      myErrorStr = "Name lookup failed";
      return(false); 
    }
    else
    {
      //printf("Opening on %s\n", openOnIP);
    }
  }
  else
  {
    mySin.sin_addr.s_addr=htonl(INADDR_ANY);
  }

  setRawIPString();
  mySin.sin_family=AF_INET;
  mySin.sin_port=hostToNetOrder(port);

  if ((ret=bind(myFD, (struct sockaddr *)&mySin, sizeof(mySin))) < 0)
  {
    myError = NetFail;
    myErrorStr="Failure to bind socket to port ";
    sprintf(localhost, "%d", port);
    myErrorStr+=localhost;
    perror("socket");
    return(false);
  }

  if ((type == TCP) && (listen(myFD, 5) < 0))
  {
    myError = NetFail;
    myErrorStr="Failure to listen on socket";
    perror("listen");
    return(false);
  }

  return(true);
}
예제 #5
0
/*
 * 作为服务器套接字
 * 完成套接字的穿件,绑定
 * 
 * */
bool RobotSocket::open(int port, Type type, const char *openOnIP)
{
	int ret;
	char localhost[maxHostNameLen()];

	if ((type == TCP) && ((myFD=socket(AF_INET, SOCK_STREAM, 0)) < 0))
	{
		myErrorStr="[socket]创建TCP套接字失败";
		perror(myErrorStr.c_str());
		return(false);
	}
	else if ((type == UDP) && ((myFD=socket(AF_INET, SOCK_DGRAM, 0)) < 0))
	{
		myErrorStr="[socket]创建UDP套接字失败";
		perror(myErrorStr.c_str());
		return(false);
	}

	myType=type;
	bzero(&mySin, sizeof(mySin));

	if (openOnIP != NULL)
	{

		if (!hostAddr(openOnIP, mySin.sin_addr))
		{
			/*不能查找到此IP主机*/
			return(false); 
		}
		else
		{
    			/*成功*/
		}
	}
	else
	{
		mySin.sin_addr.s_addr = htonl(INADDR_ANY);  //任意分配本机有的IP,任务交给内核
	}

	setIPString();
	mySin.sin_family = AF_INET;
	mySin.sin_port = hostToNetOrder(port);

	if ((ret=bind(myFD, (struct sockaddr *)&mySin, sizeof(mySin))) < 0)
	{
		myErrorStr="[bind]绑定失败";
		sprintf(localhost, "%d", port);
		myErrorStr+=localhost;
		perror(myErrorStr.c_str());
		return(false);
	}

	if ((type == TCP) && (listen(myFD, 5) < 0))
	{
		myErrorStr="[listen]绑定失败";
		perror(myErrorStr.c_str());
		return(false);
	}

	return(true);
}