1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| #include <stdio.h> #include "xparameters.h" #include "netif/xadapter.h" #include "xil_printf.h" #include "xil_printf.h" #include "lwip/init.h" #include "lwip/inet.h" #include "lwip/sockets.h" #include "lwipopts.h" #include "xil_printf.h" #include "FreeRTOS.h" #include "task.h" #include "sleep.h"
#define THREAD_STACKSIZE 1024
#define EMAC_ADDR XPAR_XEMACPS_0_BASEADDR
static struct netif client_netif; struct sockaddr_in local_addr, remote_addr;
int complete_nw_thread = 0;
void network_thread() { struct netif *netif; unsigned char mac_ethernet_address[] = { 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 };
ip_addr_t ipaddr, netmask, gw;
IP4_ADDR(&ipaddr, 192, 168, 1, 10); IP4_ADDR(&netmask, 255, 255, 255, 0); IP4_ADDR(&gw, 192, 168, 1, 1);
netif = &client_netif;
if (!xemac_add(netif, &ipaddr, &netmask, &gw, mac_ethernet_address, EMAC_ADDR)) { xil_printf("Error adding N/W interface\r\n"); return ; }
netif_set_default(netif);
netif_set_up(netif);
sys_thread_new("xemacif_input_thread", (void(*)(void*))xemacif_input_thread, netif, THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
complete_nw_thread = 1; vTaskDelete(NULL); }
void udp_socket_thread() { int count;
int socket_client = lwip_socket(AF_INET, SOCK_DGRAM, 0); if(socket_client < 0){ xil_printf("create socket failed \r\n"); return ; } memset(&remote_addr, 0, sizeof(struct sockaddr_in)); remote_addr.sin_family = AF_INET; remote_addr.sin_port = htons(8080); remote_addr.sin_addr.s_addr = inet_addr("192.168.1.100");
memset(&local_addr, 0, sizeof(struct sockaddr_in)); local_addr.sin_family = AF_INET; local_addr.sin_port = htons(8080); local_addr.sin_addr.s_addr = inet_addr("192.168.1.10");
int err = lwip_connect(socket_client, (struct sockaddr *)&remote_addr, sizeof(remote_addr)); if (err != ERR_OK) { xil_printf("UDP client: Error on connect: %d\r\n", err); lwip_close(socket_client); return ; } err = lwip_bind(socket_client, (struct sockaddr*)&local_addr, sizeof(local_addr)); if (err != ERR_OK) { xil_printf("UDP client: Error on bind: %d\r\n", err); lwip_close(socket_client); return ; }
char tx_buf[]="Hello world---AAA\r\n"; char rx_buf[16]={}; socklen_t rx_bytes; while(1){ count = lwip_sendto(socket_client, tx_buf, sizeof(tx_buf), 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr)); if (count <= 0) { xil_printf("send failed\r\n"); }
lwip_recvfrom(socket_client, rx_buf, sizeof(rx_buf), 0, (struct sockaddr *)&remote_addr, &rx_bytes); xil_printf("Receive data is: %s \r\n", rx_buf); }
lwip_close(socket_client); vTaskDelete(NULL); }
int main_thread() { xil_printf("-----lwIP Socket Mode UDP Client Application------\r\n");
lwip_init();
sys_thread_new("nw_thread", network_thread, NULL, THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
while(!complete_nw_thread){ usleep(5); }
sys_thread_new("udp_socket_thread", udp_socket_thread, NULL, THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
vTaskDelete(NULL); return 0; }
int main() { sys_thread_new("main_thread", (void(*)(void*))main_thread, 0, THREAD_STACKSIZE, DEFAULT_THREAD_PRIO); vTaskStartScheduler(); while(1); return 0; }
|