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

📄 main1.c

📁 51单片机实现网页显示功能
💻 C
字号:
/*
 * Copyright (c) 2003 Electric Application Laboratory of NAN KAI University
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * Author: Li Zhanglin <www.zlmcu.com>
 * Version: 1.0.0 20050830
 */
#include "..\GloblDef\config.h"
#include "..\GloblDef\GloblDef.h"
#include "..\TCPIP\TCPIPmem.h"
#include "..\TCPIP\IP.h"
#include "..\TCPIP\Netif.h"
#include "..\Netif\etherif.h"
#include "..\Netif\ARP.h"
#include "..\TCPIP\TCP.h"
#include "..\TCPIP\UDP.h"

#ifdef ARCH_WINDOWS
#include "..\Architecture\Windows\WinDrive.h"
#include "..\Architecture\Windows\arch_win.h"
#endif

#ifdef ARCH_ARM
# ifdef ARM_SDT
#  include "..\Architecture\ARM_SDT\ArmDrive.h"
#  include "..\Architecture\ARM_SDT\arch_arm.h"
# endif
# ifdef ARM_ADS
#  include "..\Architecture\ARM_ADS\ArmDrive.h"
#  include "..\Architecture\ARM_ADS\arch_arm.h"
# endif
#endif

#define HTTP_SERVER_PORT 80

socket  DT_XDATA * DT_XDATA HttpSocket = NULL;
socket  DT_XDATA * DT_XDATA ExAccept = NULL;

char DT_CODE *page =
{
 "HTTP/1.0 200 OK\nContent-Type: text/html\n\n"
 "<HTML><HEAD><TITLE>Welcome to ZLIP Website</TITLE></HEAD>"
 "ZLIP: http://www.zlmcu.com<;P>"
 "</BODY></HTML>\n"
};

/* discription: callback function for TCP recv. here called when 
  receive data when act as server 
 buf: the recv data
 size: the size of recv data
*/
void OnAcceptRecv(void DT_XDATA *buf,zl_u16 size) REENTRANT_MUL
{
 /* printf received data */
#ifdef MSG_PRINT
 zl_u16 tsize;
 zl_u8 DT_XDATA *tbuf;
 for(tsize = size, tbuf = (zl_u8 DT_XDATA *)buf; tsize != 0; tsize--)
  printf("%c",*tbuf++);
#endif
}

/* discription: callback function for TCP close. here called when 
  peer want to close the connection
 pSocket: the connection
*/
void OnHttpClose(socket DT_XDATA * pSocket) REENTRANT_MUL
{
 MSG("\nConnection will been gracefully closed");

 TCPClose(pSocket); /* we close too */
}
/* discription: callback function for TCP accept. here called when 
  recieve tcp connection request when act as server
 pnewSocket: the new socket to deal with the connection
*/
void OnHttpAccept(socket DT_XDATA *pNewSocket) REENTRANT_MUL
{
 zl_u16 i;
 MSG("\nAccept a connection");

 ExAccept = pNewSocket;
 pNewSocket->recv  = OnAcceptRecv;
 pNewSocket->close = OnHttpClose;

 for(i = 0; page[i]; i++);
 TCPSend(ExAccept, page, i);

 TCPClose(ExAccept);
}

int main(void)
{

 struct SEtherDevice DT_XDATA DevRTL;
 zl_u8 DT_XDATA EtherAddr[ETHER_ADDR_LEN] = {0x52,0x54,0x4c,0x30,0x2e,0x2f};

 /* whenever in little-endian or big-endian, 192.168.2.1 is 0xc0a80201 */
 IP_ADDR  IPAddr = 0x0A5D7949; /* 0x48:72 */
 IP_ADDR  NetMask = 0xffffff00; 
 IP_ADDR  GateWay = 0x0A5D7901; 
 IP_ADDR  DestIP = 0x0A5D79CC; /* 0x1A:26 */
   
 /*
  * init
  */
 
 SysInit();

 /* init. the order is not important */
 NetIfInit();
 ARPInit();
 MemInit();
 DriverInit(EtherAddr); 

 /* init Devcie struct and init this device */
 EtherDevInit(&DevRTL,EtherAddr,DriverSendPacket,DriverReceivePacket);

 /* add this device to NetIf */
 NetIfAdd(IPAddr,NetMask,GateWay,EtherInput,EtherOutput,&DevRTL);

 TimerStart();
  
 TCPInit();

 /* Illustrate how to listen as a server */
 HttpSocket = TCPSocket(IPAddr);
 ExAccept   = NULL;
 if(TCPListen(HttpSocket,HTTP_SERVER_PORT,OnHttpAccept))
  MSG("\nListen...");
 else
  MSG("\nListen Failed!");

 MSG("\nPress Any Key to End TCP Test!");
 WAIT_FOR_KEY_PRESS;

 TCPAbort(ExAccept); 
 TCPAbort(HttpSocket); 

 /* cancel the timer */
 TimerEnd();  

 return TRUE;
}

⌨️ 快捷键说明

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