├── CMakeLists.txt ├── README.md ├── client ├── client.cc ├── contest ├── contest.cc ├── dealpack.cc ├── dealpack.h ├── msg.pb.cc ├── msg.pb.h ├── msg.proto ├── server └── server.cc /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.6) 2 | 3 | add_custom_command(OUTPUT msg.pb.cc resolver.pb.h 4 | COMMAND protoc 5 | ARGS --cpp_out . ${CMAKE_CURRENT_SOURCE_DIR}/msg.proto -I${CMAKE_CURRENT_SOURCE_DIR} 6 | DEPENDS msg.proto) 7 | 8 | project(CHENK) 9 | 10 | add_library(msg_proto msg.pb.cc) 11 | target_link_libraries(msg_proto protobuf pthread) 12 | 13 | set(CLIENT ../client.cc ../dealpack.cc) 14 | add_executable(client ${CLIENT}) 15 | target_link_libraries(client msg_proto protobuf) 16 | 17 | set(SERVER ../server.cc ../dealpack.cc) 18 | add_executable(server ${SERVER}) 19 | target_link_libraries(server msg_proto protobuf) 20 | 21 | set(CONTEST ../contest.cc ../dealpack.cc) 22 | add_executable(contest ${CONTEST}) 23 | target_link_libraries(contest msg_proto protobuf) 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | protobuf_socket 2 | =============== 3 | 4 | Communication between server and client,using the google protobuf as a data carrier.Linux epoll for server and select for client. 5 | -------------------------------------------------------------------------------- /client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/updata7/protobuf_socket/05b4ada59e015a024520e47e8e6f9e491037afe9/client -------------------------------------------------------------------------------- /client.cc: -------------------------------------------------------------------------------- 1 | #include "msg.pb.h" 2 | #include "dealpack.h" 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #define ERR_EXIT(m)\ 17 | do\ 18 | {\ 19 | perror(m);\ 20 | exit(EXIT_FAILURE);\ 21 | }while(0) 22 | 23 | extern void do_client(int listenfd); //deal with connect 24 | 25 | int main(void) 26 | { 27 | int listenfd = socket(PF_INET, SOCK_STREAM, 0); 28 | if(listenfd == -1) 29 | { 30 | ERR_EXIT("listenfd"); 31 | } 32 | 33 | //server's address and port 34 | struct sockaddr_in servaddr; 35 | memset(&servaddr, 0, sizeof(servaddr)); 36 | servaddr.sin_family = AF_INET; 37 | servaddr.sin_port = htons(5588); 38 | servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 39 | 40 | //establish connect 41 | if(connect(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) 42 | { 43 | ERR_EXIT("connect"); 44 | } 45 | 46 | do_client(listenfd); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /contest: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/updata7/protobuf_socket/05b4ada59e015a024520e47e8e6f9e491037afe9/contest -------------------------------------------------------------------------------- /contest.cc: -------------------------------------------------------------------------------- 1 | #include "msg.pb.h" 2 | #include "dealpack.h" 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #define ERR_EXIT(m)\ 17 | do\ 18 | {\ 19 | perror(m);\ 20 | exit(EXIT_FAILURE);\ 21 | }while(0) 22 | 23 | extern void do_client(int listenfd); //deal with connect 24 | 25 | int main(void) 26 | { 27 | int count = 0; 28 | while(1) 29 | { 30 | int listenfd = socket(PF_INET, SOCK_STREAM, 0); 31 | if(listenfd == -1) 32 | { 33 | sleep(4); //make server enough time to deal with close 34 | ERR_EXIT("listenfd"); 35 | } 36 | 37 | //server's address and port 38 | struct sockaddr_in servaddr; 39 | memset(&servaddr, 0, sizeof(servaddr)); 40 | servaddr.sin_family = AF_INET; 41 | servaddr.sin_port = htons(5588); 42 | servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 43 | 44 | //establish connect 45 | 46 | if(connect(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) 47 | { 48 | ERR_EXIT("connect"); 49 | } 50 | 51 | struct sockaddr_in localaddr; 52 | socklen_t addrlen = sizeof(localaddr); 53 | if (getsockname(listenfd, (struct sockaddr*)&localaddr, &addrlen) < 0) 54 | ERR_EXIT("getsockname"); 55 | printf("ip=%s port=%d\n", inet_ntoa(localaddr.sin_addr), ntohs(localaddr.sin_port)); 56 | printf("count = %d\n", ++count); 57 | } 58 | 59 | // do_client(listenfd); 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /dealpack.cc: -------------------------------------------------------------------------------- 1 | #include "dealpack.h" 2 | #include "msg.pb.h" 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #define ERR_EXIT(m)\ 23 | do\ 24 | {\ 25 | perror(m);\ 26 | exit(EXIT_FAILURE);\ 27 | }while(0) 28 | 29 | typedef std::vector EventList; 30 | 31 | static void activate_nonblock(int conn); 32 | 33 | void do_service(int listenfd) 34 | { 35 | Data::datatype msg; 36 | //for epoll 37 | std::vector clients; 38 | int epollfd; 39 | epollfd = epoll_create1(EPOLL_CLOEXEC); 40 | 41 | struct epoll_event event; 42 | event.data.fd = listenfd; 43 | event.events = EPOLLIN | EPOLLET; 44 | epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &event); 45 | 46 | EventList events(16); //init 16 events 47 | 48 | //client's address 49 | struct sockaddr_in peeraddr; 50 | socklen_t len = sizeof(peeraddr); 51 | 52 | //accept connect from client 53 | int conn; 54 | int i; 55 | 56 | char recvbuf[1024] = {0}; 57 | char sendbuf = '0'; 58 | 59 | int nready; 60 | int ret; 61 | while(1) 62 | { 63 | nready = epoll_wait(epollfd, &*events.begin(), static_cast(events.size()), -1); 64 | if(nready == -1) 65 | { 66 | if(errno == EINTR) 67 | continue; 68 | 69 | ERR_EXIT("epoll_wait"); 70 | } 71 | if(nready == 0) 72 | continue; 73 | 74 | if((size_t)nready == events.size()) 75 | events.resize(events.size()*2); 76 | 77 | for(i=0; ih_addr)); 165 | return 0; 166 | } 167 | 168 | //get local port 169 | static unsigned short getlocalport(int listenfd) 170 | { 171 | unsigned short port; 172 | struct sockaddr_in localaddr; 173 | socklen_t addrlen = sizeof(localaddr); 174 | if(getsockname(listenfd, (struct sockaddr *)&localaddr, &addrlen) < 0) 175 | { 176 | ERR_EXIT("getsockname"); 177 | } 178 | 179 | port = ntohs(localaddr.sin_port); 180 | return port; 181 | } 182 | 183 | void do_client(int listenfd) 184 | { 185 | Data::datatype msg; 186 | std::string str; 187 | std::string data; 188 | char sendbuf[1024] = {0}; 189 | char recvbuf = '0'; 190 | char ip[100]; 191 | unsigned short port; 192 | getlocalip(ip); //get local ip address 193 | port = getlocalport(listenfd); //get local port 194 | unsigned short port1 = port; //must be have,for port,or else port would change,don't know why 195 | 196 | //select 197 | fd_set rset; 198 | FD_ZERO(&rset); 199 | 200 | int nready; 201 | int maxfd; 202 | int fd_stdin = fileno(stdin); 203 | if(fd_stdin > listenfd) 204 | maxfd = fd_stdin; 205 | else 206 | maxfd = listenfd; 207 | 208 | int stdineof = 0; 209 | 210 | int flag = 1; 211 | while(1) 212 | { 213 | if(flag) 214 | { 215 | std::cout<<"input the message you want to send: "; 216 | fflush(stdout); 217 | flag=0; 218 | } 219 | if(stdineof == 0) 220 | FD_SET(fd_stdin, &rset); 221 | FD_SET(listenfd, &rset); 222 | nready = select(maxfd+1, &rset, NULL, NULL, NULL); 223 | if(nready == -1) 224 | ERR_EXIT("select"); 225 | 226 | if(nready == 0) 227 | continue; 228 | if(FD_ISSET(fd_stdin, &rset)) 229 | { 230 | getline(std::cin, str); 231 | memset(sendbuf, 0, sizeof(sendbuf)); 232 | 233 | //encode data 234 | msg.set_port(port1); 235 | msg.set_str(str); 236 | msg.set_address(ip); 237 | msg.SerializeToString(&data); 238 | strcpy(sendbuf, data.c_str()); 239 | if(send(listenfd, sendbuf, strlen(sendbuf), 0) == -1) 240 | { 241 | ERR_EXIT("send"); 242 | } 243 | std::cout<<"input the message you want to send: "; 244 | fflush(stdout); 245 | } 246 | 247 | if(FD_ISSET(listenfd, &rset)) 248 | { 249 | 250 | int ret = recv(listenfd, &recvbuf, strlen(&recvbuf), 0); 251 | if(ret == -1) 252 | ERR_EXIT("read"); 253 | 254 | else if(ret == 0) 255 | { 256 | std::cout< 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | // @@protoc_insertion_point(includes) 15 | 16 | namespace Data { 17 | 18 | namespace { 19 | 20 | const ::google::protobuf::Descriptor* datatype_descriptor_ = NULL; 21 | const ::google::protobuf::internal::GeneratedMessageReflection* 22 | datatype_reflection_ = NULL; 23 | 24 | } // namespace 25 | 26 | 27 | void protobuf_AssignDesc_msg_2eproto() { 28 | protobuf_AddDesc_msg_2eproto(); 29 | const ::google::protobuf::FileDescriptor* file = 30 | ::google::protobuf::DescriptorPool::generated_pool()->FindFileByName( 31 | "msg.proto"); 32 | GOOGLE_CHECK(file != NULL); 33 | datatype_descriptor_ = file->message_type(0); 34 | static const int datatype_offsets_[4] = { 35 | GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(datatype, port_), 36 | GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(datatype, address_), 37 | GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(datatype, str_), 38 | GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(datatype, length_), 39 | }; 40 | datatype_reflection_ = 41 | new ::google::protobuf::internal::GeneratedMessageReflection( 42 | datatype_descriptor_, 43 | datatype::default_instance_, 44 | datatype_offsets_, 45 | GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(datatype, _has_bits_[0]), 46 | GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET(datatype, _unknown_fields_), 47 | -1, 48 | ::google::protobuf::DescriptorPool::generated_pool(), 49 | ::google::protobuf::MessageFactory::generated_factory(), 50 | sizeof(datatype)); 51 | } 52 | 53 | namespace { 54 | 55 | GOOGLE_PROTOBUF_DECLARE_ONCE(protobuf_AssignDescriptors_once_); 56 | inline void protobuf_AssignDescriptorsOnce() { 57 | ::google::protobuf::GoogleOnceInit(&protobuf_AssignDescriptors_once_, 58 | &protobuf_AssignDesc_msg_2eproto); 59 | } 60 | 61 | void protobuf_RegisterTypes(const ::std::string&) { 62 | protobuf_AssignDescriptorsOnce(); 63 | ::google::protobuf::MessageFactory::InternalRegisterGeneratedMessage( 64 | datatype_descriptor_, &datatype::default_instance()); 65 | } 66 | 67 | } // namespace 68 | 69 | void protobuf_ShutdownFile_msg_2eproto() { 70 | delete datatype::default_instance_; 71 | delete datatype_reflection_; 72 | } 73 | 74 | void protobuf_AddDesc_msg_2eproto() { 75 | static bool already_here = false; 76 | if (already_here) return; 77 | already_here = true; 78 | GOOGLE_PROTOBUF_VERIFY_VERSION; 79 | 80 | ::google::protobuf::DescriptorPool::InternalAddGeneratedFile( 81 | "\n\tmsg.proto\022\004Data\"F\n\010datatype\022\014\n\004port\030\001 " 82 | "\002(\005\022\017\n\007address\030\002 \002(\t\022\013\n\003str\030\003 \002(\t\022\016\n\006len" 83 | "gth\030\004 \001(\006", 89); 84 | ::google::protobuf::MessageFactory::InternalRegisterGeneratedFile( 85 | "msg.proto", &protobuf_RegisterTypes); 86 | datatype::default_instance_ = new datatype(); 87 | datatype::default_instance_->InitAsDefaultInstance(); 88 | ::google::protobuf::internal::OnShutdown(&protobuf_ShutdownFile_msg_2eproto); 89 | } 90 | 91 | // Force AddDescriptors() to be called at static initialization time. 92 | struct StaticDescriptorInitializer_msg_2eproto { 93 | StaticDescriptorInitializer_msg_2eproto() { 94 | protobuf_AddDesc_msg_2eproto(); 95 | } 96 | } static_descriptor_initializer_msg_2eproto_; 97 | 98 | 99 | // =================================================================== 100 | 101 | #ifndef _MSC_VER 102 | const int datatype::kPortFieldNumber; 103 | const int datatype::kAddressFieldNumber; 104 | const int datatype::kStrFieldNumber; 105 | const int datatype::kLengthFieldNumber; 106 | #endif // !_MSC_VER 107 | 108 | datatype::datatype() 109 | : ::google::protobuf::Message() { 110 | SharedCtor(); 111 | } 112 | 113 | void datatype::InitAsDefaultInstance() { 114 | } 115 | 116 | datatype::datatype(const datatype& from) 117 | : ::google::protobuf::Message() { 118 | SharedCtor(); 119 | MergeFrom(from); 120 | } 121 | 122 | void datatype::SharedCtor() { 123 | _cached_size_ = 0; 124 | port_ = 0; 125 | address_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 126 | str_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 127 | length_ = GOOGLE_ULONGLONG(0); 128 | ::memset(_has_bits_, 0, sizeof(_has_bits_)); 129 | } 130 | 131 | datatype::~datatype() { 132 | SharedDtor(); 133 | } 134 | 135 | void datatype::SharedDtor() { 136 | if (address_ != &::google::protobuf::internal::kEmptyString) { 137 | delete address_; 138 | } 139 | if (str_ != &::google::protobuf::internal::kEmptyString) { 140 | delete str_; 141 | } 142 | if (this != default_instance_) { 143 | } 144 | } 145 | 146 | void datatype::SetCachedSize(int size) const { 147 | GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); 148 | _cached_size_ = size; 149 | GOOGLE_SAFE_CONCURRENT_WRITES_END(); 150 | } 151 | const ::google::protobuf::Descriptor* datatype::descriptor() { 152 | protobuf_AssignDescriptorsOnce(); 153 | return datatype_descriptor_; 154 | } 155 | 156 | const datatype& datatype::default_instance() { 157 | if (default_instance_ == NULL) protobuf_AddDesc_msg_2eproto(); return *default_instance_; 158 | } 159 | 160 | datatype* datatype::default_instance_ = NULL; 161 | 162 | datatype* datatype::New() const { 163 | return new datatype; 164 | } 165 | 166 | void datatype::Clear() { 167 | if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { 168 | port_ = 0; 169 | if (has_address()) { 170 | if (address_ != &::google::protobuf::internal::kEmptyString) { 171 | address_->clear(); 172 | } 173 | } 174 | if (has_str()) { 175 | if (str_ != &::google::protobuf::internal::kEmptyString) { 176 | str_->clear(); 177 | } 178 | } 179 | length_ = GOOGLE_ULONGLONG(0); 180 | } 181 | ::memset(_has_bits_, 0, sizeof(_has_bits_)); 182 | mutable_unknown_fields()->Clear(); 183 | } 184 | 185 | bool datatype::MergePartialFromCodedStream( 186 | ::google::protobuf::io::CodedInputStream* input) { 187 | #define DO_(EXPRESSION) if (!(EXPRESSION)) return false 188 | ::google::protobuf::uint32 tag; 189 | while ((tag = input->ReadTag()) != 0) { 190 | switch (::google::protobuf::internal::WireFormatLite::GetTagFieldNumber(tag)) { 191 | // required int32 port = 1; 192 | case 1: { 193 | if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == 194 | ::google::protobuf::internal::WireFormatLite::WIRETYPE_VARINT) { 195 | DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< 196 | ::google::protobuf::int32, ::google::protobuf::internal::WireFormatLite::TYPE_INT32>( 197 | input, &port_))); 198 | set_has_port(); 199 | } else { 200 | goto handle_uninterpreted; 201 | } 202 | if (input->ExpectTag(18)) goto parse_address; 203 | break; 204 | } 205 | 206 | // required string address = 2; 207 | case 2: { 208 | if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == 209 | ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { 210 | parse_address: 211 | DO_(::google::protobuf::internal::WireFormatLite::ReadString( 212 | input, this->mutable_address())); 213 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 214 | this->address().data(), this->address().length(), 215 | ::google::protobuf::internal::WireFormat::PARSE); 216 | } else { 217 | goto handle_uninterpreted; 218 | } 219 | if (input->ExpectTag(26)) goto parse_str; 220 | break; 221 | } 222 | 223 | // required string str = 3; 224 | case 3: { 225 | if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == 226 | ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) { 227 | parse_str: 228 | DO_(::google::protobuf::internal::WireFormatLite::ReadString( 229 | input, this->mutable_str())); 230 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 231 | this->str().data(), this->str().length(), 232 | ::google::protobuf::internal::WireFormat::PARSE); 233 | } else { 234 | goto handle_uninterpreted; 235 | } 236 | if (input->ExpectTag(33)) goto parse_length; 237 | break; 238 | } 239 | 240 | // optional fixed64 length = 4; 241 | case 4: { 242 | if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == 243 | ::google::protobuf::internal::WireFormatLite::WIRETYPE_FIXED64) { 244 | parse_length: 245 | DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive< 246 | ::google::protobuf::uint64, ::google::protobuf::internal::WireFormatLite::TYPE_FIXED64>( 247 | input, &length_))); 248 | set_has_length(); 249 | } else { 250 | goto handle_uninterpreted; 251 | } 252 | if (input->ExpectAtEnd()) return true; 253 | break; 254 | } 255 | 256 | default: { 257 | handle_uninterpreted: 258 | if (::google::protobuf::internal::WireFormatLite::GetTagWireType(tag) == 259 | ::google::protobuf::internal::WireFormatLite::WIRETYPE_END_GROUP) { 260 | return true; 261 | } 262 | DO_(::google::protobuf::internal::WireFormat::SkipField( 263 | input, tag, mutable_unknown_fields())); 264 | break; 265 | } 266 | } 267 | } 268 | return true; 269 | #undef DO_ 270 | } 271 | 272 | void datatype::SerializeWithCachedSizes( 273 | ::google::protobuf::io::CodedOutputStream* output) const { 274 | // required int32 port = 1; 275 | if (has_port()) { 276 | ::google::protobuf::internal::WireFormatLite::WriteInt32(1, this->port(), output); 277 | } 278 | 279 | // required string address = 2; 280 | if (has_address()) { 281 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 282 | this->address().data(), this->address().length(), 283 | ::google::protobuf::internal::WireFormat::SERIALIZE); 284 | ::google::protobuf::internal::WireFormatLite::WriteString( 285 | 2, this->address(), output); 286 | } 287 | 288 | // required string str = 3; 289 | if (has_str()) { 290 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 291 | this->str().data(), this->str().length(), 292 | ::google::protobuf::internal::WireFormat::SERIALIZE); 293 | ::google::protobuf::internal::WireFormatLite::WriteString( 294 | 3, this->str(), output); 295 | } 296 | 297 | // optional fixed64 length = 4; 298 | if (has_length()) { 299 | ::google::protobuf::internal::WireFormatLite::WriteFixed64(4, this->length(), output); 300 | } 301 | 302 | if (!unknown_fields().empty()) { 303 | ::google::protobuf::internal::WireFormat::SerializeUnknownFields( 304 | unknown_fields(), output); 305 | } 306 | } 307 | 308 | ::google::protobuf::uint8* datatype::SerializeWithCachedSizesToArray( 309 | ::google::protobuf::uint8* target) const { 310 | // required int32 port = 1; 311 | if (has_port()) { 312 | target = ::google::protobuf::internal::WireFormatLite::WriteInt32ToArray(1, this->port(), target); 313 | } 314 | 315 | // required string address = 2; 316 | if (has_address()) { 317 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 318 | this->address().data(), this->address().length(), 319 | ::google::protobuf::internal::WireFormat::SERIALIZE); 320 | target = 321 | ::google::protobuf::internal::WireFormatLite::WriteStringToArray( 322 | 2, this->address(), target); 323 | } 324 | 325 | // required string str = 3; 326 | if (has_str()) { 327 | ::google::protobuf::internal::WireFormat::VerifyUTF8String( 328 | this->str().data(), this->str().length(), 329 | ::google::protobuf::internal::WireFormat::SERIALIZE); 330 | target = 331 | ::google::protobuf::internal::WireFormatLite::WriteStringToArray( 332 | 3, this->str(), target); 333 | } 334 | 335 | // optional fixed64 length = 4; 336 | if (has_length()) { 337 | target = ::google::protobuf::internal::WireFormatLite::WriteFixed64ToArray(4, this->length(), target); 338 | } 339 | 340 | if (!unknown_fields().empty()) { 341 | target = ::google::protobuf::internal::WireFormat::SerializeUnknownFieldsToArray( 342 | unknown_fields(), target); 343 | } 344 | return target; 345 | } 346 | 347 | int datatype::ByteSize() const { 348 | int total_size = 0; 349 | 350 | if (_has_bits_[0 / 32] & (0xffu << (0 % 32))) { 351 | // required int32 port = 1; 352 | if (has_port()) { 353 | total_size += 1 + 354 | ::google::protobuf::internal::WireFormatLite::Int32Size( 355 | this->port()); 356 | } 357 | 358 | // required string address = 2; 359 | if (has_address()) { 360 | total_size += 1 + 361 | ::google::protobuf::internal::WireFormatLite::StringSize( 362 | this->address()); 363 | } 364 | 365 | // required string str = 3; 366 | if (has_str()) { 367 | total_size += 1 + 368 | ::google::protobuf::internal::WireFormatLite::StringSize( 369 | this->str()); 370 | } 371 | 372 | // optional fixed64 length = 4; 373 | if (has_length()) { 374 | total_size += 1 + 8; 375 | } 376 | 377 | } 378 | if (!unknown_fields().empty()) { 379 | total_size += 380 | ::google::protobuf::internal::WireFormat::ComputeUnknownFieldsSize( 381 | unknown_fields()); 382 | } 383 | GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); 384 | _cached_size_ = total_size; 385 | GOOGLE_SAFE_CONCURRENT_WRITES_END(); 386 | return total_size; 387 | } 388 | 389 | void datatype::MergeFrom(const ::google::protobuf::Message& from) { 390 | GOOGLE_CHECK_NE(&from, this); 391 | const datatype* source = 392 | ::google::protobuf::internal::dynamic_cast_if_available( 393 | &from); 394 | if (source == NULL) { 395 | ::google::protobuf::internal::ReflectionOps::Merge(from, this); 396 | } else { 397 | MergeFrom(*source); 398 | } 399 | } 400 | 401 | void datatype::MergeFrom(const datatype& from) { 402 | GOOGLE_CHECK_NE(&from, this); 403 | if (from._has_bits_[0 / 32] & (0xffu << (0 % 32))) { 404 | if (from.has_port()) { 405 | set_port(from.port()); 406 | } 407 | if (from.has_address()) { 408 | set_address(from.address()); 409 | } 410 | if (from.has_str()) { 411 | set_str(from.str()); 412 | } 413 | if (from.has_length()) { 414 | set_length(from.length()); 415 | } 416 | } 417 | mutable_unknown_fields()->MergeFrom(from.unknown_fields()); 418 | } 419 | 420 | void datatype::CopyFrom(const ::google::protobuf::Message& from) { 421 | if (&from == this) return; 422 | Clear(); 423 | MergeFrom(from); 424 | } 425 | 426 | void datatype::CopyFrom(const datatype& from) { 427 | if (&from == this) return; 428 | Clear(); 429 | MergeFrom(from); 430 | } 431 | 432 | bool datatype::IsInitialized() const { 433 | if ((_has_bits_[0] & 0x00000007) != 0x00000007) return false; 434 | 435 | return true; 436 | } 437 | 438 | void datatype::Swap(datatype* other) { 439 | if (other != this) { 440 | std::swap(port_, other->port_); 441 | std::swap(address_, other->address_); 442 | std::swap(str_, other->str_); 443 | std::swap(length_, other->length_); 444 | std::swap(_has_bits_[0], other->_has_bits_[0]); 445 | _unknown_fields_.Swap(&other->_unknown_fields_); 446 | std::swap(_cached_size_, other->_cached_size_); 447 | } 448 | } 449 | 450 | ::google::protobuf::Metadata datatype::GetMetadata() const { 451 | protobuf_AssignDescriptorsOnce(); 452 | ::google::protobuf::Metadata metadata; 453 | metadata.descriptor = datatype_descriptor_; 454 | metadata.reflection = datatype_reflection_; 455 | return metadata; 456 | } 457 | 458 | 459 | // @@protoc_insertion_point(namespace_scope) 460 | 461 | } // namespace Data 462 | 463 | // @@protoc_insertion_point(global_scope) 464 | -------------------------------------------------------------------------------- /msg.pb.h: -------------------------------------------------------------------------------- 1 | // Generated by the protocol buffer compiler. DO NOT EDIT! 2 | // source: msg.proto 3 | 4 | #ifndef PROTOBUF_msg_2eproto__INCLUDED 5 | #define PROTOBUF_msg_2eproto__INCLUDED 6 | 7 | #include 8 | 9 | #include 10 | /* 11 | #if GOOGLE_PROTOBUF_VERSION < 2004000 12 | #error This file was generated by a newer version of protoc which is 13 | #error incompatible with your Protocol Buffer headers. Please update 14 | #error your headers. 15 | #endif 16 | #if 2004001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 17 | #error This file was generated by an older version of protoc which is 18 | #error incompatible with your Protocol Buffer headers. Please 19 | #error regenerate this file with a newer version of protoc. 20 | #endif 21 | */ 22 | #include 23 | #include 24 | #include 25 | #include 26 | // @@protoc_insertion_point(includes) 27 | 28 | namespace Data { 29 | 30 | // Internal implementation detail -- do not call these. 31 | void protobuf_AddDesc_msg_2eproto(); 32 | void protobuf_AssignDesc_msg_2eproto(); 33 | void protobuf_ShutdownFile_msg_2eproto(); 34 | 35 | class datatype; 36 | 37 | // =================================================================== 38 | 39 | class datatype : public ::google::protobuf::Message { 40 | public: 41 | datatype(); 42 | virtual ~datatype(); 43 | 44 | datatype(const datatype& from); 45 | 46 | inline datatype& operator=(const datatype& from) { 47 | CopyFrom(from); 48 | return *this; 49 | } 50 | 51 | inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const { 52 | return _unknown_fields_; 53 | } 54 | 55 | inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() { 56 | return &_unknown_fields_; 57 | } 58 | 59 | static const ::google::protobuf::Descriptor* descriptor(); 60 | static const datatype& default_instance(); 61 | 62 | void Swap(datatype* other); 63 | 64 | // implements Message ---------------------------------------------- 65 | 66 | datatype* New() const; 67 | void CopyFrom(const ::google::protobuf::Message& from); 68 | void MergeFrom(const ::google::protobuf::Message& from); 69 | void CopyFrom(const datatype& from); 70 | void MergeFrom(const datatype& from); 71 | void Clear(); 72 | bool IsInitialized() const; 73 | 74 | int ByteSize() const; 75 | bool MergePartialFromCodedStream( 76 | ::google::protobuf::io::CodedInputStream* input); 77 | void SerializeWithCachedSizes( 78 | ::google::protobuf::io::CodedOutputStream* output) const; 79 | ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const; 80 | int GetCachedSize() const { return _cached_size_; } 81 | private: 82 | void SharedCtor(); 83 | void SharedDtor(); 84 | void SetCachedSize(int size) const; 85 | public: 86 | 87 | ::google::protobuf::Metadata GetMetadata() const; 88 | 89 | // nested types ---------------------------------------------------- 90 | 91 | // accessors ------------------------------------------------------- 92 | 93 | // required int32 port = 1; 94 | inline bool has_port() const; 95 | inline void clear_port(); 96 | static const int kPortFieldNumber = 1; 97 | inline ::google::protobuf::int32 port() const; 98 | inline void set_port(::google::protobuf::int32 value); 99 | 100 | // required string address = 2; 101 | inline bool has_address() const; 102 | inline void clear_address(); 103 | static const int kAddressFieldNumber = 2; 104 | inline const ::std::string& address() const; 105 | inline void set_address(const ::std::string& value); 106 | inline void set_address(const char* value); 107 | inline void set_address(const char* value, size_t size); 108 | inline ::std::string* mutable_address(); 109 | inline ::std::string* release_address(); 110 | 111 | // required string str = 3; 112 | inline bool has_str() const; 113 | inline void clear_str(); 114 | static const int kStrFieldNumber = 3; 115 | inline const ::std::string& str() const; 116 | inline void set_str(const ::std::string& value); 117 | inline void set_str(const char* value); 118 | inline void set_str(const char* value, size_t size); 119 | inline ::std::string* mutable_str(); 120 | inline ::std::string* release_str(); 121 | 122 | // optional fixed64 length = 4; 123 | inline bool has_length() const; 124 | inline void clear_length(); 125 | static const int kLengthFieldNumber = 4; 126 | inline ::google::protobuf::uint64 length() const; 127 | inline void set_length(::google::protobuf::uint64 value); 128 | 129 | // @@protoc_insertion_point(class_scope:Data.datatype) 130 | private: 131 | inline void set_has_port(); 132 | inline void clear_has_port(); 133 | inline void set_has_address(); 134 | inline void clear_has_address(); 135 | inline void set_has_str(); 136 | inline void clear_has_str(); 137 | inline void set_has_length(); 138 | inline void clear_has_length(); 139 | 140 | ::google::protobuf::UnknownFieldSet _unknown_fields_; 141 | 142 | ::std::string* address_; 143 | ::std::string* str_; 144 | ::google::protobuf::uint64 length_; 145 | ::google::protobuf::int32 port_; 146 | 147 | mutable int _cached_size_; 148 | ::google::protobuf::uint32 _has_bits_[(4 + 31) / 32]; 149 | 150 | friend void protobuf_AddDesc_msg_2eproto(); 151 | friend void protobuf_AssignDesc_msg_2eproto(); 152 | friend void protobuf_ShutdownFile_msg_2eproto(); 153 | 154 | void InitAsDefaultInstance(); 155 | static datatype* default_instance_; 156 | }; 157 | // =================================================================== 158 | 159 | 160 | // =================================================================== 161 | 162 | // datatype 163 | 164 | // required int32 port = 1; 165 | inline bool datatype::has_port() const { 166 | return (_has_bits_[0] & 0x00000001u) != 0; 167 | } 168 | inline void datatype::set_has_port() { 169 | _has_bits_[0] |= 0x00000001u; 170 | } 171 | inline void datatype::clear_has_port() { 172 | _has_bits_[0] &= ~0x00000001u; 173 | } 174 | inline void datatype::clear_port() { 175 | port_ = 0; 176 | clear_has_port(); 177 | } 178 | inline ::google::protobuf::int32 datatype::port() const { 179 | return port_; 180 | } 181 | inline void datatype::set_port(::google::protobuf::int32 value) { 182 | set_has_port(); 183 | port_ = value; 184 | } 185 | 186 | // required string address = 2; 187 | inline bool datatype::has_address() const { 188 | return (_has_bits_[0] & 0x00000002u) != 0; 189 | } 190 | inline void datatype::set_has_address() { 191 | _has_bits_[0] |= 0x00000002u; 192 | } 193 | inline void datatype::clear_has_address() { 194 | _has_bits_[0] &= ~0x00000002u; 195 | } 196 | inline void datatype::clear_address() { 197 | if (address_ != &::google::protobuf::internal::kEmptyString) { 198 | address_->clear(); 199 | } 200 | clear_has_address(); 201 | } 202 | inline const ::std::string& datatype::address() const { 203 | return *address_; 204 | } 205 | inline void datatype::set_address(const ::std::string& value) { 206 | set_has_address(); 207 | if (address_ == &::google::protobuf::internal::kEmptyString) { 208 | address_ = new ::std::string; 209 | } 210 | address_->assign(value); 211 | } 212 | inline void datatype::set_address(const char* value) { 213 | set_has_address(); 214 | if (address_ == &::google::protobuf::internal::kEmptyString) { 215 | address_ = new ::std::string; 216 | } 217 | address_->assign(value); 218 | } 219 | inline void datatype::set_address(const char* value, size_t size) { 220 | set_has_address(); 221 | if (address_ == &::google::protobuf::internal::kEmptyString) { 222 | address_ = new ::std::string; 223 | } 224 | address_->assign(reinterpret_cast(value), size); 225 | } 226 | inline ::std::string* datatype::mutable_address() { 227 | set_has_address(); 228 | if (address_ == &::google::protobuf::internal::kEmptyString) { 229 | address_ = new ::std::string; 230 | } 231 | return address_; 232 | } 233 | inline ::std::string* datatype::release_address() { 234 | clear_has_address(); 235 | if (address_ == &::google::protobuf::internal::kEmptyString) { 236 | return NULL; 237 | } else { 238 | ::std::string* temp = address_; 239 | address_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 240 | return temp; 241 | } 242 | } 243 | 244 | // required string str = 3; 245 | inline bool datatype::has_str() const { 246 | return (_has_bits_[0] & 0x00000004u) != 0; 247 | } 248 | inline void datatype::set_has_str() { 249 | _has_bits_[0] |= 0x00000004u; 250 | } 251 | inline void datatype::clear_has_str() { 252 | _has_bits_[0] &= ~0x00000004u; 253 | } 254 | inline void datatype::clear_str() { 255 | if (str_ != &::google::protobuf::internal::kEmptyString) { 256 | str_->clear(); 257 | } 258 | clear_has_str(); 259 | } 260 | inline const ::std::string& datatype::str() const { 261 | return *str_; 262 | } 263 | inline void datatype::set_str(const ::std::string& value) { 264 | set_has_str(); 265 | if (str_ == &::google::protobuf::internal::kEmptyString) { 266 | str_ = new ::std::string; 267 | } 268 | str_->assign(value); 269 | } 270 | inline void datatype::set_str(const char* value) { 271 | set_has_str(); 272 | if (str_ == &::google::protobuf::internal::kEmptyString) { 273 | str_ = new ::std::string; 274 | } 275 | str_->assign(value); 276 | } 277 | inline void datatype::set_str(const char* value, size_t size) { 278 | set_has_str(); 279 | if (str_ == &::google::protobuf::internal::kEmptyString) { 280 | str_ = new ::std::string; 281 | } 282 | str_->assign(reinterpret_cast(value), size); 283 | } 284 | inline ::std::string* datatype::mutable_str() { 285 | set_has_str(); 286 | if (str_ == &::google::protobuf::internal::kEmptyString) { 287 | str_ = new ::std::string; 288 | } 289 | return str_; 290 | } 291 | inline ::std::string* datatype::release_str() { 292 | clear_has_str(); 293 | if (str_ == &::google::protobuf::internal::kEmptyString) { 294 | return NULL; 295 | } else { 296 | ::std::string* temp = str_; 297 | str_ = const_cast< ::std::string*>(&::google::protobuf::internal::kEmptyString); 298 | return temp; 299 | } 300 | } 301 | 302 | // optional fixed64 length = 4; 303 | inline bool datatype::has_length() const { 304 | return (_has_bits_[0] & 0x00000008u) != 0; 305 | } 306 | inline void datatype::set_has_length() { 307 | _has_bits_[0] |= 0x00000008u; 308 | } 309 | inline void datatype::clear_has_length() { 310 | _has_bits_[0] &= ~0x00000008u; 311 | } 312 | inline void datatype::clear_length() { 313 | length_ = GOOGLE_ULONGLONG(0); 314 | clear_has_length(); 315 | } 316 | inline ::google::protobuf::uint64 datatype::length() const { 317 | return length_; 318 | } 319 | inline void datatype::set_length(::google::protobuf::uint64 value) { 320 | set_has_length(); 321 | length_ = value; 322 | } 323 | 324 | 325 | // @@protoc_insertion_point(namespace_scope) 326 | 327 | } // namespace Data 328 | 329 | #ifndef SWIG 330 | namespace google { 331 | namespace protobuf { 332 | 333 | 334 | } // namespace google 335 | } // namespace protobuf 336 | #endif // SWIG 337 | 338 | // @@protoc_insertion_point(global_scope) 339 | 340 | #endif // PROTOBUF_msg_2eproto__INCLUDED 341 | -------------------------------------------------------------------------------- /msg.proto: -------------------------------------------------------------------------------- 1 | package Data; 2 | 3 | message datatype 4 | { 5 | required int32 port = 1; 6 | required string address = 2; //ip 7 | required string str = 3; //for communication 8 | optional fixed64 length = 4; 9 | } 10 | -------------------------------------------------------------------------------- /server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/updata7/protobuf_socket/05b4ada59e015a024520e47e8e6f9e491037afe9/server -------------------------------------------------------------------------------- /server.cc: -------------------------------------------------------------------------------- 1 | #include "dealpack.h" 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | 17 | #define ERR_EXIT(m)\ 18 | do\ 19 | {\ 20 | perror(m);\ 21 | exit(EXIT_FAILURE);\ 22 | }while(0) 23 | 24 | 25 | extern void do_service(int listenfd); 26 | 27 | int main(void) 28 | { 29 | //listening for client 30 | int listenfd = socket(PF_INET, SOCK_STREAM, 0); 31 | if(listenfd == -1) 32 | { 33 | ERR_EXIT("socket"); 34 | } 35 | 36 | struct sockaddr_in servaddr; 37 | memset(&servaddr, 0, sizeof(servaddr)); //init address 38 | servaddr.sin_family = AF_INET; 39 | servaddr.sin_port = htons(5588); //open a port for client 40 | servaddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // open the address for client 41 | 42 | //for bind the address which is being used. 43 | int on = 1; 44 | if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) 45 | { 46 | ERR_EXIT("setsockopt"); 47 | } 48 | 49 | //bind the port and address 50 | if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) 51 | { 52 | ERR_EXIT("bind"); 53 | } 54 | 55 | //listening for client 56 | if(listen(listenfd, SOMAXCONN) == -1) 57 | { 58 | ERR_EXIT("listen"); 59 | } 60 | 61 | do_service(listenfd); 62 | return 0; 63 | } 64 | --------------------------------------------------------------------------------