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

📄 tcp.lst

📁 cs8900 c51应用
💻 LST
📖 第 1 页 / 共 5 页
字号:
 205                  
 206                  TCP_DEBUGOUT("No socket found\r\n");
 207                  return(-1);
 208          
 209          }
 210          
 211          /** \brief Release a TCP socket
 212           *  \ingroup tcp_app_api
 213           *      \author 
 214           *              \li Jari Lahti (jari.lahti@violasystems.com)
 215           *      \date 21.07.2002
 216           *      \param sochandle handle to socket to be released
 217           *      \return 
 218           *              \li -1 - Error releasing the socket (Wrong socket handle or socket
 219           *              not in proper state to be released)
 220           *              \li >=0 - handle of the released socket (can not be used any more
 221           *              untill allocated again with tcp_getsocket()).
 222           *
 223           *      Once the application does not need the TCP socket any more it can invoke
 224           *      this function in order to release it. This is usefull if there is a very
 225           *      limited number of sockets (in order to save some memory) shared among
 226           *      several applications.
 227           */
 228          INT8 tcp_releasesocket (INT8 sochandle)
C51 COMPILER V7.06   TCP                                                                   11/26/2004 11:32:45 PAGE 5   

 229          {
 230                  struct tcb* soc;
 231                  
 232                  if( NO_OF_TCPSOCKETS < 0 )
 233                          return(-1);
 234                  
 235                  if( NO_OF_TCPSOCKETS == 0 )
 236                          return(-1);
 237                  
 238                  if( sochandle > NO_OF_TCPSOCKETS ) {
 239                          TCP_DEBUGOUT("Socket handle non-valid\r\n");
 240                          return(-1);
 241                  }
 242                  
 243                  if( sochandle < 0 ) {
 244                          TCP_DEBUGOUT("Socket handle non-valid\r\n");
 245                          return(-1);
 246                  }
 247                  
 248                  soc = &tcp_socket[sochandle];           /* Get referense        */
 249                  
 250                  if( (soc->state != TCP_STATE_FREE) &&
 251                          (soc->state != TCP_STATE_RESERVED) &&
 252                          (soc->state != TCP_STATE_CLOSED)                ) {
 253                          TCP_DEBUGOUT("Socket is not on valid state to be released\r\n");
 254                          return(-1);
 255                  }
 256                  
 257                  /* We are there so all OK       */
 258                  
 259                  soc->state = TCP_STATE_FREE;
 260                  soc->type = TCP_TYPE_NONE;
 261                  soc->tos = 0;
 262                  soc->event_listener = 0;
 263                  soc->rem_ip = 0;
 264                  soc->remport = 0;
 265                  soc->locport = 0;
 266                  soc->flags = 0;
 267                  
 268                  return(sochandle);
 269          
 270          }
 271          
 272          /** \brief Put TCP socket to listen on a given port
 273           *  \ingroup tcp_app_api
 274           *      \author 
 275           *              \li Jari Lahti (jari.lahti@violasystems.com)
 276           *      \date 21.07.2002
 277           *      \param sochandle handle to socket to be placed to listen state
 278           *      \param port TCP port number on which it should listen
 279           *      \return
 280           *              \li -1 - Error
 281           *              \li >=0 - OK (Socket put to listening state. Handle to 
 282           *              socket returned)
 283           *
 284           *      This function will attempt to put socket to listening state. This
 285           *      is only possible if socket was defined as either #TCP_TYPE_SERVER or
 286           *      #TCP_TYPE_CLIENT_SERVER. If basic correctness checks pass, socket is 
 287           *      put to listening mode and corresponding tcb entry is initialized.
 288           *
 289           */
 290          INT8 tcp_listen (INT8 sochandle, UINT16 port)
C51 COMPILER V7.06   TCP                                                                   11/26/2004 11:32:45 PAGE 6   

 291          {
 292                  struct tcb* soc;
 293          
 294                  if( NO_OF_TCPSOCKETS < 0 )
 295                          return(-1);
 296                  
 297                  if( NO_OF_TCPSOCKETS == 0 )
 298                          return(-1);
 299                  
 300                  if( sochandle > NO_OF_TCPSOCKETS ) {
 301                          TCP_DEBUGOUT("Socket handle non-valid\r\n");
 302                          return(-1);
 303                  }
 304                  
 305                  if( sochandle < 0 ) {
 306                          TCP_DEBUGOUT("Socket handle non-valid\r\n");
 307                          return(-1);
 308                  }
 309                  
 310                  soc = &tcp_socket[sochandle];           /* Get referense        */
 311                          
 312                  if( (soc->type & TCP_TYPE_SERVER) == 0 ) {
 313                          TCP_DEBUGOUT("Socket has no server properties\r\n");
 314                          return(-1);
 315                  }
 316                  
 317                  if( soc->event_listener == 0) {
 318                          TCP_DEBUGOUT("ERROR:No event listener function specified\r\n");
 319                          return(-1);
 320                  }
 321                  
 322                  
 323                  if( (soc->state != TCP_STATE_RESERVED) &&
 324                          (soc->state != TCP_STATE_LISTENING)     &&
 325                          (soc->state != TCP_STATE_CLOSED) &&             
 326                          (soc->state != TCP_STATE_TIMED_WAIT)            ) {
 327                          TCP_DEBUGOUT("Not possible to listen, socket on connected state\r\n");
 328                          return(-1);
 329                  
 330                  }
 331                  
 332                                  
 333                  /* Init socket          */
 334                                          
 335                  soc->state = TCP_STATE_LISTENING;
 336                  /*soc->type = TCP_TYPE_SERVER;*/
 337                  soc->flags = 0;
 338                  soc->rem_ip = 0;
 339                  soc->remport = 0;
 340                  soc->locport = port;
 341                  soc->send_unacked = 0;
 342                  soc->myflags = 0;
 343                  soc->send_next = 0xFFFFFFFF;
 344                  soc->send_mtu = TCP_DEF_MTU;
 345                  soc->receive_next = 0;
 346                  soc->retries_left = 0;
 347                                  
 348                  TCP_DEBUGOUT("TCP listening socket created\r\n");
 349                                  
 350                  return(sochandle);
 351          
 352          }
C51 COMPILER V7.06   TCP                                                                   11/26/2004 11:32:45 PAGE 7   

 353          
 354          
 355          /** \brief Initialize connection establishment towards remote IP&port
 356           *  \ingroup tcp_app_api
 357           *      \author 
 358           *              \li Jari Lahti (jari.lahti@violasystems.com)
 359           *      \date 21.07.2002
 360           *      \param sochandle handle to socket to be used for connection establishment
 361           *      \param ip remote IP address to connect to 
 362           *      \param rport remote port number to connect to
 363           *      \param myport local port to use for connection. This value can be
 364           *              specified directly or, if a value of 0 is given, TCP module will
 365           *              determine local TCP port automatically.
 366           *      \return 
 367           *              \li -1 - Error
 368           *              \li >=0 - OK (Connection establishment procedure started. Socket handle
 369           *                      returned.)
 370           *
 371           *      Invoke this function to start connection establishment procedure towards
 372           *      remote host over some socket. This is only possible if socket was
 373           *      defined as either #TCP_TYPE_CLIENT or #TCP_TYPE_CLIENT_SERVER. Function 
 374           *      will make some basic checks and if everything is OK, corresponding tcb
 375           *      socket entry will be initialized and connection procedure started.
 376           */
 377          INT8 tcp_connect (INT8 sochandle, UINT32 ip, UINT16 rport, UINT16 myport )
 378          {
 379                  struct tcb* soc;
 380                  
 381                  TCP_DEBUGOUT("FUNCTION: tcp_connect\r\n");
 382          
 383                  if( NO_OF_TCPSOCKETS < 0 )
 384                          return(-1);
 385                  
 386                  if( NO_OF_TCPSOCKETS == 0 )
 387                          return(-1);
 388                  
 389                  if( sochandle > NO_OF_TCPSOCKETS ) {
 390                          TCP_DEBUGOUT("Socket handle non-valid\r\n");
 391                          return(-1);
 392                  }
 393                  
 394                  if( sochandle < 0 ) {
 395                          TCP_DEBUGOUT("Socket handle non-valid\r\n");
 396                          return(-1);
 397                  }
 398                  
 399                  /* Is the local port defined    */
 400                  
 401                  if( myport == 0 )
 402                          myport = tcp_getfreeport();
 403                  
 404                  if( myport == 0 )
 405                          return(-1);
 406                  
 407                  soc = &tcp_socket[sochandle];           /* Get referense        */
 408                  
 409                  /* Do we have client properties?        */
 410                  
 411                  if( (soc->type & TCP_TYPE_CLIENT) == 0 ) {
 412                          TCP_DEBUGOUT("Socket has no client properties\r\n");
 413                          return(-1);
 414                  }
C51 COMPILER V7.06   TCP                                                                   11/26/2004 11:32:45 PAGE 8   

 415                  
 416                  if( soc->event_listener == 0) {
 417                          TCP_DEBUGOUT("ERROR:No event listener function specified\r\n");
 418                          return(-1);
 419                  }
 420                  
 421                  /* Are we on LISTENING, RESERVED or CLOSED state        */
 422                  
 423                  if( (soc->state != TCP_STATE_RESERVED) &&
 424                          (soc->state != TCP_STATE_LISTENING) &&
 425                          (soc->state != TCP_STATE_CLOSED)                ) {
 426                          TCP_DEBUGOUT("Socket on unvalid state to initialize CONNECT\r\n");
 427                          return(-1);
 428                  }

⌨️ 快捷键说明

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