Advertisement
Guest User

Untitled

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