最近使用google protobuf,对google protobuf的一些特点有新的发现。
文件名:msg.proto
package Data;
message datatype
{
required int32 port = 1;
required string address = 2; //ip
required string str = 3; //for communication
optional fixed64 length = 4;
}
之前一直不知道等号后面的数字是什么,可能看漏了官方上的信息。试验了一下才知道,
那数字相当于一个标记,必须唯一,如果将port=1改成port=2,使用protoc编译时,会
编译不通过,因为跟后面的address冲突了。另外,这些数字可以不用从1开始,也可以
从2开始,有试验过,没问题。
文件名:msg.pb.h
#if GOOGLE_PROTOBUF_VERSION < 2004000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers. Please update
#error your headers.
#endif
#if 2004001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers. Please
#error regenerate this file with a newer version of protoc.
#endif
编译完.proto后出现的这个.pb.h文件,我编译的时候会报跟版本相关的错误,于是把该文件的上
面代码注释掉,ok!
代码包下载链接:http://download.csdn.net/detail/ceronjyc/6543747(这个很抱歉地说,之前上传的有个漏洞,现在改进了,csdn上的资源却不能更正了,只能在github上更正了。)
或https://github.com/updata7/protobuf_socket
总结:google protobuf用于网络通信,实现数据的海量交互时才能发挥其作用,用于单机上的通信作用
不明显,顶多就是占用的存储空间较少。select实现的I/O复用,实际上是分别阻塞在它所关心的事件上。
如下伪代码:
原本以为红色字体部分会不断输出,实验之后才证明,不是这样。所以我认为select的I/O复用
模式是不断对所关心的事件轮询访问,并且会阻塞,要有被关心的事件来时,中间部分(即红
色字体)才能被执行到。不知是否理解错了,欢迎指点。
while(1)
{
if(stdineof == 0)
FD_SET(fd_stdin, &rset);
FD_SET(listenfd, &rset);
nready = select(maxfd+1, &rset, NULL, NULL, NULL);
if(nready == -1)
ERR_EXIT("select");
if(nready == 0)
continue;
if(FD_ISSET(fd_stdin, &rset))
{
}
std::cout<<"input the message you want to send: ";
fflush(stdout);
if(FD_ISSET(listenfd, &rset))
{
}
}