void csp_server(void *p) { (void) p; /* Create socket without any socket options */ csp_socket_t *sock = csp_socket(CSP_SO_NONE); /* Bind all ports to socket */ csp_bind(sock, CSP_ANY); /* Create 10 connections backlog queue */ csp_listen(sock, 10); /* Pointer to current connection and packet */ csp_conn_t *conn; csp_packet_t *packet; /* Process incoming connections */ while (1) { /* Wait for connection, 10000 ms timeout */ if ((conn = csp_accept(sock, 10000)) == NULL) continue; /* Read packets. Timout is 100 ms */ while ((packet = csp_read(conn, 100)) != NULL) { switch (csp_conn_dport(conn)) { case MY_PORT: /* Process packet here */ blink(K_LED_GREEN); csp_buffer_free(packet); break; default: /* Let the service handler reply pings, buffer use, etc. */ #ifdef TARGET_LIKE_MSP430 blink(K_LED_GREEN); blink(K_LED_GREEN); #else blink(K_LED_BLUE); #endif csp_service_handler(conn, packet); break; } } /* Close current connection, and handle next */ csp_close(conn); } }
unsigned WINAPI task_server(void * parameters) { /* Create socket without any socket options */ csp_socket_t *sock = csp_socket(CSP_SO_NONE); /* Bind all ports to socket */ csp_bind(sock, CSP_ANY); /* Create 10 connections backlog queue */ csp_listen(sock, 10); /* Pointer to current connection and packet */ csp_conn_t *conn; csp_packet_t *packet; /* Process incoming connections */ while (1) { /* Wait for connection, 10000 ms timeout */ if ((conn = csp_accept(sock, 10000)) == NULL) continue; /* Read packets. Timout is 100 ms */ while ((packet = csp_read(conn, 100)) != NULL) { switch (csp_conn_dport(conn)) { case MY_PORT: /* Process packet here */ printf("Packet received on MY_PORT: %s\r\n", (char *) packet->data); csp_buffer_free(packet); default: /* Let the service handler reply pings, buffer use, etc. */ csp_service_handler(conn, packet); break; } } /* Close current connection, and handle next */ csp_close(conn); } return 0; }