C++ server:linux, protoc -I=./ --cpp_out=./ Infor.proto
Java client:包含protobuf-java-2.5.0.jar包, protoc.exe --java_out=./ Infor.proto
Infor.proto文件:
1 message my_message{ 2 required string startedTime =1; 3 required string version=2; 4 required double configuredCapacity=3; 5 required double dfsUsed =4; 6 required int32 fileNum=5; 7 required int32 replicatedFilesNum =6; 8 required int32 blockNum =7; 9 required int32 livedNodeNum =8; 10 required int32 decommissioningNodeNum=9; 11 }
Server.cpp
1 #include <netinet/in.h> // for sockaddr_in 2 #include <sys/types.h> // for socket 3 #include <sys/socket.h> // for socket 4 #include <unistd.h> 5 #include <stdio.h> // for printf 6 #include <stdlib.h> // for exit 7 #include <string.h> // for bzero 8 #include <string> 9 #include <google/protobuf/message_lite.h> 10 #include <google/protobuf/io/coded_stream.h> 11 #include <google/protobuf/io/zero_copy_stream_impl_lite.h> 12 #include "infor.pb.h" 13 14 #define HELLO_WORLD_SERVER_PORT 8000 15 #define LENGTH_OF_LISTEN_QUEUE 20 16 17 int main() 18 { 19 std::string time = "2015-06-25"; 20 std::string version = "0.0.1"; 21 double config = 2.0; 22 double dfs = 3.0; 23 int file = 1000; 24 int rep = 1000; 25 int block = 1000; 26 int live = 1000; 27 int de = 1000; 28 29 struct sockaddr_in server_addr; 30 bzero(&server_addr,sizeof(server_addr)); 31 server_addr.sin_family = AF_INET; 32 server_addr.sin_addr.s_addr = htons(INADDR_ANY); 33 server_addr.sin_port = htons(HELLO_WORLD_SERVER_PORT); 34 35 int server_socket = socket(PF_INET,SOCK_STREAM,0); 36 if( server_socket < 0) 37 { 38 printf("Create Socket Failed!"); 39 exit(1); 40 } 41 { 42 int opt =1; 43 setsockopt(server_socket,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)); 44 } 45 46 if( bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr))) 47 { 48 printf("Server Bind Port : %d Failed!", HELLO_WORLD_SERVER_PORT); 49 exit(1); 50 } 51 52 if ( listen(server_socket, LENGTH_OF_LISTEN_QUEUE) ) 53 { 54 printf("Server Listen Failed!"); 55 exit(1); 56 } 57 while (true) 58 { 59 struct sockaddr_in client_addr; 60 socklen_t length = sizeof(client_addr); 61 62 int new_server_socket = accept(server_socket,(struct sockaddr*)&client_addr,&length); 63 if ( new_server_socket < 0) 64 { 65 printf("Server Accept Failed!\n"); 66 break; 67 } 68 69 my_message mm; 70 mm.set_startedtime(time); 71 mm.set_version(version); 72 mm.set_configuredcapacity(config); 73 mm.set_dfsused(dfs); 74 mm.set_filenum(file); 75 mm.set_replicatedfilesnum(rep); 76 mm.set_blocknum(block); 77 mm.set_livednodenum(live); 78 mm.set_decommissioningnodenum(de); 79 file += 1; rep += 1; block += 1; live += 1; de += 1; 80 81 int len = mm.ByteSize() + 4; 82 char *buffer = new char[len]; 83 84 google::protobuf::io::ArrayOutputStream arrayOut(buffer, len); 85 google::protobuf::io::CodedOutputStream codedOut(&arrayOut); 86 87 codedOut.WriteVarint32(mm.ByteSize()); 88 89 //write protobuf my_message mm to CodedOutputStream 90 mm.SerializeToCodedStream(&codedOut); 91 92 if(send(new_server_socket,buffer,len,0)<0) 93 { 94 printf("Send Failed\n"); 95 break; 96 } 97 98 close(new_server_socket); 99 delete buffer; 100 } 101 close(server_socket); 102 return 0; 103 }
Client:
1 import java.io.BufferedReader; 2 import java.io.DataInputStream; 3 import java.io.DataOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 import java.net.Socket; 8 9 import java.io.ByteArrayInputStream; 10 import java.io.ByteArrayOutputStream; 11 import java.io.IOException; 12 import java.util.List; 13 14 import com.google.protobuf.CodedInputStream; 15 16 public class Client { 17 public static final String IP_ADDR = "127.0.0.1"; //server ip 18 public static final int PORT = 8000; //port 19 20 public static void main(String[] args) throws InterruptedException { 21 System.out.println("client starting..."); 22 23 while (true) { 24 Thread.sleep(1000); 25 Socket socket = null; 26 try { 27 socket = new Socket(IP_ADDR, PORT); 28 29 InputStream input = socket.getInputStream(); 30 try{ 31 // 反序列化 32 Infor.my_message mm = Infor.my_message.parseDelimitedFrom(input); 33 34 System.out.println(); 35 System.out.println(); 36 System.out.println(); 37 System.out.println("服务器端返回过来的是: "); 38 System.out.println("startedTime:" + mm.getStartedTime()); 39 System.out.println("version:" + mm.getVersion()); 40 System.out.println("configuredCapacity:" + mm.getConfiguredCapacity()); 41 System.out.println("dfsUsed:" + mm.getDfsUsed()); 42 System.out.println("fileNum:" + mm.getFileNum()); 43 System.out.println("replicatedFilesNum:" + mm.getReplicatedFilesNum()); 44 System.out.println("blockNum:" + mm.getBlockNum()); 45 System.out.println("livedNodeNum:" + mm.getLivedNodeNum()); 46 System.out.println("decommissioningNodeNum:" + mm.getDecommissioningNodeNum()); 47 } catch(Exception e) 48 { 49 System.out.println(e.toString()); 50 System.out.println("catch e"); 51 break; 52 } 53 input.close(); 54 } catch (Exception e) { 55 System.out.println("客户端异常:" + e.getMessage()); 56 } finally { 57 if (socket != null) { 58 try { 59 socket.close(); 60 } catch (IOException e) { 61 socket = null; 62 System.out.println("客户端 finally 异常:" + e.getMessage()); 63 } 64 } 65 } 66 } 67 } 68 }
运行结果:
参考链接&可能遇到的问题:
http://blog.csdn.net/xiao__gui/article/details/36643949
http://www.javased.com/?api=com.google.protobuf.CodedInputStream
https://webcache.googleusercontent.com/search?q=cache:0T1W5ECY5vMJ:blog.ajhodges.com/2011/10/cross-platform-communication-using.html+&cd=2&hl=zh-CN&ct=clnk
http://stackoverflow.com/questions/26655733/protobuf-codedinputstream-parsing-partial-messages
http://stackoverflow.com/questions/14272852/trying-to-send-and-receive-message-using-protobuf-in-java-but-got-error-protoco
https://famellee.wordpress.com/2013/04/29/transmit-multiple-google-protocol-buffers-messages-between-java-and-c/