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

📄 xpt-tcp.c

📁 SyncML手册及其编程
💻 C
📖 第 1 页 / 共 2 页
字号:
    }    #endif  }  /**************************/  /* Obtain a TCP/IP socket */  /**************************/  *pSocket = (Socket_t) 0L;  if (rc == TCP_RC_OK) {    lSocket = socket(PF_INET, SOCK_STREAM, 0);    rc = CHKERROR ((int)lSocket);    *pSocket = (Socket_t) lSocket;  }  /***************************************************************/  /* Server socket handling: set the socket to the listener mode */  /***************************************************************/  if ((pszOpenMode [0] == 's') || (pszOpenMode [0] == 'S')) {    /*****************************************/    /* Bind the socket to the specified port */    /*****************************************/    if (rc == TCP_RC_OK) {      getINServerAddr ((const char *)pszPort, &socket_address);      iTcpRc = bind (lSocket, (struct sockaddr *)&socket_address, sizeof (socket_address));      rc = CHKERROR (iTcpRc);    }    /***************************************/    /* Set the socket to the listener mode */    /***************************************/    if (rc == TCP_RC_OK) {       iTcpRc = listen (lSocket, MAX_SOCKET_CONNECTIONS);       rc = CHKERROR (iTcpRc);    }  }  /***********************************************/  /* Client socket handling: connect to the host */  /***********************************************/  else if ((pszOpenMode [0] == 'c') || (pszOpenMode [0] == 'C')) {      // %%% luz 2003-04-16 : added SSL support    useSSL = pszOpenMode[1] && pszOpenMode[1]=='S';    if (rc == TCP_RC_OK && useSSL) {      // SSL preparation BEFORE opening the socket      rc = tcpEnableSSL((SocketPtr_t)&lSocket,false);      defaultPort=HTTPS_PORT;    }    if (rc == TCP_RC_OK) { // %%% luz:2003-04-16: added check for failure (was missing for client case)      struct sockaddr_in target_socket_address;      if ( pFirewallInfo && pFirewallInfo->type == TCP_FIREWALL_PROXY ) {         getINSockFirewallAddr( pFirewallInfo, &socket_address );         if (!getINSockAddr ((StringBuffer_t) pszPort, &target_socket_address, defaultPort)) {             rc = TCP_RC_ERROR;         }      } else {         if (!getINSockAddr ((StringBuffer_t) pszPort, &socket_address, defaultPort)) {             rc = TCP_RC_ERROR;         }      }      if ( rc == TCP_RC_OK ) {         iTcpRc = connect (lSocket, (struct sockaddr *)&socket_address, sizeof (socket_address));         rc = CHKERROR (iTcpRc);         // %%% luz 2003-06-26 : added SSL init after opening socket         if (rc == TCP_RC_OK && useSSL) {           // SSL preparation AFTER opening the socket           rc = tcpEnableSSL((SocketPtr_t)&lSocket,true);           if (rc != TCP_RC_OK) {             // SSL failed, close the socket             closesocket (lSocket);           }         }                  if ( rc == TCP_RC_OK && pFirewallInfo && pFirewallInfo->type == TCP_FIREWALL_SOCKS ) {             rc = makeSocksConnection( pSocket, &target_socket_address );         }      }    }  }  else    rc = TCP_RC_ERROR;  return rc;}// %%% luz 2003-06-26: Added default tcpEnableSSL() here/*****************************************************************************//*                                                                           *//*             Function: Enable SSL for the  TCP/IP connection               *//*                                                                           *//*****************************************************************************/// %%% luz 2003-06-26: If PLATFORM_TCPENABLESSL is defined, tcpReadData must be//                     implemented in xptitcp.c#ifndef PLATFORM_TCPENABLESSL// enable SSL for socketTcpRc_t tcpEnableSSL(SocketPtr_t pSocket, Bool_t aConnected) {  return TCP_RC_ENOPROTOOPT; // not supported by default}#endif // not defined PLATFORM_TCPENABLESSL/*****************************************************************************//*                                                                           *//*             Function: Close the TCP/IP connection                         *//*                                                                           *//*****************************************************************************/TcpRc_t tcpCloseConnection (SocketPtr_t pSocket) // i: socket to be closed{   TcpRc_t rc = TCP_RC_OK;   int iTcpRc;   SOCKET lSocket = pSocket ? (SOCKET) *pSocket : -1L;   if (lSocket == -1L)      return TCP_RC_ERROR;   // %%% luz 2003-06-26: give platform code to do extra cleanup   tcpBeforeSocketClose(pSocket);   /********************/   /* Close the socket */   /********************/   if (rc == TCP_RC_OK)      {      iTcpRc = closesocket (lSocket);      rc = CHKERROR (iTcpRc);      }   if (pSocket) *pSocket = -1L; // reset the socket   return rc;}/*****************************************************************************//*                                                                           *//*    Function: Wait until a client connected to this server socket.         *//*                                                                           *//*****************************************************************************/TcpRc_t tcpWaitforConnections (SocketPtr_t    pSocket,          // i: server socket                               SocketPtr_t    pNewSocket,       // o: Connection socket                               StringBuffer_t pchSenderAddress) // o: Client IP address   {   TcpRc_t rc = TCP_RC_OK;   struct sockaddr_in socket_address;   SOCKET lSocket = pSocket ? (SOCKET) *pSocket : -1L;   if ((lSocket == -1L) || (lSocket == 0L))      return TCP_RC_ERROR;   else      {      /**************************************/      /* Wait for a client to connect to me */      /**************************************/      int iSocketAddressLength = sizeof (socket_address);      *pNewSocket = (long) accept (lSocket, (struct sockaddr *)&socket_address, &iSocketAddressLength);      rc = CHKERROR (*pNewSocket);      if (rc == TCP_RC_OK)         {         getSenderAddress (&socket_address, pchSenderAddress);         }      else         {         // avoid follow-up problems and set output variables to a default value         *pNewSocket = (long) 0;         pchSenderAddress [0] = '\0';         }      }   return rc;   }/*****************************************************************************//*                                                                           *//*    Function: receive a block of data from the communication partner.      *//*                                                                           *//*****************************************************************************/// %%% luz 2003-06-26: If PLATFORM_TCPREADDATA is defined, tcpReadData must be//                     implemented in xptitcp.c#ifndef PLATFORM_TCPREADDATATcpRc_t tcpReadData (SocketPtr_t     pSocket,   // i: Socket                     DataBuffer_t    pbBuffer,  // i: Data buffer                     BufferSizePtr_t pcbBufferSize) // io: size of data buffer / # of bytes reveived   {   TcpRc_t rc = TCP_RC_OK;   int iReceived;   SOCKET lSocket = pSocket ? (SOCKET) *pSocket : -1L;   if (lSocket == -1L)      return TCP_RC_ERROR;   /************************/   /* Receive a data block */   /************************/   iReceived = recv (lSocket, (char *)pbBuffer, (int) *pcbBufferSize, 0);   /************************************************/   /* 0-byte packages indicate that the connection */   /* was closed by the communication partner      */   /************************************************/   if (iReceived == 0)      {      pbBuffer [0] = '\0';      *pcbBufferSize = 0L;      rc = TCP_RC_EOF; // end-of-transmission      }   else if (iReceived < 0)      {      /*********************/      /* An error occurred */      /*********************/      rc = CHKERROR (iReceived);      }   else      {      *pcbBufferSize = (BufferSize_t) iReceived; // return the # of bytes received.      rc = TCP_RC_OK;      }   return rc;}#endif // not defined PLATFORM_TCPREADDATA/*****************************************************************************//*                                                                           *//*        Function: Send a block of data to the communication partner.       *//*                                                                           *//*****************************************************************************/// %%% luz 2003-06-26: If PLATFORM_TCPSENDDATA is defined, tcpReadData must be//                     implemented in xptitcp.c#ifndef PLATFORM_TCPSENDDATATcpRc_t tcpSendData (SocketPtr_t        pSocket,      // i: socket                     const DataBuffer_t pbBuffer,     // i: data buffer                     BufferSize_t       cbBufferSize) // i: size of data buffer   {   TcpRc_t rc = TCP_RC_OK;   int iTcpRc;   SOCKET lSocket = pSocket ? (SOCKET) *pSocket : -1L;   if (lSocket == -1L)      return TCP_RC_ERROR; // invalid socket   iTcpRc = send (lSocket,  (char *)pbBuffer, (int) cbBufferSize, 0);   rc = CHKERROR (iTcpRc);   if ((iTcpRc > 0) && (iTcpRc != (int) cbBufferSize))       rc = TCP_RC_ERROR;   return rc;}#endif // not defined PLATFORM_TCPSENDDATA#ifndef PLATFORM_BEFORESOCKETCLOSE// Nop in standard casevoid tcpBeforeSocketClose(SocketPtr_t pSocket){  // nop}#endif // not defined PLATFORM_BEFORESOCKETCLOSE

⌨️ 快捷键说明

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