LINUX系统下通过TCP通信实现打印目录下文件、文件上传、文件下载

文章描述了一个使用TCP协议的服务器程序,通过命令交互实现目录操作(列出、上传/下载文件),以及客户端的相应功能,包括发送命令和接收响应。
摘要由CSDN通过智能技术生成

功能展示

代码实现

服务器

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <fcntl.h>

void serve_list(int acceptfd)
{
    DIR *dir;                                       //通过目录结构体获得目录下文件名称等信息,同时判断是否为目录文件,若不是则发送给客户端
    struct dirent *d;
    dir = opendir(".");
    while ((d = readdir(dir)) != NULL)
    {
        if (d->d_type != DT_DIR)
            send(acceptfd, d->d_name, 128, 0);
    }
    send(acceptfd, "over", 128, 0);
    closedir(dir);
}

void serve_put(int acceptfd,char *buf)
{
    FILE *fd = fopen(buf + 4, "w+");                //通过标准io循环接收客户端发来的文件内容并写入本地文件
    if (fd == NULL)
    {
        perror("fd err\n");
    }
    printf("fd ok\n");
    while (1)
    {
        recv(acceptfd, buf, 128, 0);
        if (strcmp(buf, "read over") == 0)
            break;
        fputs(buf, fd);
    }
    fflush(fd);
    send(acceptfd, "up over", 128, 0);
}

void serve_get(int acceptfd,char *buf)
{
    FILE *fd = fopen(buf + 4, "r");                  //通过标准io将本地文件内容循环发送给客户端,以read over结尾;
    if (fd == NULL)
    {
        perror("fd err\n");
    }
    while (fgets(buf, 32, fd) != NULL)
        send(acceptfd, buf, 128, 0);
    send(acceptfd, "read over", 128, 0);
    memset(buf, 0, 128);
    send(acceptfd, "download over", 128, 0);
}

int main(int argc, char const *argv[])
{
   
   int sockfd = socket(AF_INET, SOCK_STREAM, 0);            //建立tcp服务器,指定网络信息,绑定,监听
   if (socket < 0)
   {
       perror("socket err\n");
       return -1;
   }
   printf("sockfd:%d\n", sockfd);
   struct sockaddr_in saddr, caddr;
   saddr.sin_family = AF_INET;
   saddr.sin_port = htons(7777);
   saddr.sin_addr.s_addr = inet_addr("0.0.0.0");

   if (bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
   {
       perror("bind err\n");
       return -1;
   }
   printf("bind ok\n");

   if (listen(sockfd, 6) < 0)
   {
       perror("listen err\n");
       return -1;
   }
   printf("listen ok\n");
   int acceptfd, ret;
   char buf[128] = {0};
   char buff[128] = {0};
   int len = sizeof(caddr);
   while (1)
   {
       
       acceptfd = accept(sockfd, (struct sockaddr *)&caddr, &len);           //与客户端进行连接
       if (acceptfd < 0)
       {
           perror("accept err\n");
           return -1;
       }
       printf("accept ok\n");
        printf("ip:%s  port:%d\n", inet_ntoa(caddr.sin_addr), ntohs(caddr.sin_port));
        while (1)
        {
            ret = recv(acceptfd, buf, 128, 0);
            if (ret < 0)
            {
                printf("recv err\n");
                return -1;
            }
            else if (ret == 0)
            {
                printf("client exit\n");
                break;
            }
            
            if (strcmp(buf, "list") == 0)                  //判断接收信息,进行条件匹配,执行对应的操作函数
                serve_list(acceptfd);
            if (strncmp(buf, "put", 3) == 0)
            {
                serve_put(acceptfd,buf);
            }
            if (strncmp(buf, "get", 3) == 0)
            {
                serve_get(acceptfd,buf);
            }
        }
   }
   close(acceptfd);
   return 0;
}

客户端

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
void menu(void)
{
    printf("*************list************//列出服务器所在目录下除目录外的文件名\n");      //打印菜单栏
    printf("********put filename*********//上传一个文件\n");
    printf("********get filename*********//下载文件\n");
    printf("*************quit************//断开连接\n");
}

void client_list(int sockfd, char *buf)
{
    while (1)                                     //循环接收客户端发送的文件信息并打印
    {
        recv(sockfd, buf, 128, 0);
        if (strcmp(buf, "over") == 0)
            break;
        printf("%s\n", buf);
    }
}

void client_put(int sockfd, char *buf)
{
    FILE *fd = fopen(buf + 4, "r");             //通过文件io循环将本地文件发送给服务器
    if (fd == NULL)
    {
        perror("fd err\n");
    }
    while (fgets(buf, 128, fd) != NULL)
    {
        send(sockfd, buf, 128, 0);
    }
    send(sockfd, "read over", 128, 0);
    memset(buf, 0, 128);
    recv(sockfd, buf, 128, 0);
    printf("%s\n", buf);
    memset(buf, 0, 128);
}

void client_get(int sockfd, char *buf)
{
    FILE *fd = fopen(buf + 4, "w+");             //通过标准io循环接收服务器发送内容并写入本地文件
    if (fd == NULL)
    {
        perror("fd err\n");
    }
    while (1)
    {
        recv(sockfd, buf, 128, 0);
        fputs(buf, fd);
        if (strcmp(buf, "download over") == 0)
            break;
    }
    fflush(fd);
}

int main(int argc, char const *argv[])
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);               //建立tcp客户端信息,并与服务器连接
    if (sockfd < 0)
    {
        perror("sockfd err\n");
        return -1;
    }

    printf("sockfd:%d\n", sockfd);
    struct sockaddr_in saddr, caddr;
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(7777);
    saddr.sin_addr.s_addr = inet_addr("192.168.50.56");

    if (connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
    {
        perror("connect err\n");
        return -1;
    }
    printf("connect ok\n");

    menu();

    char buf[32] = {0};
    char buff[32] = {0};
    int n;

    while (1)                                      //循环输入命令,发送给服务器,判断命令语句执行对应的操作函数
    {
        printf("plesase input the command:\n");
        fgets(buf, 128, stdin);
        if (buf[strlen(buf) - 1] == '\n')
            buf[strlen(buf) - 1] = '\0';
        send(sockfd, buf, 128, 0);

        if (strcmp(buf, "quit") == 0)
            break;
        if (strcmp(buf, "list") == 0)
            client_list(sockfd, buf);
        if (strncmp(buf, "put", 3) == 0)
            client_put(sockfd, buf);
        if (strncmp(buf, "get", 3) == 0)
            client_get(sockfd, buf);
    }
    close(sockfd);
    return 0;
}

代码讲解

该项目的核心是通过tcp通信协议,实现命令语句的传输,判断之后执行对应的操作函数。

客户端循环发送命令语句,发送后自身也要判断语句内容,并执行相应的等待函数,服务器接受到信息后,执行对应的操作函数,并将内容发送给客户端。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值