Chinaunix首页 | 论坛 | 博客
  • 博客访问: 222837
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 412
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-24 10:27
个人简介

Continuous pursuit technical details

文章分类

全部博文(48)

文章存档

2014年(1)

2013年(47)

分类: C/C++

2013-12-10 15:12:08

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "/usr/include/errno.h"


#define MAXLINE 10240
#define SERV_PORT 18002
extern int errno;




void do_echo_file(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{
        int n;
        FILE *fopen(), *fd;
        socklen_t len;
        char filename[MAXLINE];
        char mesg[MAXLINE];
        char filetype[1024];
        struct stat st;
        int type;
        char c_type[6][10]={"File","Directory","Link","\0","Other","\0"};
        char atime[1024];
        char mtime[1024];
        char ctime[1024];


        for(;;)
        {
                //printf("Ready to receive files.......\n");
                n = 0;
                len = clilen;
                /* waiting for receive data */
                memset(filename, '\0', sizeof(filename));
                n = recvfrom(sockfd, filename, MAXLINE, 0, pcliaddr, &len);
                filename[n-1] = '\0';


                //printf("Client trying to get file %s \n\n",filename);
                if (n <=1 )
                {
                        //printf("File %s should be a real file.\n");
                        strcpy(mesg, "File should be a real file.\n");
                        sendto(sockfd, mesg, strlen(mesg), 0, pcliaddr, len);
                        continue;
                }
                /* sent data back to client */
                fd=fopen((const char *)filename, "rw");
                if (fd == NULL)
                {
                        //printf("File %s does not exists.\n");
                        strcpy(mesg, "File does not exist.\n");
                        sendto(sockfd, mesg, strlen(mesg), 0, pcliaddr, len);
                        continue;
                }


                memset(mesg, '\0', sizeof(mesg));
                memset(filetype, '\0', sizeof(filetype));
                n = fread( mesg, MAXLINE, sizeof(char), fd);
                fclose(fd);
                //printf("mesg lenght is %d", n);
                //printf("content %s\n",mesg);
                if (stat(filename, &st) == 0)
                {
                        if(st.st_mode&S_IFREG)
                                type=0;
                        else if(st.st_mode&S_IFDIR)
                                type=1;
                        else if(st.st_mode&S_IFLNK)
                                type=2;
                        else
                                type=4;
                        strftime(atime,sizeof(atime),"%a, %B %d, %Y - %H:%M:%S", localtime(&st.st_atime));
                        strftime(mtime,sizeof(mtime),"%a, %B %d, %Y - %H:%M:%S", localtime(&st.st_mtime));
                        strftime(ctime,sizeof(ctime),"%a, %B %d, %Y - %H:%M:%S", localtime(&st.st_ctime));


                        //sprintf(filetype, "%-20s %-40s% 7d\n",c_type[type],filename,(int)st.st_size);
                        sprintf(filetype, "%-20s %-40s% 7d\nAccess time: %s\nModify time: %s\nChange time: %s\n",c_type[type],filename,(int)st.st_size,atime,mtime,ctime);
                }
                else
                {
                        sprintf(filetype, "%-20s% -10d% -10d\n",filename,st.st_mode,errno);
                }
                strcat(mesg, "\nFile Tye and File Size:\n");
                strcat(mesg, filetype);
                sendto(sockfd, mesg, strlen(mesg), 0, pcliaddr, len);
        }
}


int main(void)
{
        int sockfd;
        struct sockaddr_in servaddr, cliaddr;


        sockfd = socket(AF_INET, SOCK_DGRAM, 0); /* create a socket */


        /* init servaddr */
        bzero(&servaddr, sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(SERV_PORT);


        /* bind address and port to socket */
        if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
        {
        perror("bind error");
        exit(1);
        }


        do_echo_file(sockfd, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
        //char recvBuf[128];
        //char sendBuf[128];
        //char tempBuf[256];


//    while(1)
 //   {
  //      recvfrom(sockfd,recvBuf,128,0,(sockaddr*)&cliaddr,&len);
   //     char* ipClient = inet_ntoa(cliaddr.sin_addr);
    //    sprintf(tempBuf,"%s said: %s/n",ipClient,recvBuf);
     //   printf("%s",tempBuf);
      //  gets(sendBuf);
       // sendto(svr,sendBuf,strlen(sendBuf)+1,0,(sockaddr*)&cliaddr,len);
    //}


        return 0;
}

阅读(1341) | 评论(0) | 转发(0) |
0

上一篇:udpclient.c

下一篇:select-server.c

给主人留下些什么吧!~~