使用protobuf的socket例子

一曲冷凌霜 提交于 2019-12-17 09:34:52

peop.proto

package pck;

message People{
        required string name=1;
        required int32 id=2;
        required string email=3;
}

server.cpp

gdut17@ubuntu:~/protobuf_1215/example2/people$ cat server.cpp
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#include<string>
#include<iostream>
#include"peop.pb.h"
using namespace std;
#define SIZE 300

int main(int argc,char *argv[])
{
        int server_sock,client_sock,r;
        struct sockaddr_in server_addr,client_addr;
        socklen_t len;
        char buf[SIZE];
        if(argc!=2)
        {
                printf("need port\n");
                return 1;
        }
        server_sock = socket(AF_INET,SOCK_STREAM,0);
        memset(&server_addr,0,sizeof server_addr);
        server_addr.sin_family=AF_INET;
        server_addr.sin_addr.s_addr=inet_addr("0.0.0.0");//htonl(INADDR_ANY)
        server_addr.sin_port=htons(atoi(argv[1]));
        if(-1 == bind(server_sock, (struct sockaddr*)&server_addr,sizeof(server_addr)))
        {
                printf(" bind error\n");
                return -1;
        }
        if(-1 == listen(server_sock, 10))
        {
                printf(" listen error\n");
                return -1;
        }
        printf("wait client.\n");

        client_sock=accept(server_sock,(struct sockaddr*)&client_addr,&len);

        string s;

        memset(buf,0,SIZE);
        /*
        while( r=read(client_sock,buf,10)> 0 ) {
                buf[r]='\0';
                s+=buf;
                memset(buf,0,SIZE);
        }*/
        r=read(client_sock,buf,SIZE);
        buf[r]='\0';
        s=buf;
        printf("read r=%d, s.length()=%ld\n",r,s.length());
        //解析客户端发来的数据
        pck::People p2;
        p2.ParseFromString(s);
    cout<<p2.name()<<" "<<p2.id()<<" "<<p2.email()<<endl; 

    close(server_sock);
        return 0;
}

client.cpp

gdut17@ubuntu:~/protobuf_1215/example2/people$ cat client.cpp 

#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#include<iostream>
#include"peop.pb.h"
using namespace std;


int main(int argc,char*argv[])
{
        if(argc!=3)
        {
                printf("ip + port\n");
                return 1;
        }
        int sock;
        struct sockaddr_in addr;
        memset(&addr,0,sizeof(addr));
        socklen_t len;
        sock=socket(AF_INET,SOCK_STREAM,0);
        addr.sin_family=AF_INET;
        addr.sin_addr.s_addr=inet_addr(argv[1]);
        addr.sin_port=htons(atoi(argv[2]));
        len=sizeof addr;
        int r=connect(sock,(struct sockaddr*)&addr,len);
        if(r==-1)
        {
                printf("connect error\n");
                return 0;
        }

        pck::People p;
    p.set_name("client");
    p.set_id(1);
    p.set_email("asdasd");
    string buf;
    p.SerializeToString(&buf);

    r=write(sock,buf.c_str(),buf.length());
    printf("write  r=%d, buf.length()=%ld\n",r,buf.length());
    close(sock);
        return 0;
}

makefile

INC=/home/gdut17/protobuf_1215/protobuf_install/include
LIB=/home/gdut17/protobuf_1215/protobuf_install/lib
lib=protobuf

all:server client

server.o:server.cpp
        g++ -g -c server.cpp -I$(INC) -L$(LIB) -l$(lib)
client.o:client.cpp
        g++ -g -c client.cpp -I$(INC) -L$(LIB) -l$(lib)

server:server.o peop.pb.o 
        g++ -g -o server server.o peop.pb.o -I$(INC) -L$(LIB) -l$(lib)
client:client.o peop.pb.o 
        g++ -g -o client client.o peop.pb.o -I$(INC) -L$(LIB) -l$(lib)
peop.pb.o:peop.pb.cc
        g++ -g -c peop.pb.cc -I$(INC) -L$(LIB) -l$(lib)

clean:server client server.o client.o peop.pb.o
        rm server client server.o client.o peop.pb.o

编译运行make

gdut17@ubuntu:~/protobuf_1215/example2/people$ ./server 8999
wait client.
read r=18, s.length()=18
client 1 asdasd


gdut17@ubuntu:~/protobuf_1215/example2/people$ ./client 127.0.0.1 8999
write  r=18, buf.length()=18
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!