⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpwce.c

📁 extremeDB s sample code,useful for you
💻 C
📖 第 1 页 / 共 2 页
字号:
}/***************************************************************************/MCO_RET nw_connect( nw_channel_h chan,   const char * connect_string,                     uint4 timeout)/* * IN nw_channel_h   chan       - pointer to a channel descriptor * IN const char connect_string - interconnect dependent connection-string * IN  unsigned long timeout); *  * Description: * Connects IO channel to the remote host by it's name. * * Returns MSO_S_OK if successful or error code (see above). */{  const char *cc = connect_string;  char name[128];  SOCKADDR_IN   csin;  uint4         mode = 1;  int           len;  conn_msg_t    msg;  MCO_RET       rc;/*  struct addrinfo *pres;*/  struct hostent *he;    chan->status = 0;/* Translate interconnect-dependent connect-string * Format: "xxx.xxx.xxx.xxx:nnnn" or "inet-name:port" *//* * Find ':' in connect string */    init_channel( chan );    for (;;)    {      if ( *cc == '\0' ) return (MCO_RET)(MCO_E_NW_INVADDR);      if ( *cc == ':' ) break;      cc++;    }    cc++;    len = cc-connect_string;    if ((int)(cc-connect_string) >= (int)sizeof(name) ) return (MCO_RET)(MCO_E_NW_NOMEM);    strncpy(name, connect_string, (int)(cc-connect_string-1));    name[(int)(cc-connect_string-1)] = 0;/* * Translate IP address */    if ( (he = gethostbyname(name)) == NULL ) return (MCO_RET)(MCO_E_NW_INVADDR);    csin.sin_addr.S_un.S_addr = *(PULONG)(he->h_addr_list[0]);    csin.sin_family = AF_INET;     chan->port = (uint2)atoi(cc);    csin.sin_port = htons((short) chan->port);    chan->port++;    if( (rc=nw_create_socket(chan)) != MCO_S_OK) {      return rc;    }        chan->event = CreateEvent( NULL, 0, 0, NULL);    chan->event1 = CreateEvent( NULL, 0, 0, NULL);    if ( (connect((SOCKET)chan->socket, (LPSOCKADDR)&csin, sizeof(csin))) < 0 ) {ErrRet: nw_close(chan);        return (MCO_RET)(MCO_E_NW_CONNECT);    }    chan->status = NWST_INITIALIZED | NWST_CONNECTED;    if(nw_recv(chan, (PCHAR)&msg, sizeof(conn_msg_t),      (PSHORT)&len, timeout)!=MCO_S_OK) {        goto ErrRet;    }    chan->index = msg.index;    return MCO_S_OK;}/***************************************************************************/MCO_RET nw_send ( nw_channel_h chan, char* buffer,                  int2 buflen,   uint4    timeout)/* * IN nw_channel_h  chan    - pointer to a channel descriptor. * IN  const char*  buffer  - buffer to send. * IN int2 buflen           - buffer length,is cut to 32767 bytes. * IN  unsigned long timeout - send timeout in milliseconds. It is media dependent *                            & application dependent. Application MUST set this  *                            parameter considering the media rate & it's own needs. *  * Description: * Sends a message to the communication channel  * * Returns MSO_S_OK if successful or error code (see above). */{  int             rcs;  fd_set          writefds;  TIMEVAL         tv;  timer_unit      time  = (long)timeout;  timer_unit      t1, t2 = mco_system_get_current_time();    if( (chan->status&(NWST_INITIALIZED|NWST_CONNECTED)) !=  (NWST_INITIALIZED|NWST_CONNECTED) ) {      return (MCO_RET)(MCO_E_NW_CONNECT);    }    while( buflen )    {      t1 = t2;      if( (time-=((t2=mco_system_get_current_time())-t1)) <= 0) {        return (MCO_RET)(MCO_E_NW_TIMEOUT);      }      FD_ZERO(&writefds);      FD_SET((SOCKET)chan->socket, &writefds);      tv.tv_sec = time/1000;      tv.tv_usec= (time%1000)*1000;      rcs = select (1+(int)chan->socket , 0, &writefds, 0, &tv);      if ( rcs < 0 ) // error      {        nw_close(chan);        return (MCO_RET)(MCO_E_NW_SENDERR);      }      if ( rcs == 0 ) {// timeout        nw_close(chan);        return (MCO_RET)(MCO_E_NW_TIMEOUT);      }      if ( !(FD_ISSET((SOCKET)chan->socket, &writefds) ) )      {        nw_close(chan);        return (MCO_RET)(MCO_E_NW_SENDERR);      }      if ( (rcs=send(chan->socket, buffer, buflen, 0)) <= 0 )      {        nw_close(chan);        return (MCO_RET)(MCO_E_NW_RECVERR);      }      buflen = (short)(buflen - (int2) rcs);      NumBytes += rcs;      buffer += rcs;    }    return MCO_S_OK;}/***************************************************************************/MCO_RET nw_recv ( nw_channel_h chan,  char* buffer,   int2 buflen,                  int2* recvlen,  uint4 timeout)/* IN nw_channel_h chan     - pointer to a channel descriptor. * OUT char* buffer         - buffer to receive. * IN int2 buflen           - buffer length limit,  is cut to 32767 bytes. * OUT int2* recvlen,       - actual received length, is cut to 32767 bytes. * IN unsigned long timeout - receive timeout in milliseconds. It is media dependent *                            & application dependet. Application MUST set this parameter *                            considering the media rate & it's own needs * * Description: * Receives a message from the communication cchannel *  * Returns MSO_S_OK if successful or error code (see above). */{  int             rcs;  int2            length = buflen;  fd_set          readfds;  TIMEVAL         tv;  timer_unit      time  = (long)timeout;  timer_unit      t1, t2 = mco_system_get_current_time();  if( (chan->status&(NWST_INITIALIZED|NWST_CONNECTED)) !=  (NWST_INITIALIZED|NWST_CONNECTED) )  {    return (MCO_RET)(MCO_E_NW_CONNECT);  }  while (length) {    t1 = t2;    if( (time-=((t2=mco_system_get_current_time())-t1)) <= 0) {      return (MCO_RET)(MCO_E_NW_TIMEOUT);    }    tv.tv_sec = 0;  //time/1000;    tv.tv_usec= 100*1000; //(time%1000)*1000;    FD_ZERO(&readfds);    FD_SET((SOCKET)chan->socket, &readfds);    rcs = select (1+(int)chan->socket, &readfds, 0, 0, &tv);    if( rcs < 0 )  { // error      nw_close(chan);      return (MCO_RET)(MCO_E_NW_RECVERR);    }    if( !rcs ) {  // timeout      continue;    }    if ( !(FD_ISSET((SOCKET)chan->socket, &readfds) ) )    {      continue;    }    if ( (rcs = recv(chan->socket, buffer, length, 0)) <= 0 )    {      nw_close(chan);      return (MCO_RET)(MCO_E_NW_CLOSED);    }    length = (short)(length - (short)rcs);    NumBytes += rcs;    buffer += rcs;  }  *recvlen = (int2)buflen;  return MCO_S_OK;}/* * Not implemented. The stubs for the compliance with other platforms *//*************************************************************************** * * Accept the cancel point connection * * IN  nw_channel_h chan      - pointer to the listener channel. * IN OUT nw_channel_h  ioch  - pointer to the IO channel waiting for connection. * IN  unsigned long timeout) - wait-for-connect timeout. *  * Description: * Waits for the connection of a remote host to the IO channel. * * Returns MSO_S_OK if successful or error code (see above). */MCO_RET nw_accept_cancel_point(                   nw_channel_h chan,                   char * port,                   timer_unit timeout                   ){    return MCO_S_OK;}/*************************************************************************** * * Connect to cancel point * * IN nw_channel_h   chan       - pointer to a channel descriptor * IN const char connect_string - interconnect dependent connection-string * IN  unsigned long timeout); *  * Description: * Connects IO channel to the remote host by it's name. * * Returns MSO_S_OK if successful or error code (see above). */MCO_RET nw_connect_cancel_point( nw_channel_h chan,                                 const char * connect_string,                                   timer_unit timeout                               ){    return MCO_S_OK;}/************************************************************************ * * Cancels the communication channel * *  Parameters: * * IN nw_channel_h ch      - pointer to a channel descriptor. * ************************************************************************/void  nw_cancel(nw_channel_h ch){  ch->status |= NWST_CANCEL;  SetEvent(ch->event1 );}#endif// CFG_TCP_SOCKET_CHANNEL

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -