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

📄 tcp.lst

📁 一个通过8051来控制8019上网的程序 该程序已经通过并成功用于键盘
💻 LST
📖 第 1 页 / 共 3 页
字号:
 442   3                        
 443   3               // Send header options with the next message
 444   3               // Since timestamps are optional and we do not use
 445   3               // them, do not have to send them 
 446   3               // After sending the SYN ACK the client browser will
 447   3               // blast me with 2 messages, an ACK, and a HTTP GET
 448   3               tcp_send(FLG_SYN | FLG_ACK, 28, nr);
 449   3               
 450   3               // My SYN flag increments my sequence number
 451   3               // My sequence number is always updated to point to 
 452   3               // the next byte to be sent.  So the incoming ack
 453   3               // number should equal my sequence number  
 454   3               conxn[nr].my_sequence++;
 455   3            
 456   3               conxn[nr].state = STATE_SYN_RCVD;
 457   3       //        if (debug) printf("TCP: Entered SYN RCVD state\n");
 458   3            }
 459   2                      else 
 460   2            {
 461   3               // Sender is out of sync so send reset
 462   3               conxn[nr].ipaddr = 0;
 463   3               tcp_send(FLG_RST, 20, NO_CONNECTION);   
 464   3            } 
 465   2                      break;
 466   2      
 467   2      
 468   2            case STATE_SYN_RCVD:
 469   2            // He may already be sending me data - should process it
 470   2                      conxn[nr].his_sequence += data_len;
 471   2            conxn[nr].his_ack = tcp->ack_number;
 472   2                  
 473   2            if (tcp->flags & FLG_FIN)
 474   2                      {
 475   3                         // His FIN counts as a byte of data
 476   3               conxn[nr].his_sequence++;
 477   3               tcp_send(FLG_ACK, 20, nr);
 478   3               conxn[nr].state = STATE_CLOSE_WAIT;
 479   3       //        if (debug) printf("TCP: Entered CLOSE_WAIT state\n");
 480   3                     
 481   3                      // At this point we would normally wait for the application
 482   3               // to close.  For now, send FIN right away.
 483   3               tcp_send(FLG_FIN | FLG_ACK, 20, nr);
 484   3               conxn[nr].my_sequence++;   // For my FIN
 485   3               conxn[nr].state = STATE_LAST_ACK;
 486   3      //         if (debug) printf("TCP: Entered LAST ACK state\n");
 487   3            }
 488   2      
 489   2                      // Make sure he is ACKing my SYN
C51 COMPILER V7.06   TCP                                                                   06/26/2004 13:41:26 PAGE 9   

 490   2                      else if (tcp->ack_number == conxn[nr].my_sequence)
 491   2            {
 492   3               conxn[nr].state = STATE_ESTABLISHED;
 493   3       //        if (debug) printf("TCP: Entered ESTABLISHED state\n");
 494   3               // If sender sent data ignore it and he will resend
 495   3               // Do not send response because we received no
 496   3               // data... wait for client to send something to me 
 497   3            }
 498   2            break;
 499   2      
 500   2      
 501   2            case STATE_ESTABLISHED:
 502   2            conxn[nr].his_ack = tcp->ack_number;
 503   2                 
 504   2            if (tcp->flags & FLG_FIN)
 505   2                      {
 506   3                         // His FIN counts as a byte of data
 507   3               conxn[nr].his_sequence++;
 508   3               tcp_send(FLG_ACK, 20, nr);
 509   3               conxn[nr].state = STATE_CLOSE_WAIT;
 510   3       //        if (debug) printf("TCP: Entered CLOSE_WAIT state\n");
 511   3                     
 512   3                      // At this point we would normally wait for the application
 513   3               // to close.  For now, send FIN immediately.
 514   3               tcp_send(FLG_FIN | FLG_ACK, 20, nr);
 515   3               conxn[nr].my_sequence++;   // For my FIN
 516   3               conxn[nr].state = STATE_LAST_ACK;
 517   3      //         if (debug) printf("TCP: Entered LAST ACK state\n");
 518   3            }
 519   2              
 520   2                 break;
 521   2      
 522   2      
 523   2            case STATE_CLOSE_WAIT:
 524   2            // With this code, should not get here
 525   2       //     if (debug) printf("TCP: Oops! Rcvd unexpected message\n");
 526   2            
 527   2            break;
 528   2      
 529   2            
 530   2            case STATE_LAST_ACK:
 531   2            conxn[nr].his_ack = tcp->ack_number;
 532   2                  
 533   2            // If he ACK's my FIN then close
 534   2            if (tcp->ack_number == conxn[nr].my_sequence)
 535   2            {
 536   3               conxn[nr].state = STATE_CLOSED;
 537   3               conxn[nr].ipaddr = 0;  // Free up struct area
 538   3               just_closed = TRUE;
 539   3            }
 540   2            break;
 541   2      
 542   2            
 543   2            case STATE_FIN_WAIT_1:
 544   2            // He may still be sending me data - should process it
 545   2                      conxn[nr].his_sequence += data_len;
 546   2            conxn[nr].his_ack = tcp->ack_number;
 547   2                        
 548   2            if (tcp->flags & FLG_FIN)
 549   2            {
 550   3               // His FIN counts as a byte of data
 551   3               conxn[nr].his_sequence++;
C51 COMPILER V7.06   TCP                                                                   06/26/2004 13:41:26 PAGE 10  

 552   3               tcp_send(FLG_ACK, 20, nr);
 553   3               
 554   3               // If he has ACK'd my FIN then we can close connection
 555   3               if (tcp->ack_number == conxn[nr].my_sequence)
 556   3                              {
 557   4                      conxn[nr].state = STATE_TIME_WAIT;
 558   4       //             if (debug) printf("TCP: Entered TIME_WAIT state\n");
 559   4                     
 560   4                      conxn[nr].state = STATE_CLOSED;
 561   4                      conxn[nr].ipaddr = 0;  // Free up connection
 562   4                      just_closed = TRUE;
 563   4              }
 564   3                              else
 565   3                              {
 566   4                                      // He has not ACK'd my FIN.  This happens when there is a simultaneous
 567   4                                      // close - I got his FIN but he has not yet ACK'd my FIN
 568   4                                      conxn[nr].state = STATE_CLOSING;
 569   4      //                              if (debug) printf("TCP: Entered CLOSING state\n");
 570   4                              }
 571   3                      }
 572   2            else if (tcp->ack_number == conxn[nr].my_sequence)
 573   2            {
 574   3               // He has ACK'd my FIN but has not sent a FIN yet himself
 575   3               conxn[nr].state = STATE_FIN_WAIT_2;
 576   3       //        if (debug) printf("TCP: Entered FIN_WAIT_2 state\n");
 577   3            }
 578   2            break;
 579   2      
 580   2            
 581   2            case STATE_FIN_WAIT_2:
 582   2            // He may still be sending me data - should process it
 583   2                      conxn[nr].his_sequence += data_len;
 584   2            conxn[nr].his_ack = tcp->ack_number;
 585   2            
 586   2            if (tcp->flags & FLG_FIN)
 587   2            {
 588   3               conxn[nr].his_sequence++; // For his FIN flag
 589   3               tcp_send(FLG_ACK, 20, nr);
 590   3               conxn[nr].state = STATE_TIME_WAIT;
 591   3       //        if (debug) printf("TCP: Entered TIME_WAIT state\n");
 592   3               conxn[nr].state = STATE_CLOSED;
 593   3               conxn[nr].ipaddr = 0;  // Free up struct area
 594   3               just_closed = TRUE;
 595   3            }
 596   2            break;
 597   2                  
 598   2                  
 599   2            case STATE_TIME_WAIT:
 600   2            // With this code, should not get here
 601   2       //     if (debug) printf("TCP: Oops! In TIME_WAIT state\n");
 602   2            break;
 603   2      
 604   2            
 605   2            case STATE_CLOSING:
 606   2            // Simultaneous close has happened. I have received his FIN
 607   2            // but he has not yet ACK'd my FIN.  Waiting for ACK.
 608   2                      // Will not receive data in this state
 609   2                      conxn[nr].his_ack = tcp->ack_number;
 610   2                      
 611   2                      if (tcp->ack_number == conxn[nr].my_sequence)
 612   2            {
 613   3                         conxn[nr].state = STATE_TIME_WAIT;
C51 COMPILER V7.06   TCP                                                                   06/26/2004 13:41:26 PAGE 11  

 614   3       //        if (debug) printf("TCP: Entered TIME_WAIT state\n");
 615   3               
 616   3               // Do not send any response to his ACK
 617   3               conxn[nr].state = STATE_CLOSED;
 618   3               conxn[nr].ipaddr = 0;  // Free up struct area
 619   3               just_closed = TRUE;
 620   3            }
 621   2            break;
 622   2      
 623   2            
 624   2            default:
 625   2       //     if (debug) printf("TCP: Error, no handler\n");
 626   2            break;
 627   2         }
 628   1         
 629   1         // This is for debug, to see when conxn closes
 630   1         if (just_closed)
 631   1         {
 632   2            just_closed = FALSE;
 633   2            if (debug)
 634   2            {                                                                                                   
             -                                                                                                                        
             -                                                                                                                        
             -                                                                                                                        
             -                                                                                                                        
             -                                                                                                                        
             -                                           
 635   3       //        printf("TCP: Closed connection ");
 636   3               memset(text, 0, 10);
 637   3               itoa((UINT)nr, text, 10);
 638   3      //                 printf(text);
 639   3      //                 printf("\n");
 640   3            }
 641   2         }
 642   1      }
 643          
 644          
 645          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   3917    ----
   CONSTANT SIZE    =     10    ----
   XDATA SIZE       =    231      17
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =      6      25
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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