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

📄 chargenloop.c

📁 picoos源码。The RTOS and the TCP/IP stack will be built automatically.
💻 C
字号:
/*
 *  Copyright (c) 2005, Dennis Kuschel.
 *  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.
 *
 */

#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/memp.h"
#include "lwip/tcpip.h"
#include "lwip/ip_addr.h"
#include "lwip/stats.h"
#include "arch/perf.h"
#include "netif/loopif.h"

#include "lwip/sockets.h"

#include <picoos.h>


/*-------------------------------------------------------------------------*/


/* imported functions */
extern void chargen_init(void);

/* local functions */
int main_setup(void);
int main_program(void);
static void tcpip_init_done(void * arg);
static void lwip_init(void);
static void chargen_sink(void);

/* global variables */
struct netif loopif;


/*-------------------------------------------------------------------------*/

/*  This function connects to a chargen server on port 19
 *  and prints out all the data it gets.
 */
static void chargen_sink(void)
{
  int rc, s;
  struct sockaddr_in saddr;
  fd_set masterset, readset;
  char rxbuffer[200+1];
    
  s = socket(AF_INET, SOCK_STREAM, 0);
  LWIP_ASSERT("chargen_sink(): Socket create failed.", s >= 0);

  memset(&saddr, 0, sizeof(saddr));
  saddr.sin_family = AF_INET;
  saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  saddr.sin_port = htons(19);  /* chargen */

  if (connect(s, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
    LWIP_ASSERT("chargen_sink(): Socket connect failed.", 0);

  FD_ZERO(&masterset);
  FD_SET(s, &masterset);

  for (;;)
  {
    readset = masterset;
    rc = select(s + 1, &readset, NULL, NULL, NULL);
    LWIP_ASSERT("chargen_sink(): connection is broken.", rc >= 0);

    if (rc > 0)
    {
      if (FD_ISSET(s, &readset))
      {
        rc = recv(s, rxbuffer, 200, 0);
        LWIP_ASSERT("chargen_sink(): recv failed.", rc > 0);
        if (rc > 0)
        {
          rxbuffer[rc] = 0;
          printf("%s", rxbuffer);
        }
      }
    }
  }
}


/*-------------------------------------------------------------------------*/


/*  lwIP initialization
 *  (with loopback interface)
 */
static void lwip_init(void)
{
  struct ip_addr  ipaddr;
  struct ip_addr  netmask;
  struct ip_addr  gatew;
  sys_sem_t       sem;
    
#ifdef PERF
  perf_init("lwip.perf");
#endif /* PERF */
#ifdef STATS
  stats_init();
#endif /* STATS */
  sys_init();
  mem_init();
  memp_init();
  pbuf_init();
  netif_init();
    
  IP4_ADDR(&gatew,   127,0,0,1);
  IP4_ADDR(&ipaddr,  127,0,0,1);
  IP4_ADDR(&netmask, 255,0,0,0);
  
  netif_add(&loopif, &ipaddr, &netmask, &gatew,
            NULL, loopif_init, tcpip_input);
  
  netif_set_default(&loopif);
  netif_set_up(&loopif);

  sem = sys_sem_new(0);
  tcpip_init(tcpip_init_done, &sem);
  sys_sem_wait(sem);
  sys_sem_free(sem);
}


/* initialization callback routine
 */
static void tcpip_init_done(void * arg)
{
  sys_sem_t *sem = arg;
  sys_sem_signal(*sem);
}


/*-------------------------------------------------------------------------*/


/*  main program
 *  (first task running in pico]OS context)
 */
int main_program(void)
{
  printf("\n" POS_STARTUPSTRING "\n\n");

  /* initialize lwIP TCP/IP stack */
  lwip_init();

  /* initialize application code */
  chargen_init();

  printf("starting chargen sink in a few seconds...\n\n");
  posTaskSleep(MS(5000));

  /* call the chargen sink */
  chargen_sink();

  return 0;
}


/*  main setup function
 *  (is called before pico]OS is started)
 */
int main_setup(void)
{
  return 0;
}

⌨️ 快捷键说明

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