Linux--命名函数实现

Makefile:

.PHONY:all
all:client mutiServer

client:client.cc
	g++ -o $@ $^ -std=c++11
mutiServer:server.cc
	g++ -o $@ $^ -std=c++11

.PHONY:clean
clean:
	rm -f client mutiServer

server.cc

#include "comm.hpp"
#include 

static void getMessage(int fd)
{
    char buffer[SIZE];
    while(true)
    {
        memset(buffer,'\0',sizeof (buffer));
        ssize_t s=read(fd,buffer,sizeof(buffer)-1);
        if(s>0)
        {
            cout<<"["< "<

client.cc

#include "comm.hpp"

int main()
{
    //1.获取管道
    int fd=open(ipcPath.c_str(),O_WRONLY);
    if(fd<0)
    {
        perror("open");
        exit(1);
    }
    
    //2.ipc过程
    string buffer;
    while(true)
    {
        cout<<"please enter your message line:>";
        getline(cin,buffer);
        write(fd,buffer.c_str(),buffer.size());
    }

    //3.关闭管道文件
    close(fd);
    return 0;
}

comm.hpp

#ifndef _COMM_H_
#define _COMM_H_

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "Log.hpp"

using namespace std;

#define MODE 0666
#define SIZE 128
string ipcPath = "./fifo.ipc";

#endif

Log.hpp

#ifndef _LOG_H
#define _LOG_H

#include 
#include 
#include 
using namespace std;

#define Debug 0
#define Notice 1
#define Warning 2
#define Error 3

const string msg[]={
    "Debug",
    "Notice",
    "Warning",
    "Error"
};



ostream &Log(string message,int level)
{
    cout<<" | "<<(unsigned)time(NULL)<<" | "<

你可能感兴趣的:(Linux,linux,运维,服务器)