一个简单的文件共享工程 -- TMClient

本文档介绍了TMClient项目,这是一个用于文件共享的客户端工具。主要包含TMClient.h头文件,详细内容涉及TCP网络通信及命令行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TMClient文件夹中文件:

TMClient.h:

#ifndef __TCP_MULTIPLEXING_CLIENT_H__
#define __TCP_MULTIPLEXING_CLIENT_H__

#include "../Head/Uncopy.h"
#include "../TConnection/TConnection.h"

class TMClient : public TConnection, public Uncopy
{
public:
    TMClient(const char * host, const char * serv);
    ~TMClient();

public:
    void mainloop();

protected:
    virtual bool handlecmd(char * data) = 0;

protected:
    int  m_connfd;
};

#endif
TMClient.cpp:

#include <errno.h>
#include <unistd.h>
#include <cstdio>
#include <cstring>

#include "TMClient.h"

TMClient::TMClient(const char * host, const char * serv)
 : m_connfd(-1)
{
    m_connfd = TConnection::connect(host, serv);
}

TMClient::~TMClient()
{
    if (-1 != m_connfd) {
        close(m_connfd);
    }
}

void TMClient::mainloop()
{
    char  buff[BUFFSIZ] = { 0 };
    const int reserved = sizeof(unsigned short);

    printf(">>> ");
    while (NULL != fgets(buff + reserved, BUFFSIZ - 1 - reserved, stdin)) {
        char * data = buff + reserved;
        int len = strlen(data);
        if ('\n' == data[len - 1]) {
            data[len - 1] = '\0';
            --len;
        }

        if (strcmp("quit", data) == 0) {
            break;
        }

        unsigned short uslen = len;
        unsigned char  cmd;

        if (!handlecmd(data)) {
            break;
        }
        printf(">>> ");
    }
    if (ferror(stdin)) {
        printf("fgets error: %s\n", strerror(errno));
    }

    close(m_connfd);
    m_connfd = -1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值