Advertisement
Guest User

Untitled

a guest
Jul 18th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. /*
  2.  * daytime.c: a simple network service based on lwIP and mini-os
  3.  *
  4.  * Tim Deegan <Tim.Deegan@eu.citrix.net>, July 2007
  5.  */
  6.  
  7. #include <os.h>
  8. #include <xmalloc.h>
  9. #include <console.h>
  10. #include <netfront.h>
  11. #include <lwip/api.h>
  12.  
  13. static char message[29];
  14.  
  15. void run_server(void *p)
  16. {
  17.     struct ip_addr listenaddr = { 0 };
  18.     struct netconn *listener;
  19.     struct netconn *session;
  20.     struct timeval tv;
  21.     err_t rc;
  22.  
  23.     start_networking();
  24.  
  25.     if (1) {
  26.         struct ip_addr ipaddr = { htonl(0x0a00020A) };
  27.         struct ip_addr netmask = { htonl(0xffffff00) };
  28.         struct ip_addr gw = { 0 };
  29.         networking_set_addr(&ipaddr, &netmask, &gw);
  30.     }
  31.  
  32.     tprintk("Opening connection\n");
  33.  
  34.     listener = netconn_new(NETCONN_TCP);
  35.     tprintk("Connection at %p\n", listener);
  36.  
  37.     rc = netconn_bind(listener, &listenaddr, 13);
  38.     if (rc != ERR_OK) {
  39.         tprintk("Failed to bind connection: %i\n", rc);
  40.         return;
  41.     }
  42.  
  43.     rc = netconn_listen(listener);
  44.     if (rc != ERR_OK) {
  45.         tprintk("Failed to listen on connection: %i\n", rc);
  46.         return;
  47.     }
  48.  
  49.     while (1) {
  50.         session = netconn_accept(listener);
  51.         if (session == NULL)
  52.             continue;
  53.     tprintk("Got a new connection\n");
  54.  
  55.         gettimeofday(&tv, NULL);
  56.         sprintf(message, "%20lu.%6.6lu\n", tv.tv_sec, tv.tv_usec);
  57.         (void) netconn_write(session, message, strlen(message), NETCONN_COPY);
  58.         (void) netconn_disconnect(session);
  59.         (void) netconn_delete(session);
  60.     }
  61. }
  62.  
  63.  
  64. int app_main(start_info_t *si)
  65. {
  66.     create_thread("server", run_server, NULL);
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement