The server uses PHP and the client uses Java to communicate with each other through protobuf [original]

Source: Internet
Author: User

Let me record the journal account (only "sent by the server, displayed after the client reads "):

I. server-side environment setup (based on PHP)

1. The server construction work is skipped. The Environment version is listed as follows:

PHP version 5.3.2-1, Apache/2.2.14, ubuntu10.04 lts

2. Because Google's protobuf only supports three languages, PHP is not included yet, and only third-party download is supported:

Http://code.google.com/p/pb4php/

3. decompress the zip package to/var/www/protocolbuf.

---------- >>>>>> Yes, you are not mistaken. This is the end of the process. Done. As long as your PHP job is okay, the protobuf PHP environment will be ready.

 

2. Build the client environment (based on Java)

1. The operating system I use is Ubuntu 10.04 lts.

2. Of course, you must install Java and eclipse

Version: Helios Service Release 1, Java-6-sun-1.6.0.24

3. Download protobuf official version

Http://code.google.com/p/protobuf/downloads/list

4. decompress the package to a specific path, such ~ /Protobuf

5. Follow ~ The instructions in/protobuf/install.txt compile protobuf in three steps (don't forget, if C ++ does not support it, you can use apt-Get install g ++)

Go ~ In/protobuf, execute make, make check, and make install successively.

6. Note that the compiled protoc is in/usr/local/bin, you can use/usr/local/bin/protoc -- version to compare the version number of the ZIP file you downloaded.

7. If an error occurs, such as "error while loading shared libraries: libprotobuf. so.7: cannot open shared object file: no such file or directory ". In fact, this file cannot be found in/usr/lib, actually there are some in/usr/local/lib. We can establish a soft connection to "sudo ln-S/usr/local/lib/libprotobuf. so.7.0.0/usr/lib/libprotobuf. so.7"

 

Iii. server and client test code

1. Start with the client's

Edit your own proto file, such as pictures_protos.proto, go to the directory where pictures_protos.proto is located, and compile it with the following command

/Usr/local/bin/protc-I =./-- java_out =./pictures_protos.proto

 

Pictures_protos.proto

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

Package test. PROTOS;

Message picture {
Required string filename = 1;
Required string relpath = 2;
Required int32 clicks = 3;
}

Message pictures {
Repeated picture PIC = 1;
}

<<

 

Test. Java

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

......

Urlconnection conn = getconnection ("http: // localhost/protobuftest. php ");
Try {
Conn. Connect ();
Inputstream is = conn. getinputstream ();
Pictures urlpics = pictures. parsefrom (is );
For (picture urlpic: urlpics. getpiclist ()){
System. Out. println ("filename:" + urlpic. getfilename ());
System. Out. println ("relpath:" + urlpic. getrelpath ());
System. Out. println ("clicks:" + urlpic. getclicks ());
System. Out. println ("~~~~~~~~~~~~~~~~~~~~~~~ ");
}
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

......

Public urlconnection getconnection (string Surl ){
URL url = NULL;
Try {
Url = new URL (Surl );
} Catch (malformedurlexception e ){
// Todo auto-generated Catch Block
System. Out. println ("debugtag" + "remtoe image exception" + E. tostring ());
E. printstacktrace ();
Return NULL;
}

Urlconnection conn = NULL;
Try {
Conn = URL. openconnection ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
System. Out. println ("debugtag" + "remtoe image exception" + E. tostring ());
E. printstacktrace ();
Return NULL;
}
Try {
Conn. Connect ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
System. Out. println ("debugtag" + "remtoe image exception" + E. tostring ());
E. printstacktrace ();
Return NULL;
}

Return conn;
}

......

<<

 

2. Let's talk about the server side.

We noticed that a page named protobuftest. php is to be accessed in test. java. This PHP file is like this:

First, create a firstparser under/var/www/protocolbuf/parser. and copy the pictures_protos.proto file to a directory, for example, firstparser. this directory of PHP is to modify pictures_protos.proto for normal compilation. Because the package keyword is not supported during PHP compilation, We Need To comment out the Directory and execute PHP firstparser. PHP, pb_proto_pictures_protos.php will be generated in the same directory. Finally, we can create a protobuftest under/var/www. PHP is used to communicate with the client.

 

Firstparser. php

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

Require_once ("./pb_parser.php ");
$ Parser = new pbparser ();
$ Parser-> parse ("./pictures_protos.proto ");
Echo "done/N ";

<<

 

Protobuftest. php

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>

Require_once ("./protocolbuf/message/pb_message.php ");
Require_once ("./protocolbuf/parser/pb_proto_pictures_protos.php ");

$ Pics = new pictures ();
$ PIC = $ pics-> add_pic ();
$ Pic-> set_filename ("fn01 ");
$ Pic-> set_relpath ("rp01 ");
$ Pic-> set_clicks (1 );
$ PIC = $ pics-> add_pic ();
$ Pic-> set_filename ("fn02 ");
$ Pic-> set_relpath ("rp02 ");
$ Pic-> set_clicks (2 );
$ PIC = $ pics-> add_pic ();
$ Pic-> set_filename ("fn03 ");
$ Pic-> set_relpath ("rp03 ");
$ Pic-> set_clicks (3 );
$ PIC = $ pics-> add_pic ();
$ Pic-> set_filename ("file name 04 ");
$ Pic-> set_relpath ("path name 04 ");
$ Pic-> set_clicks (4 );
$ PIC = $ pics-> add_pic ();
$ Pic-> set_filename ("file name 05 ");
$ Pic-> set_relpath ("pathname 05 ");
$ Pic-> set_clicks (5 );
$ PIC = $ pics-> add_pic ();
$ Pic-> set_filename ("fn06 ");
$ Pic-> set_relpath ("rp06 ");
$ Pic-> set_clicks (6 );
$ PIC = $ pics-> add_pic ();
$ Pic-> set_filename ("fn07 ");
$ Pic-> set_relpath ("rp07 ");
$ Pic-> set_clicks (7 );

$ String = $ pics-> serializetostring ();

Echo $ string;

<<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.