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

📄 tcp.lst

📁 tcpip.rar 是一个51控制8019的程序,我已用于商用.还很稳定,有兴趣的可以看一下,对TCP IP UDP ICMP ARP RARP HTTP均可以实现.
💻 LST
📖 第 1 页 / 共 3 页
字号:
 460   3               // After sending the SYN ACK the client browser will
 461   3               // blast me with 2 messages, an ACK, and a HTTP GET
 462   3               tcp_send(FLG_SYN | FLG_ACK, 28, nr);
 463   3               
 464   3               // My SYN flag increments my sequence number
 465   3               // My sequence number is always updated to point to 
 466   3               // the next byte to be sent.  So the incoming ack
 467   3               // number should equal my sequence number  
 468   3               conxn[nr].my_sequence++;
 469   3            
 470   3               conxn[nr].state = STATE_SYN_RCVD;
 471   3               if (debug) printf("TCP: Entered SYN RCVD state\n");
 472   3            }
 473   2                      else 
 474   2            {
 475   3               // Sender is out of sync so send reset
 476   3               conxn[nr].ipaddr = 0;
 477   3               tcp_send(FLG_RST, 20, NO_CONNECTION);   
 478   3            } 
 479   2                      break;
 480   2      
 481   2      
 482   2            case STATE_SYN_RCVD:
 483   2            // He may already be sending me data - should process it
 484   2                      conxn[nr].his_sequence += data_len;
 485   2            conxn[nr].his_ack = tcp->ack_number;
 486   2                  
 487   2            if (tcp->flags & FLG_FIN)
 488   2                      {
 489   3                         // His FIN counts as a byte of data
C51 COMPILER V7.06   TCP                                                                   05/19/2004 15:18:11 PAGE 9   

 490   3               conxn[nr].his_sequence++;
 491   3               tcp_send(FLG_ACK, 20, nr);
 492   3               conxn[nr].state = STATE_CLOSE_WAIT;
 493   3               if (debug) printf("TCP: Entered CLOSE_WAIT state\n");
 494   3                     
 495   3                      // At this point we would normally wait for the application
 496   3               // to close.  For now, send FIN right away.
 497   3               tcp_send(FLG_FIN | FLG_ACK, 20, nr);
 498   3               conxn[nr].my_sequence++;   // For my FIN
 499   3               conxn[nr].state = STATE_LAST_ACK;
 500   3               if (debug) printf("TCP: Entered LAST ACK state\n");
 501   3            }
 502   2      
 503   2                      // Make sure he is ACKing my SYN
 504   2                      else if (tcp->ack_number == conxn[nr].my_sequence)
 505   2            {
 506   3               conxn[nr].state = STATE_ESTABLISHED;
 507   3               if (debug) printf("TCP: Entered ESTABLISHED state\n");
 508   3               // If sender sent data ignore it and he will resend
 509   3               // Do not send response because we received no
 510   3               // data... wait for client to send something to me 
 511   3            }
 512   2            break;
 513   2      
 514   2      
 515   2            case STATE_ESTABLISHED:
 516   2            conxn[nr].his_ack = tcp->ack_number;
 517   2                 
 518   2            if (tcp->flags & FLG_FIN)
 519   2                      {
 520   3                         // His FIN counts as a byte of data
 521   3               conxn[nr].his_sequence++;
 522   3               tcp_send(FLG_ACK, 20, nr);
 523   3               conxn[nr].state = STATE_CLOSE_WAIT;
 524   3               if (debug) printf("TCP: Entered CLOSE_WAIT state\n");
 525   3                     
 526   3                      // At this point we would normally wait for the application
 527   3               // to close.  For now, send FIN immediately.
 528   3               tcp_send(FLG_FIN | FLG_ACK, 20, nr);
 529   3               conxn[nr].my_sequence++;   // For my FIN
 530   3               conxn[nr].state = STATE_LAST_ACK;
 531   3               if (debug) printf("TCP: Entered LAST ACK state\n");
 532   3            }
 533   2                      else if (data_len != 0)
 534   2            {
 535   3                              // Received normal TCP segment from sender with data
 536   3              // Send an ACK immediately and pass the data on to
 537   3                              // the application
 538   3                              conxn[nr].his_sequence += data_len;
 539   3               tcp_send(FLG_ACK, 20, nr);             // Send ACK
 540   3                
 541   3                                                                                                      
 542   3                              // Send pointer to start of TCP payload
 543   3                              // http_server increments my sequence number when 
 544   3               // sending so don't worry about it here
 545   3               result = http_server(inbuf, header_len, nr, 0);
 546   3                                                                      
 547   3                              // Start timer to close conxn if no activity
 548   3                              conxn[nr].inactivity = INACTIVITY_TIME;
 549   3                      }
 550   2                 break;
 551   2      
C51 COMPILER V7.06   TCP                                                                   05/19/2004 15:18:11 PAGE 10  

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

 614   2            conxn[nr].his_ack = tcp->ack_number;
 615   2            
 616   2            if (tcp->flags & FLG_FIN)
 617   2            {
 618   3               conxn[nr].his_sequence++; // For his FIN flag
 619   3               tcp_send(FLG_ACK, 20, nr);
 620   3               conxn[nr].state = STATE_TIME_WAIT;
 621   3               if (debug) printf("TCP: Entered TIME_WAIT state\n");
 622   3               conxn[nr].state = STATE_CLOSED;
 623   3               conxn[nr].ipaddr = 0;  // Free up struct area
 624   3               just_closed = TRUE;
 625   3            }
 626   2            break;
 627   2                  
 628   2                  
 629   2            case STATE_TIME_WAIT:
 630   2            // With this code, should not get here
 631   2            if (debug) printf("TCP: Oops! In TIME_WAIT state\n");
 632   2            break;
 633   2      
 634   2            
 635   2            case STATE_CLOSING:
 636   2            // Simultaneous close has happened. I have received his FIN
 637   2            // but he has not yet ACK'd my FIN.  Waiting for ACK.
 638   2                      // Will not receive data in this state
 639   2                      conxn[nr].his_ack = tcp->ack_number;
 640   2                      
 641   2                      if (tcp->ack_number == conxn[nr].my_sequence)
 642   2            {
 643   3                         conxn[nr].state = STATE_TIME_WAIT;
 644   3               if (debug) printf("TCP: Entered TIME_WAIT state\n");
 645   3               
 646   3               // Do not send any response to his ACK
 647   3               conxn[nr].state = STATE_CLOSED;
 648   3               conxn[nr].ipaddr = 0;  // Free up struct area
 649   3               just_closed = TRUE;
 650   3            }
 651   2            break;
 652   2      
 653   2            
 654   2            default:
 655   2            if (debug) printf("TCP: Error, no handler\n");
 656   2            break;
 657   2         }
 658   1         
 659   1         // This is for debug, to see when conxn closes
 660   1         if (just_closed)
 661   1         {
 662   2            just_closed = FALSE;
 663   2            if (debug)
 664   2            {
 665   3               printf("TCP: Closed connection ");
 666   3               memset(text, 0, 10);
 667   3               itoa((UINT)nr, text, 10);
 668   3                         printf(text);
 669   3                         printf("\n");
 670   3            }
 671   2         }
 672   1      }
 673          
 674          
 675          
C51 COMPILER V7.06   TCP                                                                   05/19/2004 15:18:11 PAGE 12  



MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   4739    ----
   CONSTANT SIZE    =    839    ----
   XDATA SIZE       =    231      19
   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 + -