Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
Network
ftpd服务和UDP通讯服务 同时进行 出现问题
发布于 2018-04-17 13:09:50 浏览:3118
订阅该版
您好: 使用lpc4088开发板例程,在 ftpd服务示例程序中 增加 upd通讯任务(1收/1发,2个线程,手册示例),出现错误(ftpd和udp发送都错误)。 ![a.jpg](/uploads/201804/17/120053bnzi7nncu94qo2kg.jpg) application.c ```/* 此demo用于演示ftp服务器 */ #include
#include
#ifdef RT_USING_FINSH #include
#include
#endif #include "components.h" extern void lpc_emac_hw_init(void); void rt_init_thread_entry(void* parameter) { #ifdef RT_USING_LWIP /* initialize eth interface */ lpc_emac_hw_init(); #endif #ifdef RT_USING_COMPONENTS_INIT /* initialization RT-Thread Components */ rt_components_init(); #endif { extern rt_err_t mci_hw_init(const char *device_name); mci_hw_init("sd0"); } /* Filesystem Initialization */ #ifdef RT_USING_DFS { #ifdef RT_USING_DFS_ELMFAT /* mount sd card fat partition 1 as root directory */ if (dfs_mount("sd0", "/", "elm", 0, 0) == 0) { rt_kprintf("File System initialized!
"); #if (RT_DFS_ELM_USE_LFN != 0) && (defined RT_DFS_ELM_CODE_PAGE_FILE) { extern void ff_convert_init(void); ff_convert_init(); } #endif } else rt_kprintf("File System initialzation failed!
"); #endif } #endif /* do some thing here. */ #if defined(RT_USING_DFS) && defined(RT_USING_LWIP) { extern void ftpd_start(void); /* start ftp server */ rt_kprintf("ftp server begin...
"); ftpd_start(); rt_kprintf("ftp server started!!
"); } #endif { // UDPTEST extern void udptest_start(); udptest_start(); } } int rt_application_init() { rt_thread_t tid; tid = rt_thread_create("init", rt_init_thread_entry, RT_NULL, 2048, RT_THREAD_PRIORITY_MAX/3, 20);// if (tid != RT_NULL) rt_thread_startup(tid); return 0; } ``` ftpd.c ```#include
#include
#include
#include
#include
#include
#define FTP_PORT 21 #define FTP_SRV_ROOT "/" #define FTP_MAX_CONNECTION 5 // 2 #define FTP_USER "rtt" #define FTP_PASSWORD "demo" #define FTP_WELCOME_MSG "220 welcome on RT-Thread FTP server.
" #define FTP_BUFFER_SIZE 1024 #define INET_ADDRSTRLEN 16 struct ftp_session { rt_bool_t is_anonymous; int sockfd; struct sockaddr_in remote; struct sockaddr_in server; char serveraddr[INET_ADDRSTRLEN]; /* pasv data */ int pasv_listen_sockfd; char pasv_active; int pasv_sockfd; unsigned short pasv_port; size_t offset; /* current directory */ char currentdir[256]; struct ftp_session* next; }; static struct ftp_session* session_list = NULL; int ftp_process_request(struct ftp_session* session, char * buf); int ftp_get_filesize(char *filename); struct ftp_session* ftp_new_session() { struct ftp_session* session; session = (struct ftp_session*)rt_malloc(sizeof(struct ftp_session)); rt_memset((void *)session, 0, sizeof(struct ftp_session)); session->next = session_list; session_list = session; return session; } void ftp_close_session(struct ftp_session* session) { struct ftp_session* list; if (session_list == session) { session_list = session_list->next; session->next = NULL; } else { list = session_list; while (list->next != session) list = list->next; list->next = session->next; session->next = NULL; } rt_free(session); } static int open_data_connection(struct ftp_session* session) { socklen_t len = sizeof(struct sockaddr); struct sockaddr_in sin; #if 0 /* Previous PORT command from client */ if (ctrl->data_address[0]) { ctrl->data_sd = socket(AF_INET, SOCK_STREAM, 0); if (-1 == ctrl->data_sd) { printf("Failed creating data socket"); return -1; } memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(ctrl->data_port); inet_aton(ctrl->data_address, &(sin.sin_addr)); if (connect(ctrl->data_sd, (struct sockaddr *)&sin, len) == -1) { printf("Failed connecting data socket to client"); close(ctrl->data_sd); ctrl->data_sd = -1; return -1; } DBG("Connected successfully to client's previously requested address:PORT %s:%d", ctrl->data_address, ctrl->data_port); return 0; } #endif /* Previous PASV command, accept connect from client */ if (session->pasv_listen_sockfd > 0) { char client_ip[100]; session->pasv_sockfd = accept(session->pasv_listen_sockfd, (struct sockaddr *)&sin, &len); if (-1 == session->pasv_sockfd) { printf("Failed accepting connection from client"); return -1; } len = sizeof(struct sockaddr); if (-1 == getpeername(session->pasv_sockfd, (struct sockaddr *)&sin, &len)) { printf("Cannot determine client address"); closesocket(session->pasv_sockfd); session->pasv_sockfd = -1; return -1; } printf("Client PASV data connection from %s
", inet_ntoa(sin.sin_addr)); } return 0; } static void close_data_connection(struct ftp_session* session) { /* PASV server listening socket */ if (session->pasv_listen_sockfd > 0) { closesocket(session->pasv_listen_sockfd); session->pasv_listen_sockfd = -1; } /* PASV client socket */ if (session->pasv_sockfd > 0) { closesocket(session->pasv_sockfd); session->pasv_sockfd = -1; } #if 0 /* PORT */ if (ctrl->data_address[0]) { ctrl->data_address[0] = 0; ctrl->data_port = 0; } #endif } int ftp_get_filesize(char * filename) { int pos; int end; int fd; fd = open(filename, O_RDONLY, 0); if (fd < 0) return -1; pos = lseek(fd, 0, SEEK_CUR); end = lseek(fd, 0, SEEK_END); lseek (fd, pos, SEEK_SET); close(fd); return end; } rt_bool_t is_absolute_path(char* path) { #ifdef _WIN32 if (path[0] == '\\' || (path[1] == ':' && path[2] == '\\')) return RT_TRUE; #else if (path[0] == '/') return RT_TRUE; #endif return RT_FALSE; } int build_full_path(struct ftp_session* session, char* path, char* new_path, size_t size) { if (is_absolute_path(path) == RT_TRUE) strcpy(new_path, path); else { rt_sprintf(new_path, "%s/%s", session->currentdir, path); } return 0; } void ftpd_thread_entry(void* parameter) { int numbytes; int sockfd, maxfdp1; struct sockaddr_in local; fd_set readfds, tmpfds; struct ftp_session* session; rt_uint32_t addr_len = sizeof(struct sockaddr); char * buffer = (char *) rt_malloc(FTP_BUFFER_SIZE); local.sin_port=htons(FTP_PORT); local.sin_family=PF_INET; local.sin_addr.s_addr=INADDR_ANY; FD_ZERO(&readfds); FD_ZERO(&tmpfds); sockfd=socket(AF_INET, SOCK_STREAM, 0); if(sockfd < 0) { rt_kprintf("create socket failed
"); return ; } bind(sockfd, (struct sockaddr *)&local, addr_len); listen(sockfd, FTP_MAX_CONNECTION); FD_SET(sockfd, &readfds); for(;;) { /* get maximum fd */ maxfdp1 = sockfd + 1; session = session_list; while (session != RT_NULL) { if (maxfdp1 < session->sockfd + 1) maxfdp1 = session->sockfd + 1; FD_SET(session->sockfd, &readfds); session = session->next; } tmpfds=readfds; if (select(maxfdp1, &tmpfds, 0, 0, 0) == 0) continue; if(FD_ISSET(sockfd, &tmpfds)) { int com_socket; struct sockaddr_in remote; com_socket = accept(sockfd, (struct sockaddr*)&remote, &addr_len); if(com_socket == -1) { rt_kprintf("Error on accept()
Continuing...
"); continue; } else { rt_kprintf("Got connection from %s
", inet_ntoa(remote.sin_addr)); send(com_socket, FTP_WELCOME_MSG, strlen(FTP_WELCOME_MSG), 0); FD_SET(com_socket, &readfds); /* new session */ session = ftp_new_session(); if (session != NULL) { if (-1 == getsockname(com_socket, (struct sockaddr *)&session->server, &addr_len)) { printf("Cannot determine our address, need it if client should connect to us
"); } ipaddr_ntoa_r((ip_addr_t*)&(session->server.sin_addr.s_addr), session->serveraddr, sizeof(session->serveraddr)); strcpy(session->currentdir, FTP_SRV_ROOT); session->sockfd = com_socket; session->remote = remote; } } } { struct ftp_session* next; session = session_list; while (session != NULL) { next = session->next; if (FD_ISSET(session->sockfd, &tmpfds)) { numbytes=recv(session->sockfd, buffer, FTP_BUFFER_SIZE, 0); if(numbytes==0 || numbytes==-1) { rt_kprintf("Client %s disconnected
", inet_ntoa(session->remote.sin_addr)); FD_CLR(session->sockfd, &readfds); closesocket(session->sockfd); session->sockfd = -1; ftp_close_session(session); } else { buffer[numbytes]=0; if(ftp_process_request(session, buffer)==-1) { rt_kprintf("Client %s disconnected
", inet_ntoa(session->remote.sin_addr)); closesocket(session->sockfd); session->sockfd = -1; ftp_close_session(session); } } } session = next; } } } // rt_free(buffer); } int do_list(char* directory, int sockfd) { DIR* dirp; struct dirent* entry; char line_buffer[256], line_length; #ifdef _WIN32 struct _stat s; #else struct stat s; #endif dirp = opendir(directory); if (dirp == NULL) { line_length = rt_sprintf(line_buffer, "500 Internal Error
"); send(sockfd, line_buffer, line_length, 0); return -1; } while (1) { entry = readdir(dirp); if (entry == NULL) break; rt_sprintf(line_buffer, "%s/%s", directory, entry->d_name); #ifdef _WIN32 if (_stat(line_buffer, &s) ==0) #else if (stat(line_buffer, &s) == 0) #endif { if (s.st_mode & S_IFDIR) line_length = rt_sprintf(line_buffer, "drw-r--r-- 1 admin admin %d Jan 1 2000 %s
", 0, entry->d_name); else line_length = rt_sprintf(line_buffer, "-rw-r--r-- 1 admin admin %d Jan 1 2000 %s
", s.st_size, entry->d_name); send(sockfd, line_buffer, line_length, 0); } else { rt_kprintf("Get directory entry error
"); break; } } closedir(dirp); return 0; } int do_simple_list(char* directory, int sockfd) { DIR* dirp; struct dirent* entry; char line_buffer[256], line_length; dirp = opendir(directory); if (dirp == NULL) { line_length = rt_sprintf(line_buffer, "500 Internal Error
"); send(sockfd, line_buffer, line_length, 0); return -1; } while (1) { entry = readdir(dirp); if (entry == NULL) break; line_length = rt_sprintf(line_buffer, "%s
", entry->d_name); send(sockfd, line_buffer, line_length, 0); } closedir(dirp); return 0; } int str_begin_with(char* src, char* match) { while (*match) { /* check source */ if (*src == 0) return -1; if (*match != *src) return -1; match ++; src ++; } return 0; } int ftp_process_request(struct ftp_session* session, char *buf) { int fd; struct timeval tv; fd_set readfds; char filename[256]; int numbytes; char *sbuf; char *parameter_ptr, *ptr; rt_uint32_t addr_len = sizeof(struct sockaddr_in); struct sockaddr_in local, pasvremote; sbuf =(char *)rt_malloc(FTP_BUFFER_SIZE); tv.tv_sec=3, tv.tv_usec=0; local.sin_family=PF_INET; local.sin_addr.s_addr=INADDR_ANY; /* remove */ ptr = buf; while (*ptr) { if (*ptr == ' ' || *ptr == '
') *ptr = 0; ptr ++; } /* get request parameter */ parameter_ptr = strchr(buf, ' '); if (parameter_ptr != NULL) parameter_ptr ++; // debug: rt_kprintf("%s requested: \"%s\"
", inet_ntoa(session->remote.sin_addr), buf); // //----------------------- if(str_begin_with(buf, "USER")==0) { rt_kprintf("%s sent login \"%s\"
", inet_ntoa(session->remote.sin_addr), parameter_ptr); // login correct if(strcmp(parameter_ptr, "anonymous") == 0) { session->is_anonymous = RT_TRUE; rt_sprintf(sbuf, "331 Anonymous login OK send e-mail address for password.
", parameter_ptr); send(session->sockfd, sbuf, strlen(sbuf), 0); } else if (strcmp(parameter_ptr, FTP_USER) == 0) { session->is_anonymous = RT_FALSE; rt_sprintf(sbuf, "331 Password required for %s
", parameter_ptr); send(session->sockfd, sbuf, strlen(sbuf), 0); } else { // incorrect login rt_sprintf(sbuf, "530 Login incorrect. Bye.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); rt_free(sbuf); return -1; } return 0; } else if(str_begin_with(buf, "PASS")==0) { rt_kprintf("%s sent password \"%s\"
", inet_ntoa(session->remote.sin_addr), parameter_ptr); if (strcmp(parameter_ptr, FTP_PASSWORD)==0 || session->is_anonymous == RT_TRUE) { // password correct rt_sprintf(sbuf, "230 User logged in.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); rt_free(sbuf); return 0; } // incorrect password rt_sprintf(sbuf, "530 Login or Password incorrect. Bye!
"); send(session->sockfd, sbuf, strlen(sbuf), 0); rt_free(sbuf); return -1; } else if(str_begin_with(buf, "LIST")==0 ) { memset(sbuf,0,FTP_BUFFER_SIZE); open_data_connection(session); rt_sprintf(sbuf, "150 Opening Binary mode connection for file list.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); do_list(session->currentdir, session->pasv_sockfd); close_data_connection(session); session->pasv_active = 0; rt_sprintf(sbuf, "226 Transfert Complete.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); } else if(str_begin_with(buf, "NLST")==0 ) { memset(sbuf, 0, FTP_BUFFER_SIZE); rt_sprintf(sbuf, "150 Opening Binary mode connection for file list.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); open_data_connection(session); do_simple_list(session->currentdir, session->pasv_sockfd); close_data_connection(session); session->pasv_active = 0; rt_sprintf(sbuf, "226 Transfert Complete.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); } else if(str_begin_with(buf, "PWD")==0 || str_begin_with(buf, "XPWD")==0) { rt_sprintf(sbuf, "257 \"%s\" is current directory.
", session->currentdir); send(session->sockfd, sbuf, strlen(sbuf), 0); } else if(str_begin_with(buf, "TYPE")==0) { // Ignore it if(strcmp(parameter_ptr, "I")==0) { rt_sprintf(sbuf, "200 Type set to binary.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); } else { rt_sprintf(sbuf, "200 Type set to ascii.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); } } else if(str_begin_with(buf, "PASV")==0) { int dig1, dig2; //int sockfd; int optval=1; int port; struct sockaddr_in data; socklen_t len = sizeof(struct sockaddr); char *msg, *p; if (session->pasv_sockfd > 0) { closesocket(session->pasv_sockfd); session->pasv_sockfd = -1; } if (session->pasv_listen_sockfd > 0) closesocket(session->pasv_listen_sockfd); session->pasv_port = 10000; session->pasv_active = 1; local.sin_port=htons(session->pasv_port); local.sin_addr.s_addr=INADDR_ANY; dig1 = (int)(session->pasv_port/256); dig2 = session->pasv_port % 256; FD_ZERO(&readfds); if((session->pasv_listen_sockfd=socket(PF_INET, SOCK_STREAM, 0))==-1) { rt_sprintf(sbuf, "425 Can't open data connection0.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); goto err1; } if(setsockopt(session->pasv_listen_sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))==-1) { rt_sprintf(sbuf, "425 Can't open data connection1.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); goto err1; } if(bind(session->pasv_listen_sockfd, (struct sockaddr *)&local, addr_len)==-1) { rt_sprintf(sbuf, "425 Can't open data connection2.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); goto err1; } if(listen(session->pasv_listen_sockfd, 1)==-1) { rt_sprintf(sbuf, "425 Can't open data connection3.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); goto err1; } if (-1 == getsockname(session->pasv_listen_sockfd, (struct sockaddr *)&data, &len)) { rt_kprintf("Cannot determine our address, need it if client should connect to us
"); goto err1; } port = ntohs(data.sin_port); rt_kprintf("Port %d
", port); /* Convert server IP address and port to comma separated list */ msg = rt_strdup(session->serveraddr); if (!msg) { rt_sprintf(sbuf, "426 Internal server error.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); goto err1; } p = msg; while ((p = strchr(p, '.'))) *p++ = ','; rt_kprintf("Listening %d seconds @ port %d
", tv.tv_sec, session->pasv_port); rt_sprintf(sbuf, "227 Entering passive mode (%s,%d,%d)
", msg, port / 256, port % 256); send(session->sockfd, sbuf, strlen(sbuf), 0); rt_free(sbuf); rt_free(msg); return 0; #if 0 FD_SET(sockfd, &readfds); select(0, &readfds, 0, 0, &tv); if(FD_ISSET(sockfd, &readfds)) { if((session->pasv_sockfd = accept(sockfd, (struct sockaddr*)&pasvremote, &addr_len))==-1) { rt_sprintf(sbuf, "425 Can't open data connection4.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); goto err1; } else { rt_kprintf("Got Data(PASV) connection from %s
", inet_ntoa(pasvremote.sin_addr)); session->pasv_active = 1; closesocket(sockfd); } } else { err1: closesocket(session->pasv_sockfd); session->pasv_active = 0; rt_free(sbuf); return 0; } #endif err1: close_data_connection(session); session->pasv_active = 0; rt_free(sbuf); rt_free(msg); return 0; } else if (str_begin_with(buf, "RETR")==0) { int file_size; open_data_connection(session); strcpy(filename, buf + 5); build_full_path(session, parameter_ptr, filename, 256); file_size = ftp_get_filesize(filename); if (file_size == -1) { rt_sprintf(sbuf, "550 \"%s\" : not a regular file
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); session->offset=0; close_data_connection(session); rt_free(sbuf); return 0; } fd = open(filename, O_RDONLY, 0); if (fd < 0) { close_data_connection(session); rt_free(sbuf); return 0; } if(session->offset>0 && session->offset < file_size) { lseek(fd, session->offset, SEEK_SET); rt_sprintf(sbuf, "150 Opening binary mode data connection for partial \"%s\" (%d/%d bytes).
", filename, file_size - session->offset, file_size); } else { rt_sprintf(sbuf, "150 Opening binary mode data connection for \"%s\" (%d bytes).
", filename, file_size); } send(session->sockfd, sbuf, strlen(sbuf), 0); while((numbytes = read(fd, sbuf, FTP_BUFFER_SIZE))>0) { send(session->pasv_sockfd, sbuf, numbytes, 0); } rt_sprintf(sbuf, "226 Finished.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); close(fd); close_data_connection(session); } else if (str_begin_with(buf, "STOR")==0) { open_data_connection(session); if(session->is_anonymous == RT_TRUE) { rt_sprintf(sbuf, "550 Permission denied.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); close_data_connection(session); rt_free(sbuf); return 0; } build_full_path(session, parameter_ptr, filename, 256); fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0); if(fd < 0) { rt_sprintf(sbuf, "550 Cannot open \"%s\" for writing.
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); close_data_connection(session); rt_free(sbuf); return 0; } rt_sprintf(sbuf, "150 Opening binary mode data connection for \"%s\".
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); FD_ZERO(&readfds); FD_SET(session->pasv_sockfd, &readfds); rt_kprintf("Waiting %d seconds for data...
", tv.tv_sec); while(select(session->pasv_sockfd+1, &readfds, 0, 0, &tv)>0 ) { if((numbytes=recv(session->pasv_sockfd, sbuf, FTP_BUFFER_SIZE, 0))>0) { write(fd, sbuf, numbytes); } else if(numbytes==0) { close(fd); close_data_connection(session); rt_sprintf(sbuf, "226 Finished.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); break; } else if(numbytes==-1) { close(fd); close_data_connection(session); rt_free(sbuf); return -1; } } close_data_connection(session); } else if(str_begin_with(buf, "SIZE")==0) { int file_size; build_full_path(session, parameter_ptr, filename, 256); file_size = ftp_get_filesize(filename); if( file_size == -1) { rt_sprintf(sbuf, "550 \"%s\" : not a regular file
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); } else { rt_sprintf(sbuf, "213 %d
", file_size); send(session->sockfd, sbuf, strlen(sbuf), 0); } } else if(str_begin_with(buf, "MDTM")==0) { rt_sprintf(sbuf, "550 \"/\" : not a regular file
"); send(session->sockfd, sbuf, strlen(sbuf), 0); } else if(str_begin_with(buf, "SYST")==0) { rt_sprintf(sbuf, "215 %s
", "RT-Thread RTOS"); send(session->sockfd, sbuf, strlen(sbuf), 0); } else if(str_begin_with(buf, "CWD")==0) { build_full_path(session, parameter_ptr, filename, 256); rt_sprintf(sbuf, "250 Changed to directory \"%s\"
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); strcpy(session->currentdir, filename); rt_kprintf("Changed to directory %s", filename); } else if(str_begin_with(buf, "CDUP")==0) { rt_sprintf(filename, "%s/%s", session->currentdir, ".."); rt_sprintf(sbuf, "250 Changed to directory \"%s\"
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); strcpy(session->currentdir, filename); rt_kprintf("Changed to directory %s", filename); } else if(str_begin_with(buf, "PORT")==0) { int i; int portcom[6]; char tmpip[100]; i=0; portcom[i++]=atoi(strtok(parameter_ptr, ".,;()")); for(;i<6;i++) portcom*=atoi(strtok(0, ".,;()")); rt_sprintf(tmpip, "%d.%d.%d.%d", portcom[0], portcom[1], portcom[2], portcom[3]); FD_ZERO(&readfds); if((session->pasv_sockfd=socket(AF_INET, SOCK_STREAM, 0))==-1) { rt_sprintf(sbuf, "425 Can't open data connection.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); closesocket(session->pasv_sockfd); session->pasv_active = 0; rt_free(sbuf); return 0; } pasvremote.sin_addr.s_addr=inet_addr(tmpip); pasvremote.sin_port=htons(portcom[4] * 256 + portcom[5]); pasvremote.sin_family=PF_INET; if(connect(session->pasv_sockfd, (struct sockaddr *)&pasvremote, addr_len)==-1) { // is it only local address?try using gloal ip addr pasvremote.sin_addr=session->remote.sin_addr; if(connect(session->pasv_sockfd, (struct sockaddr *)&pasvremote, addr_len)==-1) { rt_sprintf(sbuf, "425 Can't open data connection.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); closesocket(session->pasv_sockfd); rt_free(sbuf); return 0; } } session->pasv_active=1; session->pasv_port = portcom[4] * 256 + portcom[5]; rt_kprintf("Connected to Data(PORT) %s @ %d
", tmpip, portcom[4] * 256 + portcom[5]); rt_sprintf(sbuf, "200 Port Command Successful.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); } else if(str_begin_with(buf, "REST")==0) { if(atoi(parameter_ptr)>=0) { session->offset=atoi(parameter_ptr); rt_sprintf(sbuf, "350 Send RETR or STOR to start transfert.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); } } else if(str_begin_with(buf, "MKD")==0) { if (session->is_anonymous == RT_TRUE) { rt_sprintf(sbuf, "530 Permission denied.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); rt_free(sbuf); return 0; } build_full_path(session, parameter_ptr, filename, 256); if(mkdir(filename, 0) == -1) { rt_sprintf(sbuf, "550 File \"%s\" exists.
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); } else { rt_sprintf(sbuf, "257 directory \"%s\" successfully created.
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); } } else if(str_begin_with(buf, "DELE")==0) { if (session->is_anonymous == RT_TRUE) { rt_sprintf(sbuf, "530 Permission denied.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); rt_free(sbuf); return 0; } build_full_path(session, parameter_ptr, filename, 256); if(unlink(filename)==0) rt_sprintf(sbuf, "250 Successfully deleted file \"%s\".
", filename); else { rt_sprintf(sbuf, "550 Not such file or directory: %s.
", filename); } send(session->sockfd, sbuf, strlen(sbuf), 0); } else if(str_begin_with(buf, "RMD")==0) { if (session->is_anonymous == RT_TRUE) { rt_sprintf(sbuf, "530 Permission denied.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); rt_free(sbuf); return 0; } build_full_path(session, parameter_ptr, filename, 256); if(unlink(filename) == -1) { rt_sprintf(sbuf, "550 Directory \"%s\" doesn't exist.
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); } else { rt_sprintf(sbuf, "257 directory \"%s\" successfully deleted.
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); } } else if(str_begin_with(buf, "RNFR")==0) { if (session->is_anonymous == RT_TRUE) { rt_sprintf(sbuf, "530 Permission denied.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); rt_free(sbuf); return 0; } build_full_path(session, parameter_ptr, filename, 256); rt_sprintf(sbuf, "350 Successfully rececive old file \"%s\".
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); } else if(str_begin_with(buf, "RNTO")==0) { char new_filename[256]; if (session->is_anonymous == RT_TRUE) { rt_sprintf(sbuf, "530 Permission denied.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); rt_free(sbuf); return 0; } build_full_path(session, parameter_ptr, new_filename, 256); if(rename(filename, new_filename) == -1) { rt_sprintf(sbuf, "553 rename file \"%s\" error.
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); } else { rt_sprintf(sbuf, "250 Successfully rename to new file \"%s\".
", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); } } else if((str_begin_with(buf, "NOOP")==0) || str_begin_with(buf, "noop")==0) { rt_sprintf(sbuf, "200 noop!
"); send(session->sockfd, sbuf, strlen(sbuf), 0); } else if(str_begin_with(buf, "QUIT")==0) { rt_sprintf(sbuf, "221 Bye!
"); send(session->sockfd, sbuf, strlen(sbuf), 0); rt_free(sbuf); return -1; } else { rt_sprintf(sbuf, "502 Not Implemented.
"); send(session->sockfd, sbuf, strlen(sbuf), 0); } rt_free(sbuf); return 0; } void ftpd_start() { rt_thread_t tid; tid = rt_thread_create("ftpd", ftpd_thread_entry, RT_NULL, 4096, 30, 5); if (tid != RT_NULL) rt_thread_startup(tid); } #ifdef RT_USING_FINSH #include
FINSH_FUNCTION_EXPORT(ftpd_start, start ftp server); #ifdef FINSH_USING_MSH int cmd_ftpd_start(int argc, char** argv) { ftpd_start(); return 0; } FINSH_FUNCTION_EXPORT_ALIAS(cmd_ftpd_start, __cmd_ftpd_start, start ftp server.); #endif #endif ``` udptest.c ```#include
#include
#include
#include
#include
#include
#include "lwip/sys.h" #include "lwip/netdb.h" #include "lwip/sockets.h" const char send_data[] = "This is UDP Client from RT-Thread.
"; void udpclient(const char* url, int port) { int sock; struct hostent *host; struct sockaddr_in server_addr; int count = 1; /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ host = (struct hostent *) gethostbyname(url); /* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */ if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { rt_kprintf("Socket error
"); return; } /* 初始化预连接的服务端地址 */ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr = *((struct in_addr *) host->h_addr); rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); /* 总计发送count次数据 */ while (count) { /* 发送数据到服务远端 */ sendto(sock, send_data, strlen(send_data), 0, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)); /* 线程休眠一段时间 */ //rt_thread_delay(50); /* 计数值减一 */ count--; } /* 关闭这个socket */ lwip_close(sock); } void udpsend(void *arg) { LWIP_UNUSED_ARG(arg); while(1) { rt_thread_delay(200); // 10ms udpclient("192.168.10.215", 6215); rt_kprintf("."); } } //udpserv()函数即可启动这个UDP服务端,该UDP服务端在端口上进行监听。 //当服务端接收到数据时,它将把数据打印到控制终端中;如果服务端接收到exit字符串 //时,那么服务端将退出服务。 void udpserv(void* paramemter) { int sock; int bytes_read; char *recv_data; rt_uint32_t addr_len; struct sockaddr_in server_addr, client_addr; /* 分配接收用的数据缓冲 */ recv_data = rt_malloc(1024); if (recv_data == RT_NULL) { /* 分配内存失败,返回 */ rt_kprintf("No memory
"); return; } /* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */ if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { rt_kprintf("Socket error
"); /* 释放接收用的数据缓冲 */ rt_free(recv_data); return; } /* 初始化服务端地址 */ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(5000); server_addr.sin_addr.s_addr = INADDR_ANY; rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); /* 绑定socket到服务端地址 */ if (bind(sock, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)) == -1) { /* 绑定地址失败 */ rt_kprintf("Bind error
"); /* 释放接收用的数据缓冲 */ rt_free(recv_data); return; } addr_len = sizeof(struct sockaddr); rt_kprintf("UDPServer Waiting for client on port 5000...
"); while (1) { /* 从sock中收取最大1024字节数据 */ bytes_read = recvfrom(sock, recv_data, 1024, 0, (struct sockaddr *) &client_addr, &addr_len); /* UDP不同于TCP,它基本不会出现收取的数据失败的情况,除非设置了超时等待 */ recv_data[bytes_read] = '\0'; /* 把末端清零 */ /* 输出接收的数据 */ rt_kprintf("
(%s , %d) said : ", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); rt_kprintf("%s", recv_data); /* 如果接收数据是exit,退出 */ if (strcmp(recv_data, "exit") == 0) { lwip_close(sock); /* 释放接收用的数据缓冲 */ rt_free(recv_data); break; } } return; } void udptest_start() { rt_thread_t tid_send = 0; rt_thread_t tid_recv = 0; tid_send = rt_thread_create("udptest_send", udpsend, RT_NULL, 4096, 30, 5); if (tid_send != RT_NULL) rt_thread_startup(tid_send); tid_recv = rt_thread_create("udptest_recv", udpserv, RT_NULL, 4096, 30, 5); if (tid_recv != RT_NULL) rt_thread_startup(tid_recv); } #ifdef RT_USING_FINSH #include
FINSH_FUNCTION_EXPORT(udptest_start, start udptest server); #ifdef FINSH_USING_MSH int cmd_udptest_start(int argc, char** argv) { udptest_start(); return 0; } FINSH_FUNCTION_EXPORT_ALIAS(cmd_udptest_start, __cmd_udptest_start, start udptest server.); #endif #endif ``` 请教各位大神,如何处理这个故障,谢谢!
查看更多
2
个回答
默认排序
按发布时间排序
Q007
2018-04-23
这家伙很懒,什么也没写!
诸神,搭把手帮个忙,谢谢啦!
我夏了夏天
认证专家
2018-04-24
Life isn't about finding yourself, life is about creating yourself.
这代码贴的也太长了 :L 挑重点说明问题就好了
撰写答案
登录
注册新账号
关注者
0
被浏览
3.1k
关于作者
Q007
这家伙很懒,什么也没写!
提问
8
回答
12
被采纳
0
关注TA
发私信
相关问题
1
lwip1.4.1连接经常会断开无法连接上,可以ping通
2
LPC1768:RTT+LWIP+webserver用IE刷网页出现硬件中断错误(已经解决)
3
求一些LWIP开发的经验,目前ping一直不稳定。
4
stm32f207+dp83848无法ping通
5
RTT下的LWIP传递机制
6
rtt内lwip的socket是否是线程安全?
7
Lwip+enc28j60无法ping通
8
坑爹的rtconfig.h lwip关掉了checksum
9
花了一个晚上,把RT2.0的LWIP、网卡驱动、文件系统整合起来了,发现一点小问题
10
lwip例程中udp发送时如何指定源端口发送到指定目的地端口
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
CherryUSB的bootuf2配置
2
在用clangd开发RTT吗,快来试试如何简单获得清晰干净的工作区
3
GD32F450 片内 flash驱动适配
4
STM32H7R7运行CherryUSB
5
RT-Smart首次线下培训,锁定2024 RT-Thread开发者大会!
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
UART
WIZnet_W5500
ota在线升级
PWM
freemodbus
flash
cubemx
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
中断
编译报错
Debug
SFUD
rt_mq_消息队列_msg_queue
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
a1012112796
10
个答案
1
次被采纳
踩姑娘的小蘑菇
4
个答案
1
次被采纳
红枫
4
个答案
1
次被采纳
张世争
4
个答案
1
次被采纳
Ryan_CW
4
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
6
次点赞
YZRD
2
篇文章
5
次点赞
lizimu
2
篇文章
5
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部