ftp源代码,供C的学习

  • t3_754443
    了解作者
  • 4.9KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-12 03:40
    上传日期
开源的ftp传输代码。喜欢研究C的可以研究下,我也是下载的,觉得可以。所以分享出来。
tftp.rar
  • tftp
  • tftp-client.c
    7.3KB
  • tftp-server.c
    8.2KB
内容介绍
#include <stdio.h> #include <sys/socket.h> #include <string.h> #include <errno.h> #include <sys/stat.h> #include <netinet/in.h> #include <arpa/inet.h rel='nofollow' onclick='return false;'> #include <netdb.h> #include <sys/ioctl.h> #include <net/if.h> #include <net/if_arp.h> #include <net/ethernet.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <netinet/in.h> #include <fcntl.h> #include <pthread.h> #define BUF_LEN 512 #define PATH_LEN 40 #define GET_FILE 1 #define SEND_FILE 2 #define MSG_ACK 3 #define MSG_BUF_LEN 512 #define HOME_DIR "/home/send/" #define MAX_NUMBER 10 struct msghead{ int msgtype; char path[PATH_LEN]; struct stat filestat; struct in_addr peerip; int isok; }; /************关于本文档******************************************** *filename: tftp-server.c *purpose: 这是在Linux下用C语言写的一个简单的文件传输服务器端,支持文件上传和下载 使用方法:./tftp-server 更多开源软件,请访问http://fossd.net/?fromuid=32 *********************************************************************/ struct in_addr getlocalip() { int fd,ret; struct ifreq ifr; struct in_addr myip; struct sockaddr_in* addr; fd = socket(AF_INET,SOCK_PACKET,htons(0x0806)); if(fd <= 0) { perror("socket"); return ; } strcpy(ifr.ifr_name,"eth0"); ret = ioctl(fd,SIOCGIFADDR,&ifr); if(ret != 0) { perror("ioctl"); return ; } addr = (struct sockaddr_in*)&ifr.ifr_addr; myip = addr->sin_addr; // printf("ip:%s \n",inet_ntoa(myip)); return myip; } void sharethread(struct msghead* pmsg) { struct msghead* ptr = pmsg; int ret; int fd; int sockfd,listenfd; char buf[MSG_BUF_LEN]; int len ,opt = 1; int totallen = 0; struct stat filestat; struct sockaddr_in servaddr,peeraddr; if(ptr->isok != 0) { return; } listenfd = socket(AF_INET,SOCK_STREAM,0); if(listenfd <= 0) { perror("socket[thread]"); return ; } opt = sizeof(struct sockaddr); if(ptr->msgtype == GET_FILE) { ret = setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); if(ret != 0) { perror("setsockopt[thread]"); close(listenfd); return; } servaddr.sin_family = AF_INET; servaddr.sin_port = htons(5678); servaddr.sin_addr = getlocalip(); ret = bind(listenfd,(struct sockaddr*)&servaddr,opt); if(ret != 0) { perror("bind[thread]"); close(listenfd); return ; } ret = listen(listenfd,5); if(ret != 0) { perror("listen[thread]"); close(listenfd); return; } sockfd = accept(listenfd,(struct sockaddr*)&peeraddr,&opt); if(sockfd <= 0) { perror("accept[thread]"); close(listenfd); return ; } fd = open(ptr->path,O_RDONLY); if(fd <= 0) { perror("open[thread]"); printf("%s \n",strerror(errno)); printf("file is:%s \n",ptr->path); close(listenfd); close(sockfd); return ; } ret = fstat(fd,&filestat); if(ret != 0) { perror("fstat[thread]"); close(listenfd); close(sockfd); close(fd); return; } len = filestat.st_size; while((ret = read(fd,buf,MSG_BUF_LEN))!= 0) { totallen += ret; write(sockfd,buf,ret); } if(totallen != len) { printf("send length is wrong\n"); close(listenfd); close(sockfd); close(fd); return ; } printf("send %s to client sucess \n",ptr->path); close(fd); close(listenfd); close(sockfd); } else if(ptr->msgtype == SEND_FILE) { peeraddr.sin_family = AF_INET; peeraddr.sin_port = htons(5678); strncpy((char*)&peeraddr.sin_addr,(char*)&ptr->peerip,sizeof(struct in_addr)); sleep(2); ret = connect(listenfd,(struct sockaddr*)&peeraddr,opt); if(ret != 0) { perror("connect[thread]"); close(listenfd); return; } len = ptr->filestat.st_size; fd = open(ptr->path,O_CREAT|O_WRONLY,777); if(fd <= 0) { perror("open[thread]"); close(listenfd); return ; } while((ret = read(listenfd,buf,MSG_BUF_LEN))!= 0) { write(fd,buf,ret); totallen += ret; } if(totallen != len) { printf("recv file length wrong\n"); close(listenfd); close(fd); return ; } printf("recv %s from client sucess \n",ptr->path); close(listenfd); close(fd); } } int main(int argc,char* argv[]) { int ret; struct sockaddr_in servaddr,peeraddr; int listenfd,connfd; int opt = 1; char homepath[50],*pchar; int fd_flag[MAX_NUMBER]={-1}; ret = mkdir(HOME_DIR,777); listenfd = socket(AF_INET,SOCK_STREAM,0); if(listenfd <= 0) { perror("socket[main]"); return listenfd; } ret = setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(int)); if(ret != 0) { perror("setsockopt[main]"); close(listenfd); return ret; } servaddr.sin_family = AF_INET; servaddr.sin_port = htons(1234); servaddr.sin_addr = getlocalip(); ret = bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); if(ret != 0) { perror("bind[main]"); close(listenfd); return ret; } listen(listenfd,5); opt = sizeof(peeraddr); connfd = accept(listenfd,(struct sockaddr*)&peeraddr,&opt); if(connfd <= 0) { perror("accept[main]"); close(listenfd); return connfd; } fd_flag[0] = listenfd; fd_flag[1] = connfd; while(1) { char buf[BUF_LEN]; int count,opt,i,maxfd = 0,tmpfd,j; struct msghead msg,*pmsg; fd_set readfds,writefds; pthread_t pid; pthread_attr_t attr; FD_ZERO(&readfds); FD_ZERO(&writefds); for(i = 0; i < MAX_NUMBER;i++) { if(fd_flag[i] != -1) { FD_SET(fd_flag[i],&readfds); maxfd = (maxfd > fd_flag[i])? maxfd:fd_flag[i]; } } ret = select(maxfd+1,&readfds,&writefds,NULL,NULL); if(ret <= 0) { perror("select"); printf("ret[%d] \n",ret); close(listenfd); return ret; } if(FD_ISSET(listenfd,&readfds)) { tmpfd = accept(listenfd,(struct sockaddr*)&peeraddr,&opt); if(tmpfd < 0) { perror("accept"); close(listenfd); return tmpfd; } else if(tmpfd == 0) { perror("accept"); continue; } for(j = 1; j < MAX_NUMBER;j++) { if(fd_flag[j] != -1) { fd_flag[j] = tmpfd; break; } } } for(i = 1; i < MAX_NUMBER;i++) { if((fd_flag[i] != -1)&&(FD_ISSET(fd_flag[i],&readfds))) { bzero(buf,BUF_LEN); count = read(connfd,buf,BUF_LEN); if(count < 0) { perror("read[main]"); close(connfd); fd_flag[i] = -1; continue; } else if(count == 0) { close(connfd); fd_flag[i] = -1; continue; } pmsg = (struct msghead*)&buf; // printf("recv:path[%s],type[%d],size[%d]\n",pmsg->path,pmsg->msgtype,pmsg->filestat.st_size); msg.msgtype = MSG_ACK; msg.isok = 0; if(pmsg->msgtype == GET_FILE) { strcpy(msg.path,pmsg->path); ret = stat(pmsg->path,&msg.filestat); if(ret != 0) { perror("stat"); msg.isok = ret; } } else if(pmsg->msgtype == SEND_FILE) { strcpy(homepath,HOME_DIR); pchar = strrchr(pmsg->path,'/'); if(pchar != NULL) { pchar++; strcat(homepath,pchar); } else { strcat(homepath,pmsg->path); } strcpy(msg.path,homepath); msg.filestat.st_size = pmsg->filestat.st_size; if(pmsg->isok != 0) { msg.isok = -1; msg.filestat.st_size = 0; } } ret = write(connfd,(void*)&msg,sizeof(msg)); if(ret <= 0) { perror("write[main]"); close(connfd); fd_flag[i] = -1; continue; } strncpy((char*)&
评论
    相关推荐
    • FTP工具
      FTP工具
    • ftpserver
      很不错的,方便、快捷!试试吧
    • ftp工具
      ftp工具
    • ftp同步
      ftp同步 FTP Synchronizer
    • FTP工具
      FTP工具
    • ftp
      FTP Golang中的简单客户端-服务器ftp实现 ftp客户端 当前实现以下功能: 目录 光盘 残障人士 ftp服务器 通过TCP套接字与ftpClient通信以显示目录信息。 硬编码以使用localhost端口1202 目前实现: 目录 光盘 ...
    • ftp client
      ftp 客户端,免费绿色,解压后就可以直接用,方便易用。
    • FTP Server
      一款很不錯的FTP軟件,試了很多軟件,都沒這款好用,架設FTP服務很容易
    • FTP工具
      好用的FTP工具,上传、下载都不错。
    • FTP12312321
      FTP 64位 直接解压就可以用,无需安装秘钥。FTP 64位 直接解压就可以用,无需安装秘钥。FTP 64位 直接解压就可以用,无需安装秘钥。