/*-----------------------------------------------------------------------------------*/ static void tcpecho_thread(void *arg) { struct netconn *conn, *newconn; err_t err; LWIP_UNUSED_ARG(arg); /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Bind connection to well known port number TCP_ECHO_PORT. */ netconn_bind(conn, IP_ADDR_ANY, TCP_ECHO_PORT); /* Tell connection to go into listening mode. */ netconn_listen(conn); while (1) { /* Grab new connection. */ newconn = netconn_accept(conn); printf("accepted new connection %p\n", newconn); /* Process the new connection. */ if (newconn != NULL) { struct netbuf *buf; void *data; u16_t len; while ((buf = netconn_recv(newconn)) != NULL) { printf("Recved\n"); do { netbuf_data(buf, &data, &len); err = netconn_write(newconn, data, len, NETCONN_COPY); #if 0 if (err != ERR_OK) { printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(err)); } #endif } while (netbuf_next(buf) >= 0); netbuf_delete(buf); } printf("Got EOF, looping\n"); /* Close connection and discard connection identifier. */ netconn_close(newconn); netconn_delete(newconn); } } }
void vBasicWEBServer( void *pvParameters ) { struct netconn *pxHTTPListener, *pxNewConnection; struct ip_addr xIpAddr, xNetMast, xGateway; extern err_t ethernetif_init( struct netif *netif ); static struct netif EMAC_if; /* Parameters are not used - suppress compiler error. */ ( void ) pvParameters; /* Create and configure the EMAC interface. */ IP4_ADDR(&xIpAddr,emacIPADDR0,emacIPADDR1,emacIPADDR2,emacIPADDR3); IP4_ADDR(&xNetMast,emacNET_MASK0,emacNET_MASK1,emacNET_MASK2,emacNET_MASK3); IP4_ADDR(&xGateway,emacGATEWAY_ADDR0,emacGATEWAY_ADDR1,emacGATEWAY_ADDR2,emacGATEWAY_ADDR3); netif_add(&EMAC_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input); /* make it the default interface */ netif_set_default(&EMAC_if); /* bring it up */ netif_set_up(&EMAC_if); /* Create a new tcp connection handle */ pxHTTPListener = netconn_new( NETCONN_TCP ); netconn_bind(pxHTTPListener, NULL, webHTTP_PORT ); netconn_listen( pxHTTPListener ); /* Loop forever */ for( ;; ) { /* Wait for connection. */ pxNewConnection = netconn_accept(pxHTTPListener); if(pxNewConnection != NULL) { /* Service connection. */ vProcessConnection( pxNewConnection ); while( netconn_delete( pxNewConnection ) != ERR_OK ) { vTaskDelay( webSHORT_DELAY ); } } } }
static struct netconn * createServer(int port) { struct netconn * conn; err_t err; conn = netconn_new_with_callback(NETCONN_TCP, scpi_netconn_callback); if (conn == NULL) { return NULL; } err = netconn_bind(conn, NULL, port); if (err != ERR_OK) { netconn_delete(conn); return NULL; } netconn_listen(conn); return conn; }
void telnet_thread(void *arg) { struct netconn *conn, *newconn; struct ip_addr l_ip; IP4_ADDR(&l_ip, 192,168,1,2); /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Bind connection to well known port number 7. */ netconn_bind(conn, &l_ip, 23); /* Tell connection to go into listening mode. */ netconn_listen(conn); while(1) { newconn = netconn_accept(conn); server_client(newconn); } }
void tcpecho_entry(void *parameter) { struct netconn *conn, *newconn; err_t err; /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Bind connection to well known port number 7. */ netconn_bind(conn, NULL, TCP_ECHO_PORT); /* Tell connection to go into listening mode. */ netconn_listen(conn); while(1) { /* Grab new connection. */ newconn = netconn_accept(conn); /* Process the new connection. */ if(newconn != NULL) { struct netbuf *buf; void *data; u16_t len; while((buf = netconn_recv(newconn)) != NULL) { do { netbuf_data(buf, &data, &len); err = netconn_write(newconn, data, len, NETCONN_COPY); if(err != ERR_OK){} } while(netbuf_next(buf) >= 0); netbuf_delete(buf); } /* Close connection and discard connection identifier. */ netconn_delete(newconn); } } }
/** * @brief http server thread * @param arg: pointer on argument(not used here) * @retval None */ static void http_server_netconn_thread(void *arg) { struct netconn *conn, *newconn; err_t err; /* Create a new TCP connection handle */ conn = netconn_new(NETCONN_TCP); if (conn!= NULL) { /* Bind to port 80 (HTTP) with default IP address */ err = netconn_bind(conn, NULL, 80); if (err == ERR_OK) { /* Put the connection into LISTEN state */ netconn_listen(conn); while(1) { /* accept any icoming connection */ newconn = netconn_accept(conn); /* serve connection */ http_server_serve(newconn); /* delete connection */ netconn_delete(newconn); } } else { printf("can not bind netconn"); } } else { printf("can not create netconn"); } }
/*-----------------------------------------------------------------------------------*/ int lwip_listen(int s, int backlog) { struct lwip_socket *sock; err_t err; DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog)); sock = get_socket(s); if (!sock) { return -1; } err = netconn_listen(sock->conn); if (err != ERR_OK) { DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err)); sock_set_errno(sock, err_to_errno(err)); return -1; } sock_set_errno(sock, 0); return 0; }
static msg_t rawd_loop(void *data) { struct netconn *nc, *ncnew; int ret; chRegSetThreadName("net/rawd"); nc = netconn_new(NETCONN_TCP); if (!nc) return 1; netconn_bind(nc, NULL, RAW_PORT); netconn_listen(nc); for (;;) { ret = netconn_accept(nc, &ncnew); if (ret) continue; rawd_new(ncnew); } return 0; }
/** * @brief TCP server thread * @param arg: pointer on argument(not used here) * @retval None * TCP 连接线程,有它创建其他线程 */ static void tcp_netconn_thread(void *arg) { struct netconn *conn, *newconn; err_t err; int i; //char disp[20]="get a connect!\0"; char thread_name[16]; int thread_num=0; /* Create a new TCP connection handle */ /*创建返回数据处理进程,该进程处理feedback数据*/ xTaskCreate(NetFeedBack_thread, "FEEDBACKPROCESS", NET_FEEDBACK_THREAD_STACKSIZE, NULL,FEED_BACK_PRIO, NULL); conn = netconn_new(NETCONN_TCP); conn->recv_timeout = 10000; //等待连接超时时间为10s if (conn!= NULL) { /* Bind to port 10000 with default IP address */ err = netconn_bind(conn, NULL, 10000); if (err == ERR_OK) { /* Put the connection into LISTEN state */ netconn_listen(conn); printf(" start to listen the connect \r\n"); while(1) { /* accept any icoming connection */ newconn = netconn_accept(conn); if(NULL != newconn) //成功连接 { printf("a client connected to the server "); if(client_used<MAX_CLIENT_NUM) //连接数小于最大连接数 { client_used++; for(i=0;i<MAX_CLIENT_NUM;i++) { if(user_info[i].conn==NULL) { user_info[i].conn = newconn; user_info[i].is_used = 1; user_info[i].user_id = ++thread_num; user_info[i].period = 0; /*设定新任务名字*/ sprintf(thread_name,"USERPROCESS%d\0",thread_num); /*创建新任务*/ LCD_DisplayStringLine(Line3, (uint8_t*)thread_name); printf(thread_name); printf(" new client thread\r\n"); //netconn_write(newconn,thread_name,13,NETCONN_NOCOPY); xTaskCreate(NetUserProcess_thread, thread_name, NET_USERPROCESS_STACKSIZE ,(void*)&user_info[i], USERPROCESS_THREAD_PRIO,NULL); break; } } } else { //超出最大连接数据,关闭连接 netconn_write(newconn,"net user is reach the max number\r\n\0",32,NETCONN_COPY); netconn_close(newconn); printf("net user is reach the max number,close it\r\n\0"); } } else { vTaskDelay(100); } /* serve connection */ //http_server_serve(newconn); /* delete connection */ //netconn_delete(newconn); } } else { printf("can not bind netconn"); } } else { printf("can not create netconn"); } }
/*! \brief WEB server main task * check for incoming connection and process it * */ portTASK_FUNCTION( vBasicWEBServer, pvParameters ) { struct netconn *pxHTTPListener, *pxNewConnection; portCHAR token[6]; portSHORT TaskIdx; portLONG err_count; /* initialize current nb connection */ sCurrentNbHTTPConn = 0; vSemaphoreCreateBinary( xMutexNbHTTPConn ); x_default_page_len = strlen( pcDefaultPage ); /* HTTP configuration. */ // Get the xCFGMutex. if( pdFALSE == x_supervisor_SemaphoreTake( xCFGMutex, 0 ) ) { // Failed to get the CFG mutex, use the default HTTP config. webHttpPort = webHTTP_PORT; } // Mutex has been taken else { // get the field value for port number if (config_file_get_value(HTTP_CONFIG_FILE, "port" , token) >= 0) { sscanf(token, "%u", &webHttpPort); } // if it does not exist, use the default value else { webHttpPort = webHTTP_PORT; } // Release the xCFGMutex. x_supervisor_SemaphoreGive( xCFGMutex ); } // Create a new tcp connection handle pxHTTPListener = netconn_new( NETCONN_TCP ); netconn_bind(pxHTTPListener, NULL, webHttpPort ); netconn_listen( pxHTTPListener ); // Loop forever for( ;; ) { #if ( (LWIP_VERSION) == ((1U << 24) | (3U << 16) | (2U << 8) | (LWIP_VERSION_RC)) ) /* Wait for a first connection. */ pxNewConnection = netconn_accept(pxHTTPListener); #else while(netconn_accept(pxHTTPListener, &pxNewConnection) != ERR_OK) { vTaskDelay( webSHORT_DELAY ); } #endif if(pxNewConnection != NULL) { /* read the nb of connection, no need to use Mutex */ while( webHTTP_NB_CONN == sCurrentNbHTTPConn ) { vTaskDelay( webSHORT_DELAY ); } // Take the xWEBMutex if there are no active connections. if( 0 == sCurrentNbHTTPConn ) { if( pdFALSE == x_supervisor_SemaphoreTake( xWEBMutex, 0 ) ) { prvweb_SendErrorPage( pxNewConnection, 503, "", "AVR32 UC3 Web server under maintenance." ); // Close the connection. vTaskDelay( 50 ); err_count = 4; while( netconn_close( pxNewConnection ) != ERR_OK ) { if (--err_count == 0) break; vTaskDelay( webSHORT_DELAY ); } err_count = 4; while( netconn_delete( pxNewConnection ) != ERR_OK ) { if (--err_count == 0) break; vTaskDelay( webSHORT_DELAY ); } continue; } } // Find an available spot in the tTaskHandle[] array. // We're sure to find one because sCurrentNbHTTPConn < webHTTP_NB_CONN. TaskIdx = 0; while( NULL != tTaskHandle[TaskIdx] ) TaskIdx++; while( xSemaphoreTake(xMutexNbHTTPConn , portMAX_DELAY ) != pdTRUE ); sCurrentNbHTTPConn++; // Release the mutex. xSemaphoreGive( xMutexNbHTTPConn ); // TRACE_COM2( "nb http conn:%d",sCurrentNbHTTPConn ); if( xTaskCreate( prvweb_ProcessSingleConnection, ( signed portCHAR * )"WCONN", webHTTP_CONNECTION_STACK_SIZE, pxNewConnection, webHTTP_CONNECTION_PRIORITY, &tTaskHandle[TaskIdx] ) != pdPASS) { TRACE_COM2( "xTaskCreate() alloc error" ); /* delete connection */ err_count = 4; while( netconn_close( pxNewConnection ) != ERR_OK ) { if (--err_count == 0) break; vTaskDelay( webSHORT_DELAY ); } err_count = 4; while( netconn_delete( pxNewConnection ) != ERR_OK ) { if (--err_count == 0) break; vTaskDelay( webSHORT_DELAY ); } while( xSemaphoreTake(xMutexNbHTTPConn , portMAX_DELAY ) != pdTRUE ); sCurrentNbHTTPConn--; // Release the xWEBMutex if there are no active connections. if( 0 == sCurrentNbHTTPConn ) { x_supervisor_SemaphoreGive( xWEBMutex ); } // Release the mutex. xSemaphoreGive( xMutexNbHTTPConn ); }// end if task not created }// end if new connection }// end infinite loop }
/* telnet server thread entry */ void telnet_thread(void* parameter) { rt_err_t result,err; rt_uint32_t event; struct netbuf *buf; struct netconn *conn, *newconn; /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); /* Bind connection to well known port number 7. */ netconn_bind(conn, NULL, TELNET_PORT); /* Tell connection to go into listening mode. */ netconn_listen(conn); /* register telnet device */ telnet->device.type = RT_Device_Class_Char; telnet->device.init = telnet_init; telnet->device.open = telnet_open; telnet->device.close = telnet_close; telnet->device.read = telnet_read; telnet->device.write = telnet_write; telnet->device.control = telnet_control; /* no private */ telnet->device.user_data = RT_NULL; /* register telnet device */ rt_device_register(&telnet->device, "telnet", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM); while (1) { rt_kprintf("telnet server waiting for connection\n"); /* Grab new connection. */ err = netconn_accept(conn, &newconn); //if (err == RT_EOK) continue; /* set network rx call back */ newconn->callback = rx_callback; rt_kprintf("new telnet connection, switch console to telnet...\n"); /* Process the new connection. */ /* set console */ rt_console_set_device("telnet"); /* set finsh device */ finsh_set_device("telnet"); /* set init state */ telnet->state = STATE_NORMAL; telnet->echo_mode = finsh_get_echo(); /* disable echo mode */ finsh_set_echo(0); while (1) { /* try to send all data in tx ringbuffer */ telnet_process_tx(telnet, newconn); /* receive network event */ result = rt_event_recv(telnet->nw_event, NW_MASK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_TICK_PER_SECOND, &event); if (result == RT_EOK) { /* get event successfully */ if (event & NW_RX) { /* do a rx procedure */ err= netconn_recv(newconn, &buf); if (buf != RT_NULL) { rt_uint8_t *data; rt_uint16_t length; /* get data */ netbuf_data(buf, (void**)&data, &length); telnet_process_rx(telnet, data, length); /* release buffer */ netbuf_delete(buf); } else { if (newconn->last_err == ERR_CLSD) { /* close connection */ telnet_process_close(telnet, newconn); break; } } } if (event & NW_TX) telnet_process_tx(telnet, newconn); if (event & NW_CLOSED) { /* process close */ telnet_process_close(telnet, newconn); break; } } else if (result == -RT_ETIMEOUT) { if (newconn->state == NETCONN_CLOSE) { telnet_process_close(telnet, newconn); break; } } } } }
/** * Ethernet to PERF task * * @param none * @return none */ void BRIDGE_PERF_Task( void *pvParameters ) { struct ip_addr xIpAddr, xNetMast, xGateway; extern err_t ethernetif_init( struct netif *netif ); SIM_SCGC6 |= SIM_SCGC6_PIT_MASK;//enable PIT /* Parameters are not used - suppress compiler error */ ( void )pvParameters; tcpip_init( NULL, NULL ); /* Create and configure the FEC interface. */ IP4_ADDR( &xIpAddr, configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 ); IP4_ADDR( &xNetMast, configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 ); IP4_ADDR( &xGateway, configGW_ADDR0, configGW_ADDR1, configGW_ADDR2, configGW_ADDR3 ); netif_add( &ENET_if, &xIpAddr, &xNetMast, &xGateway, NULL, ethernetif_init, tcpip_input ); /* make it the default interface */ netif_set_default( &ENET_if ); /* bring it up */ netif_set_up( &ENET_if ); #if BENCHMARK_PROTOCOL //**********************************UDP**********************************// { struct udp_pcb *remote_pcb; remote_pcb = udp_new(); if(!remote_pcb) vTaskDelete(NULL); /*FSL: error during buffer creation*/ #if BENCHMARK_SEND_TO_PC //**********************************TX***********************************// { uint16 i; /*client mode: connect to this server address*/ struct ip_addr ServerIPAddr; struct pbuf *p; /*Fill the array*/ for(i=0;i<sizeof(array);i++) array[i] = i%256;//dummy sequence /*Server IP address to connect*/ IP4_ADDR(&ServerIPAddr,192,168,0,1); printf("Starting UDP TX Client\n"); if( udp_connect(remote_pcb, &ServerIPAddr, (u16_t)BENCHMARK_PORT) != ERR_OK ) vTaskDelete(NULL); /*FSL: error during socket creation*/ if( (p = pbuf_alloc(PBUF_TRANSPORT,0,PBUF_REF)) == NULL ) vTaskDelete(NULL); /*FSL: error during buffer creation*/ p->payload = &array[0]; p->len = p->tot_len = BENCHMARK_PACKET_LENGTH; /*FSL: send selected number of packets*/ for(pkt_counter=0;pkt_counter<BENCHMARK_NUMBER_OF_PACKETS;pkt_counter++) { if( udp_send(remote_pcb, p) != ERR_OK ) break;//error: abort } /*FSL: send ending frames*/ p->len = p->tot_len = 1; /*FSL: send 10 times*/ udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); udp_send(remote_pcb, p); //FSL: remove connection pbuf_free(p); udp_remove(remote_pcb); printf("Leaving UDP Tx Benchmark Test\n"); vTaskDelete(NULL); /*FSL: task no longer needed*/ } #else //**********************************RX***********************************// { printf("Starting UDP RX Server\n"); PIT_TCTRL0 = PIT_TCTRL_TEN_MASK;//enable timer PIT_MCR &= ~PIT_MCR_MDIS_MASK; PIT_LDVAL0 = 0xFFFFFFFF;//max value load_value = PIT_LDVAL0; PIT_TCTRL0 = 0; if( udp_bind(remote_pcb, IP_ADDR_ANY, BENCHMARK_PORT) != ERR_OK ) printf("Wrong bind to UDP port\n"); udp_recv(remote_pcb, udp_receive_fnc, NULL); /*Task no longer needed*/ vTaskDelete(NULL); } #endif } #else //**********************************TCP**********************************// #if BENCHMARK_SEND_TO_PC //**********************************TX***********************************// { struct netconn *conn; uint16 i; /*client mode: connect to this server address*/ struct ip_addr ServerIPAddr; /*Fill the array*/ for(i=0;i<sizeof(array);i++) array[i] = i%256;//dummy sequence /*Server IP address to connect*/ IP4_ADDR(&ServerIPAddr,192,168,0,1); printf("Starting TCP Tx Benchmark Test\n"); /*FSL: wait forever until getting a connection to a valid server*/ do { printf("Trying to connect to server...\n"); /* Create a new TCP PCB. */ conn = netconn_new(NETCONN_TCP); /*client connection*/ if( netconn_connect(conn, &ServerIPAddr, (uint16)BENCHMARK_PORT) != ERR_OK ) { /*close connection for a new SYN process*/ netconn_close(conn); netconn_delete(conn); vTaskDelay(2000); } else break; } while(1);/*infinite loop until getting a valid SYN/SYN-ACK/ACK*/ printf("Connected to server\n"); /*FSL: send selected number of packets*/ pkt_counter = BENCHMARK_NUMBER_OF_PACKETS;//will send in a moment /*FSL: only send when connection is established, otherwise close*/ while( pkt_counter-- ) { netconn_write(conn, &array[0], sizeof(array), NETCONN_NOCOPY); } /*FSL: no more packets*/ netconn_close(conn); netconn_delete(conn); printf("Leaving TCP Tx Benchmark Test\n"); /*FSL: good bye*/ vTaskDelete(NULL); } #else //**********************************RX***********************************// for(;;) { struct netconn *conn, *newconn; struct netbuf *inbuf; uint32 total_length_received = 0; double final_result; /* Create a new TCP PCB. */ conn = netconn_new(NETCONN_TCP); if( netconn_bind(conn,IP_ADDR_ANY,(uint16)BENCHMARK_PORT) != ERR_OK ) { /*close connection for a new SYN process*/ netconn_close(conn); netconn_delete(conn); /*FSL: good bye*/ vTaskDelete(NULL); } printf("Starting TCP Rx Benchmark Test\n"); /* Put the connection into LISTEN state. */ netconn_listen(conn); PIT_TCTRL0 = PIT_TCTRL_TEN_MASK;//enable timer PIT_MCR &= ~PIT_MCR_MDIS_MASK; PIT_LDVAL0 = 0xFFFFFFFF;//max value load_value = PIT_LDVAL0; PIT_TCTRL0 = 0; /* waiting for connection from client */ newconn = netconn_accept(conn); PIT_TCTRL0 = PIT_TCTRL_TEN_MASK;//enable timer for(;;) { if( (inbuf = netconn_recv(newconn)) != NULL ) { /*data is not touch: inbuf->ptr and inbuf->ptr->tot_len*/ total_length_received += inbuf->ptr->tot_len; /*free pbuf memory*/ netbuf_delete(inbuf); } else { current_timer = PIT_CVAL0;//get last time break;/*connection closed*/ } } /*FSL: no more packets*/ netconn_close(newconn); netconn_delete(newconn); netconn_close(conn); netconn_delete(conn); /*calculate*/ time_sfw = (load_value + 1 - current_timer); PIT_TCTRL0 = 0;//stop the timer printf("Benchmark results for TCP Rx:\n\n"); printf("Number of bytes received = %d\n",total_length_received); printf("Number of ticks = %d\n",time_sfw); printf("Frequency of ticks = %d\n",(configCPU_CLOCK_HZ/2)); printf("Calculate benchmark: Bytes/sec\n\n"); final_result = (double)total_length_received * (configCPU_CLOCK_HZ/2) /(double)time_sfw; printf("Measured throughput is %9.2f Bytes/sec\n",final_result); } #endif #endif }
void rpc_server_thread(void *p) { struct netconn *conn, *client_conn; int error; (void)p; struct netbuf *buf; char *data; u16_t len; err_t err; serial_datagram_rcv_handler_t handler; chRegSetThreadName("rpc_service_call"); /* Creates a TCP server */ conn = netconn_new(NETCONN_TCP); if (conn == NULL) { chSysHalt("Cannot create SimpleRPC service call server connection (out of memory)."); } netconn_bind(conn, IP_ADDR_ANY, RPC_SERVER_PORT); netconn_listen(conn); while (1) { serial_datagram_rcv_handler_init(&handler, input_buffer, sizeof input_buffer, serial_datagram_recv_cb, NULL); error = netconn_accept(conn, &client_conn); if (error != ERR_OK) { continue; } /* method_called will be set to true once a callback is fired. */ method_called = false; output_bytes_written = 0; while (!method_called) { /* Tries to receive something from the connection. */ err = netconn_recv(client_conn, &buf); /* If connection closed early */ if (err != ERR_OK) { break; } do { netbuf_data(buf, (void **)&data, &len); err = serial_datagram_receive(&handler, data, len); } while (netbuf_next(buf) >= 0); netbuf_delete(buf); } if (output_bytes_written > 0) { netconn_write(client_conn, output_buffer, output_bytes_written, NETCONN_COPY); } netconn_close(client_conn); netconn_delete(client_conn); } }
/** * TCP echo server thread. */ msg_t tcp_echo_server(void *p) { struct netconn *conn, *newconn; err_t err, accept_err; struct netbuf *buf; void *data; u16_t len; err_t recv_err; LWIP_UNUSED_ARG(p); /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); if (conn!=NULL) { /* Bind connection to well known port number 7. */ err = netconn_bind(conn, NULL, TCP_THREAD_PORT); if (err == ERR_OK) { /* Tell connection to go into listening mode. */ netconn_listen(conn); while (1) { /* Grab new connection. */ accept_err = netconn_accept(conn, &newconn); /* Process the new connection. */ if (accept_err == ERR_OK) { recv_err = netconn_recv(newconn, &buf); while ( recv_err == ERR_OK) { do { netbuf_data(buf, &data, &len); netconn_write(newconn, data, len, NETCONN_COPY); } while (netbuf_next(buf) >= 0); netbuf_delete(buf); recv_err = netconn_recv(newconn, &buf); } /* Close connection and discard connection identifier. */ netconn_close(newconn); netconn_delete(newconn); } } } else { netconn_delete(newconn); // printf(" can not bind TCP netconn"); } } else { // printf("can not create TCP netconn"); } return RDY_OK; }
int main() { sys_sem_t sem; sys_init(); if(sys_sem_new(&sem, 0) != ERR_OK) { LWIP_ASSERT("failed to create semaphore", 0); } tcpip_init(tcpip_init_done, &sem); sys_sem_wait(&sem); sys_sem_free(&sem); /////////////////////////////////////////////////////////////////////////////////////////////////// struct netconn *conn, *newconn; err_t err; /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); netconn_set_noautorecved(conn, 0); tcp_nagle_disable(conn->pcb.tcp); /* Bind connection to well known port number 7. */ netconn_bind(conn, NULL, 80); /* Tell connection to go into listening mode. */ netconn_listen(conn); while (1) { /* Grab new connection. */ err = netconn_accept(conn, &newconn); printf("accepted new connection %p\n", newconn); /* Process the new connection. */ if (err == ERR_OK) { struct netbuf *buf; void *data; u16_t len; u64_t total_rcvd = 0; u64_t eal_tsc_resolution_hz = rte_get_timer_hz(); u64_t end = rte_get_timer_cycles() + eal_tsc_resolution_hz; while ((err = netconn_recv(newconn, &buf)) == ERR_OK) { netbuf_data(buf, &data, &len); if (len > 0) { total_rcvd += len; } if (rte_get_timer_cycles() >= end) { printf("%llu \n", (unsigned long long)total_rcvd); total_rcvd = 0; end = rte_get_timer_cycles() + eal_tsc_resolution_hz; } #if 0 if (err != ERR_OK) { printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(err)); } #endif //} while (netbuf_next(buf) >= 0); netbuf_delete(buf); } /*printf("Got EOF, looping\n");*/ /* Close connection and discard connection identifier. */ netconn_close(newconn); netconn_delete(newconn); } } while (1); }
/*-----------------------------------------------------------------------------------*/ static void tcpecho_thread(void *arg) { struct netconn *conn, *newconn; err_t err; LWIP_UNUSED_ARG(arg); /* Create a new connection identifier. */ conn = netconn_new(NETCONN_TCP); if (conn!=NULL) { /* Bind connection to well known port number 7. */ err = netconn_bind(conn, NULL, 7); if (err == ERR_OK) { /* Tell connection to go into listening mode. */ netconn_listen(conn); while (1) { /* Grab new connection. */ err_t xErr = netconn_accept(conn, &newconn); /* Process the new connection. */ if (xErr== ERR_OK) { struct netbuf *buf; void *data; u16_t len; while ((xErr = netconn_recv(newconn, &buf)) == ERR_OK) { do { netbuf_data(buf, &data, &len); netconn_write(newconn, data, len, NETCONN_COPY); } while (netbuf_next(buf) >= 0); netbuf_delete(buf); } /* Close connection and discard connection identifier. */ netconn_close(newconn); netconn_delete(newconn); } } } else { printf(" can not bind TCP netconn"); } } else { printf("can not create TCP netconn"); } }