資源簡介
該項目實現了在linux環境下的文件傳輸功能,能夠將客戶端的文件上傳給服務器,同時能夠將服務器的文件下載到客戶端,可以在客戶端查看更改雙方路徑及目錄,并且支持多任務。在項目中應用了TCP網絡編程,文件IO,多任務編程,進程間通信等相關知識,熟練綜合運用了linux應用層相關知識。
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?“client_process.h“
int?handle_server_ack(sockfd)
{
????int?ret;
????uint32_t??body_len;
????uint32_t??total_len;
????msg_head_ctrl_t?*msg;
????ret?=?recv(?sockfd?&body_len??sizeof(body_len)?MSG_PEEK);
????if?(?ret<=0?){
????????printf(“connectionn?lost?...?\n“);
????????exit(-1);
????}
????body_len?=?ntohl(body_len);
????total_len?=?body_len?+?sizeof(msg_head_ctrl_t);
????msg?=?malloc(?total_len?+?1?);
????if?(?NULL?==?msg?){
????????perror(“out?of?memeory\n“);
????????return?;
????}
????memset(?msg?0?total_len?+?1);
????ret?=?recv(?sockfd?msg??total_len?MSG_WAITALL);
????if?(?ret<=0?){
????????printf(“connectionn?lost?...?\n“);
????????exit(-1);
????}
????msg->body_len?=?ntohl(msg->body_len);
????msg->command??=?ntohl(msg->command);
????if?(?(COMMAND_LS==msg->command)?||?(COMMAND_PWD==msg->command)?){
???????printf(“\n%s\n“msg->msg_body);
????}
????else?if?(?COMMAND_GET?==?msg->command?){
????????save_file_to_disk(msg->msg_body);
????}
????free(msg);
}
int?send_command_to_server(?int?sockfd?char?*user_input?command_type_t?type)
{
????ssize_t?ret;
????msg_head_ctrl_t?*msg;
????int?body_len;
????int?total_len;
????body_len?=?strlen(user_input);
????total_len?=?body_len?+?sizeof(msg_head_ctrl_t);
????msg?=?malloc(total_len);
????if?(?NULL?==?msg?){
????????printf(“out?of?memeory!\n“);
????????return?FAILED;
????}
????msg->command?=?htonl(type);
????msg->body_len?=?htonl(body_len);
????memcpy(msg->msg_body?user_input?body_len);
????ret?=?send(sockfd?msg?total_len?0);
????if?(?ret?????????printf(“connection?lost?...\n“);
????????free(msg);
????????return?SOCK_ERROR;
????}???????
????free(msg);
????return?SUCCESSFUL;
}
int?get_file_from_server(int?sockfd?char?*user_input?command_type_t?type)
{
????int?ret;
????int?fd=-1;
????uint32_t?len;
????fd_set??fs_client;
????struct?timeval?timeout;
????msg_head_ctrl_t?*msg;
????ret?=?send_command_to_server(sockfd?user_input?type);
????if?(?ret?!=?SUCCESSFUL){
???????return?ret;
????}
????msg?=?malloc(sizeof(msg_head_ctrl_t)?+?MAX_READ_BYTES?+?1);
????if?(?NULL?==?msg?){
????????printf(“out?of?memory!\n“);
????????return?FAILED;
????}
????while(true){
???????FD_ZERO(&fs_client);
???????FD_SET(?sockfd?&fs_client);
???????timeout.tv_sec?=?DEFAULT_CLIENT_TIME_OUT;
???????timeout.tv_usec?=?0;
???????ret?=?select(sockfd+1?&fs_client?NULL?NULL?NULL);
???????if?(?0?==?ret?){
???????//time?out
???????????printf(“time?out....\n“);
???????????ret?=?SOCK_ERROR;
???????????break;
???????}
???????if?(?-1?==?ret?){
???????????continue;
???????}
??????
???????memset(msg?0?sizeof(msg_head_ctrl_t)?+?MAX_READ_BYTES?+?1);
???????ret?=?recv(sockfd?&len?sizeof(msg->body_len)?MSG_PEEK);
???????if?(ret?<=?0){
???????????printf(“sock?error!\n“);
???????????ret?=?SOCK_ERROR;
???????????goto?Exit;
???????}
???????
???????len?=?ntohl(len);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2010-01-23?11:08??tcp\
?????文件????????6764??2010-01-07?19:34??tcp\client_process.c
?????文件?????????355??2010-01-07?19:34??tcp\client_process.h
?????文件????????1843??2010-01-07?19:34??tcp\common.c
?????文件????????1401??2010-01-07?19:34??tcp\common.h
?????文件????????1279??2010-01-07?19:34??tcp\ftpclient.c
?????文件????????1769??2010-01-07?19:34??tcp\ftpserver.c
?????文件?????????340??2010-01-07?19:34??tcp\Makefile
?????文件????????5825??2010-01-07?19:34??tcp\serv_client.c
?????文件?????????157??2010-01-07?19:34??tcp\serv_client.h
- 上一篇:35GHzVCO研制
- 下一篇:地鐵自動售票機計算當前站和目的站的站數
評論
共有 條評論