Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 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. #define DIE(assertion, call_description)                \
  8.     do {                                \
  9.         if (assertion) {                    \
  10.             tprintk("(%s, %d): %s\n",           \
  11.                     __FILE__, __LINE__, call_description);  \
  12.             return;             \
  13.         }                           \
  14.     } while(0)
  15.  
  16.  
  17. void client_thread(void *p)
  18. {
  19.  
  20.     struct netconn *netConn;
  21.     struct ip_addr remote_ip;
  22.     int rc;
  23.  
  24.     start_networking();
  25.  
  26.     struct ip_addr local_ip = { htonl(0x0a00020b) };
  27.     struct ip_addr netmask = { htonl(0xffffff00) };
  28.     struct ip_addr gw = { 0 };
  29.     networking_set_addr(&local_ip, &netmask, &gw);
  30.  
  31.     netConn = netconn_new(NETCONN_TCP);
  32.     DIE(!netConn, "netconn_new");
  33.  
  34.     remote_ip.addr = htonl(0x0a00020a);
  35.     rc = netconn_connect(netConn, &remote_ip, 13);
  36.     DIE(rc != ERR_OK, "netconn_connect");
  37.  
  38.     tprintk("Connected successfully!\n");
  39.  
  40.     struct netbuf *recvBuf;
  41.     recvBuf = netconn_recv(netConn);
  42.     DIE(!recvBuf, "netconn_recv");  
  43.  
  44.     char *buf;
  45.     int sz;
  46.  
  47.     rc = netbuf_data(recvBuf, (void**)&buf, (u16_t*)&sz);
  48.     DIE(rc != ERR_OK, "ntebuf_data");
  49.     tprintk( "Received: %s\n", buf);
  50.  
  51.     free(recvBuf);
  52.     netconn_disconnect(netConn);
  53.     netconn_delete(netConn);
  54.    
  55. }
  56.  
  57.  
  58. int app_main(start_info_t *si)
  59. {  
  60.     create_thread("client", client_thread, NULL);
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement