createSocket

此方法用于创建新的套接字。

特征符
int RiCOSSocket_createSocket (RiCOSSocket * const me,
   const char *SocketAddress, unsigned int nSocketPort);
参数
me

这是 RiCOSSocket 对象。

SocketAddress

这是套接字地址。您可以将此地址设置为字符串主机名。缺省值为 NULL。

nSocketPort

这是套接字端口号。缺省值为 0。

返回

套接字创建状态。可能的值如下所示:

示例
int RiCOSSocket_createSocket(RiCOSSocket * const me,
   const char * SocketAddress, unsigned int nSocketPort)
{
   static struct sockaddr_in   addr;
   int proto;
   char hostName[128];
   int rvStat;

   if (me == NULL) {return 0;}

   if (nSocketPort == 0) {
      nSocketPort = 6423;
   }

   addr.sin_family = AF_INET;
   proto = IPPROTO_TCP;
   (void)gethostname(hostName, sizeof(hostName)-1);

   if (NULL != SocketAddress && strlen(SocketAddress) 
      != 0) {
      if (!strcmp(hostName, SocketAddress)) {
         SocketAddress = NULL;}
      else {
         (void)strcpy(hostName, SocketAddress);
         addr.sin_addr.s_addr = inet_addr(hostName);
         if (((unsigned long)ERROR) ==
            addr.sin_addr.s_addr) {
            addr.sin_addr.s_addr =
               hostGetByName(hostName);
         }
         if (((unsigned long)ERROR) ==
            addr.sin_addr.s_addr) {
            fprintf(stderr, "Could not get the address 
               of host '%s'\n", hostName);
            return 0;
         }
      }
   }   
   if (NULL == SocketAddress || strlen(SocketAddress) 
      == 0) {
      addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
   }

   #ifdef unix
      endprotoent();
   #endif /* unix */   

   addr.sin_port = htons((u_short)nSocketPort);   
   if ((me->theSock = socket(AF_INET, SOCK_STREAM,
      proto)) == -1) {
      fprintf(stderr,"Could not create socket\n");
      me->theSock = 0;
      return 0;
   }
   while ((rvStat = connect(me->theSock, 
      (struct sockaddr *)&addr, sizeof(addr))) ==
      SOCKET_ERROR && (errno == EINTR));
      if (SOCKET_ERROR == rvStat) {
         fprintf(stderr, "Could not connect to server 
            at %s port %d\n Error No. : %d\n", hostName,
            (int)nSocketPort, errno);
      return 0;
   }   
   return 1;}

反馈