echo服务器--聊天版

log.hpp

cpp 复制代码
#pragma once
#include <iostream>
#include <string>
#include <cstdarg>
#include <cstdio>
#include <ctime>

#define DEBUG 0
#define NORMAL 1
#define WARNING 2
#define ERROR 3
#define FATAL 4

const char *gLevelMap[5] = {"DEBUG", "NORMAL", "WARNING", "ERROR", "FATAL"};

#define LOGFILE "./threadpool.log"

void logMessage(int level, const char *format, ...)
{
    char stdBuffer[1024]; // 标准部分
    time_t timestamp = time(nullptr);
    struct tm *time = localtime(&timestamp);
    snprintf(stdBuffer, sizeof stdBuffer, "[%s][%ld]", gLevelMap[level], timestamp);

    char logBuffer[64]; // 自定义部分
    va_list args;
    va_start(args, format);
    vsnprintf(logBuffer, sizeof(logBuffer), format, args); // 使用 vsnprintf 而不是 vsprintf
    va_end(args);

    // FILE* fp=fopen(LOGFILE,"a");

    // 打印到显示器
    printf("%s %s\n", stdBuffer, logBuffer);

    // 打印到指定文件
    // fprintf(fp,"%s%s\n",stdBuffer,logBuffer);

    // fclose(fp);
}

Makefile

cpp 复制代码
.PHONY:all
all:udp_client udp_server

udp_client:udp_client.cc
	g++ -o $@ $^ -std=c++11
udp_server:udp_server.cc
	g++ -o $@ $^ -std=c++11

.PHONY:clean
clean:
	rm -f udp_client udp_server

udp_client.cc

cpp 复制代码
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include "log.hpp"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>

static void usage(std::string proc)
{
    std::cout << "\nUsage: " << proc << " ip port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        usage(argv[0]);
        exit(1);
    }

    std::string ip = argv[1];
    uint16_t port = atoi(argv[2]);

    struct sockaddr_in server;
    bzero(&server, sizeof server);
    server.sin_addr.s_addr = inet_addr(ip.c_str());
    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    socklen_t len = sizeof server;

    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0)
    {
        logMessage(FATAL, "client sock error: %d:%s", errno, strerror(errno));
        exit(2);
    }

    std::string message;
    char buffer[1024];
    while (true)
    {
        std::cout << "请输入你的信息# ";
        getline(std::cin, message);
        if (message == "quit")
            break;
        sendto(sock, message.c_str(), message.size(), 0, (struct sockaddr *)&server, len);

        struct sockaddr_in temp;
        socklen_t len = sizeof(temp);
        ssize_t s = recvfrom(sock, buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&temp, &len);
        if (s > 0)
        {
            buffer[s] = 0;
            std::cout << "server echo# " << buffer << std::endl;
        }
    }
    close(sock);
    return 0;
}

udp_server.cc

cpp 复制代码
#include "udp_server.hpp"
#include <memory>

static void usage(std::string proc)
{
    std::cout << "\nUsage: " << proc << "port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        usage(argv[0]);
        exit(1);
    }
    uint16_t port = atoi(argv[1]);
    std::unique_ptr<UdpServer> svr(new UdpServer(port));

    svr->initServer();

    svr->Start();

    return 0;
}

udp_server.hpp

cpp 复制代码
#ifndef UDP_SERVER_HPP
#define UDP_SERVER_HPP

#include <iostream>
#include <string>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include "log.hpp"
#include <cerrno>
#include <string.h>
#include <cstdlib>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#define SIZE 1024

class UdpServer
{
public:
    UdpServer(uint16_t port, std::string ip = "")
        : _port(port), _ip(ip), _sock(-1)
    {
    }

    bool initServer()
    {
        // 从这里开始,就是新的系统调用
        // 1.创建套接字
        _sock = socket(AF_INET, SOCK_DGRAM, 0);
        if (_sock < 0)
        {
            logMessage(FATAL, "%d:%s", errno, strerror(errno));
            exit(2);
        }
        // 2.bind绑定
        struct sockaddr_in local;
        bzero(&local, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_addr.s_addr = _ip.empty() ? INADDR_ANY : inet_addr(_ip.c_str());
        local.sin_port = htons(_port);
        if (bind(_sock, (const sockaddr *)&local, sizeof(local)) < 0)
        {
            logMessage(FATAL, "%d:%s", errno, strerror(errno));
            exit(3);
        }
        logMessage(NORMAL, "init udp server done ...%s", strerror(errno));

        return true;
    }

    void Start()
    {
        char buffer[SIZE];
        while (true)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof peer;
            bzero(&peer, sizeof peer);
            ssize_t s = recvfrom(_sock, buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&peer, &len);
            if (s > 0)
            {
                buffer[s] = 0;
                // 输出发送的数据信息
                // 是谁?
                uint16_t cli_port = ntohs(peer.sin_port);
                std::string cli_ip = inet_ntoa(peer.sin_addr);
                std::cout << "ip: " << cli_ip << " | " << "端口号" << cli_port << "接收到的信息:" << buffer << std::endl;
            }
            sendto(_sock, buffer, strlen(buffer), 0, (struct sockaddr *)&peer, len);
        }
    }

    ~UdpServer()
    {
        if (_sock >= 0)
            close(_sock);
    }

private:
    // 一个服务器,一般需要ip地址和port
    std::string _ip;
    uint16_t _port;
    int _sock;
};

#endif
相关推荐
。puppy5 分钟前
HCIP--3实验- 链路聚合,VLAN间通讯,Super VLAN,MSTP,VRRPip配置,OSPF(静态路由,环回,缺省,空接口),NAT
运维·服务器
一只爱打拳的程序猿12 分钟前
【Spring】更加简单的将对象存入Spring中并使用
java·后端·spring
幸运超级加倍~12 分钟前
软件设计师-上午题-16 算法(4-5分)
笔记·算法
杨荧13 分钟前
【JAVA毕业设计】基于Vue和SpringBoot的服装商城系统学科竞赛管理系统
java·开发语言·vue.js·spring boot·spring cloud·java-ee·kafka
颇有几分姿色14 分钟前
深入理解 Linux 内存管理:free 命令详解
linux·运维·服务器
minDuck15 分钟前
ruoyi-vue集成tianai-captcha验证码
java·前端·vue.js
yannan2019031320 分钟前
【算法】(Python)动态规划
python·算法·动态规划
埃菲尔铁塔_CV算法22 分钟前
人工智能图像算法:开启视觉新时代的钥匙
人工智能·算法
EasyCVR22 分钟前
EHOME视频平台EasyCVR视频融合平台使用OBS进行RTMP推流,WebRTC播放出现抖动、卡顿如何解决?
人工智能·算法·ffmpeg·音视频·webrtc·监控视频接入
linsa_pursuer23 分钟前
快乐数算法
算法·leetcode·职场和发展