int main (int argc, char **argv) { if (argc < 3) { logging("usage: " + std::string(argv[0]) + " <IPaddress> <Port>"); return 1; } struct sockaddr_in addr; int sockfd = create_udp_client(&addr, argv[1], std::atoi(argv[2])); mkdir("Download", 0777); mkdir("Upload", 0777); int status = client_echo(sockfd, addr); return status; }
static int rpc_ping_proto(const char *host, unsigned long nfs_version, const char *proto, long seconds, long micros) { struct conn_info info; CLIENT *client; enum clnt_stat stat; struct protoent *prot; prot = getprotobyname(proto); if (!prot) return 1; info.host = host; info.program = NFS_PROGRAM; info.version = nfs_version; info.proto = prot; info.send_sz = 0; info.recv_sz = 0; info.timeout.tv_sec = seconds; info.timeout.tv_usec = micros; info.port = portmap_getport(&info); if (!info.port) return 0; if (prot->p_proto == IPPROTO_UDP) { info.send_sz = UDPMSGSIZE; info.recv_sz = UDPMSGSIZE; client = create_udp_client(&info); } else client = create_tcp_client(&info); if (!client) return 0; clnt_control(client, CLSET_TIMEOUT, (char *) &info.timeout); clnt_control(client, CLSET_RETRY_TIMEOUT, (char *) &info.timeout); stat = clnt_call(client, NFSPROC_NULL, (xdrproc_t) xdr_void, 0, (xdrproc_t) xdr_void, 0, info.timeout); clnt_destroy(client); if (stat != RPC_SUCCESS) return 0; return 1; }
apr_socket_t * create_mcast_client(apr_pool_t *context, char *mcast_ip, apr_port_t port, int ttl, const char *interface, char *bind_address, int bind_hostname) { apr_socket_t *socket = create_udp_client(context, mcast_ip, port, interface, bind_address, bind_hostname); if(!socket) { return NULL; } apr_mcast_hops(socket, ttl); mcast_emit_on_if( context, socket, mcast_ip, port, interface); return socket; }
static unsigned short portmap_getport(struct conn_info *info) { struct conn_info pmap_info; unsigned short port = 0; CLIENT *client; enum clnt_stat stat; struct pmap parms; pmap_info.host = info->host; pmap_info.port = PMAPPORT; pmap_info.program = PMAPPROG; pmap_info.version = PMAPVERS; pmap_info.proto = info->proto; pmap_info.send_sz = RPCSMALLMSGSIZE; pmap_info.recv_sz = RPCSMALLMSGSIZE; pmap_info.timeout.tv_sec = PMAP_TOUT_UDP; pmap_info.timeout.tv_usec = 0; if (info->proto->p_proto == IPPROTO_TCP) { pmap_info.timeout.tv_sec = PMAP_TOUT_TCP; client = create_tcp_client(&pmap_info); } else client = create_udp_client(&pmap_info); if (!client) return 0; parms.pm_prog = info->program; parms.pm_vers = info->version; parms.pm_prot = info->proto->p_proto; parms.pm_port = 0; stat = clnt_call(client, PMAPPROC_GETPORT, (xdrproc_t) xdr_pmap, (caddr_t) &parms, (xdrproc_t) xdr_u_short, (caddr_t) &port, pmap_info.timeout); clnt_destroy(client); if (stat != RPC_SUCCESS) return 0; return port; }
static void udp_conn(void *pvParameters) { ESP_LOGI(TAG, "task udp_conn start... \n\r"); //等待是否已经成功连接到路由器的标志位 xEventGroupWaitBits(udp_event_group, WIFI_CONNECTED_BIT, false, true, portMAX_DELAY); ESP_LOGI(TAG, "esp32 is ready !!! create udp client or connect servece after 5s... \n\r"); vTaskDelay(5000 / portTICK_RATE_MS); //创建客户端并且检查是否创建成功 #if Server_Station_Option ESP_LOGI(TAG, "Now Let us create udp server ... \n\r"); if (create_udp_server() == ESP_FAIL) { ESP_LOGI(TAG, " server create socket error , stop !!! \n\r"); vTaskDelete(NULL); } else { ESP_LOGI(TAG, "server create socket Succeed !!! \n\r"); } #else ESP_LOGI(TAG, "Now Let us create udp client ... \n\r"); if (create_udp_client() == ESP_FAIL) { ESP_LOGI(TAG, "client create socket error , stop !!! \n\r"); vTaskDelete(NULL); } else { ESP_LOGI(TAG, "client create socket Succeed !!! \n\r"); } #endif //创建一个发送和接收数据的任务 TaskHandle_t tx_rx_task; xTaskCreate(&send_recv_data, "send_recv_data", 4096, NULL, 4, &tx_rx_task); //等待 UDP连接成功标志位 xEventGroupWaitBits(udp_event_group, UDP_CONNCETED_SUCCESS, false, true, portMAX_DELAY); int bps; char sendBuff[1024] = "hello xuhong,I am from Esp32 ..."; while (1) { total_data = 0; vTaskDelay(3000 / portTICK_RATE_MS); //时隔三秒发送一次数据 #if !Server_Station_Option send_Buff_with_UDP(sendBuff, 1024); #endif bps = total_data / 3; if (total_data <= 0) { int err_ret = check_connected_socket(); if (err_ret == -1) { ESP_LOGW(TAG, "udp send & recv stop !!! will close socket ... \n\r"); close_socket(); break; } } ESP_LOGI(TAG, "udp recv %d byte per sec! total pack: %d \n\r", bps, success_pack); } vTaskDelete(tx_rx_task); vTaskDelete(NULL); }
Ganglia_udp_send_channels Ganglia_udp_send_channels_create( Ganglia_pool p, Ganglia_gmond_config config ) { apr_array_header_t *send_channels = NULL; cfg_t *cfg=(cfg_t *)config; int i, num_udp_send_channels = cfg_size( cfg, "udp_send_channel"); apr_pool_t *context = (apr_pool_t*)p; /* Return null if there are no send channels specified */ if(num_udp_send_channels <= 0) return (Ganglia_udp_send_channels)send_channels; /* Create my UDP send array */ send_channels = apr_array_make( context, num_udp_send_channels, sizeof(apr_socket_t *)); for(i = 0; i< num_udp_send_channels; i++) { cfg_t *udp_send_channel; char *mcast_join, *mcast_if, *host; int port, ttl, bind_hostname; apr_socket_t *socket = NULL; apr_pool_t *pool = NULL; char *bind_address; udp_send_channel = cfg_getnsec( cfg, "udp_send_channel", i); host = cfg_getstr( udp_send_channel, "host" ); mcast_join = cfg_getstr( udp_send_channel, "mcast_join" ); mcast_if = cfg_getstr( udp_send_channel, "mcast_if" ); port = cfg_getint( udp_send_channel, "port"); ttl = cfg_getint( udp_send_channel, "ttl"); bind_address = cfg_getstr( udp_send_channel, "bind" ); bind_hostname = cfg_getbool( udp_send_channel, "bind_hostname"); debug_msg("udp_send_channel mcast_join=%s mcast_if=%s host=%s port=%d\n", mcast_join? mcast_join:"NULL", mcast_if? mcast_if:"NULL", host? host:"NULL", port); if(bind_address != NULL && bind_hostname == cfg_true) { err_msg("udp_send_channel: bind and bind_hostname are mutually exclusive, both parameters can't be specified for the same udp_send_channel\n"); exit(1); } /* Create a subpool */ apr_pool_create(&pool, context); /* Join the specified multicast channel */ if( mcast_join ) { /* We'll be listening on a multicast channel */ socket = create_mcast_client(pool, mcast_join, port, ttl, mcast_if, bind_address, bind_hostname); if(!socket) { err_msg("Unable to join multicast channel %s:%d. Exiting\n", mcast_join, port); exit(1); } } else { /* Create a UDP socket */ socket = create_udp_client( pool, host, port, mcast_if, bind_address, bind_hostname ); if(!socket) { err_msg("Unable to create UDP client for %s:%d. Exiting.\n", host? host: "NULL", port); exit(1); } } /* Add the socket to the array */ *(apr_socket_t **)apr_array_push(send_channels) = socket; } return (Ganglia_udp_send_channels)send_channels; }