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

📄 tcp_client_demo.lst

📁 cs8900 c51应用
💻 LST
📖 第 1 页 / 共 2 页
字号:
 152                  if(tcpc_demo_connect){
 153                          if(tcp_connect(tcpc_demo_soch, TCPC_DEMO_RMT_IP, TCPC_DEMO_RMT_PORT,0)>0){
 154                                  /* Connection establishing procedure started */
 155                                  tcpc_demo_connect=0;
 156                                  }
 157                  }
 158                  
 159                  if(tcpc_demo_senddata){
 160                          if(tcpc_demo_send()!=-1)                
 161                                  tcpc_demo_senddata=0;
 162                  }
 163          }
 164          
 165           /*
 166           * Event listener invoked when TCP/IP stack receives TCP data for
 167           * a given socket. Parameters:
 168           * - cbhandle - handle of the socket this packet is intended for. Check it
 169           *      just to be sure, but in general case not needed
 170           * - event - event that is notified. For TCP there are quite a few possible
 171           *      events, check switch structure below for more information
C51 COMPILER V7.06   TCP_CLIENT_DEMO                                                       11/26/2004 11:32:46 PAGE 4   

 172           * - par1, par2 - parameters who's use depends on the event that is notified
 173           */
 174          INT32 tcpc_demo_eventlistener(INT8 cbhandle, UINT8 event, UINT32 par1, UINT32 par2) reentrant
 175          {
 176          /* This function is called by TCP stack to inform about events  */
 177                  UINT16 i;
 178                  
 179                  if( cbhandle != tcpc_demo_soch)         /* Not our handle       */
 180                          return(-1);
 181                  
 182                  switch( event ){
 183                          
 184                          /* Connection request event. Used by TCP/IP stack to inform
 185                           * the application someone is trying to establish a connection.
 186                           * Server can decide, based on provided IP address and port number,
 187                           * whether to allow or not connection establishment.
 188                           * Parameters:
 189                           *  - par1 - remote hosts IP address
 190                           *  - par2 - remote hosts port number
 191                           * 
 192                           * Return values from event listener:
 193                           * -1 - do not allow connection to be established (send reset)
 194                           * -2 - do not send any response for now to the SYN packet (let's
 195                           *              think a little before answering)
 196                           * 1  - allow connection to be established
 197                           */
 198                          case TCP_EVENT_CONREQ:
 199                                  /* should never get here in client TCP application! */
 200                                  DEBUGOUT("Connection request arrived!\r\n");
 201                                  
 202                                  /* Enable all connections       */
 203                                  return(-1);
 204                          
 205                                  break;
 206                                  
 207                          /* Connection abort event. Connection on a given socket is beeing 
 208                           * aborted for somereason (usually retransmissions are used up or 
 209                           * some abnormal situation in communication happened).
 210                           * Parameters:
 211                           *  - par1 - remote hosts IP address
 212                           *  - par2 - remote hosts port number
 213                           */
 214                          case TCP_EVENT_ABORT:
 215                                  DEBUGOUT("Connection aborting!\r\n");
 216                                  break;
 217                          
 218                          /* Connection established event - three-way handshaking performed
 219                           * OK and connection is established.
 220                           * Parameters:
 221                           *  - par1 - remote hosts IP address
 222                           *  - par2 - remote hosts port number
 223                           */
 224                          case TCP_EVENT_CONNECTED:
 225                                  DEBUGOUT("TCP connection established!\r\n");
 226                                  /* do something ... (initialize data sending for example */
 227                                  break;
 228                                  
 229                          /* Connection closing event. Happens when TCP connection is
 230                           * intentionally close by some side calling close function and
 231                           * initializing proper TCP connection close procedure.
 232                           * Parameters:
 233                           *  - par1 - remote hosts IP address
C51 COMPILER V7.06   TCP_CLIENT_DEMO                                                       11/26/2004 11:32:46 PAGE 5   

 234                           *  - par2 - remote hosts port number
 235                           */
 236                          case TCP_EVENT_CLOSE:
 237                                  DEBUGOUT("TCP Connection closing...!\r\n");
 238                                  break;
 239                                  
 240                          /* Data acknowledgment event. Happens when data that was
 241                           * previously sent gets acknowledged. This means we can now
 242                           * send some more data! :-)
 243                           * Parameters:
 244                           *  - par1 - remote hosts IP address
 245                           *  - par2 - remote hosts port number
 246                           */
 247                          case TCP_EVENT_ACK:
 248                                  DEBUGOUT("Data acknowledged!\r\n");
 249                                  /* if more data should be sent, adjust variables and
 250                                          set tcpc_demo_senddata variable */
 251                                          
 252                                  break;
 253                          
 254                          /* Data received event. Happens when we receive some data over the
 255                           * TCP connection.
 256                           * Parameters:
 257                           *  - par1 - number of data bytes received
 258                           *  - par2 = 0
 259                           */
 260                          case TCP_EVENT_DATA:
 261                                  DEBUGOUT("Data arrived!\r\n");
 262                                  /* read data that was received (and 
 263                                   * probably do something with it :-)
 264                                   */
 265                                  for(i=0;i<par1;i++)
 266                                          RECEIVE_NETWORK_B();
 267                                          
 268                                  /* If needed initialize data sending
 269                                   * by setting tcpc_demo_senddata variable
 270                                   */
 271                                  break;
 272                                  
 273                          /* Regenerate data event. Happens when data needs to be
 274                           * retransmitted because of possible loss on the network.
 275                           * Note that THE SAME DATA must be sent over and over again
 276                           * until TCP_EVENT_ACK is generated (for that data)! 
 277                           * Parameters:
 278                           *  - par1 - amount of data to regenerate (usually all)
 279                           *      - par2 = 0
 280                           */
 281                          case TCP_EVENT_REGENERATE:
 282                                  tcpc_demo_send();
 283                                  break;
 284                  
 285                  
 286                          default:
 287                                  return(-1);
 288                  }
 289          }
 290          
 291          INT16 tcpc_demo_send(void){
 292                  UINT16 i;
 293                  /* first check if data sending is possible (it may be that
 294                   * previously sent data is not yet acknowledged)
 295                   */
C51 COMPILER V7.06   TCP_CLIENT_DEMO                                                       11/26/2004 11:32:46 PAGE 6   

 296                  if(tcp_checksend(tcpc_demo_soch) < 0 ){
 297                          /* Not yet */
 298                          return -1;
 299                  }
 300                  
 301                  /* put message in buffer. Message needs to start from TCP_APP_OFFSET
 302                   * because TCP/IP stack will put headers in front of the message to
 303                   * avoid data copying
 304                   */
 305          /*      for(i=0;i<32;i++)
 306                          net_buf[TCP_APP_OFFSET+i]='A'+(i%25); */
 307                  for(i=0;i<BufferLen;i++)
 308                          net_buf[TCP_APP_OFFSET+i]=Buffer[i];
 309          
 310                  /* send data */
 311                  return tcp_send(tcpc_demo_soch, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, Buffer
             -Len);
 312          
 313          }

C51 COMPILATION COMPLETE.  5 WARNING(S),  1 ERROR(S)

⌨️ 快捷键说明

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