Linux under Qt+googleprotobuf+socket

Source: Internet
Author: User
Tags sendmsg htons

When I was ready to graduate recently, I kept busy. Internship in the company with the elder brother and strong brother contacted Googleprotobuf, feel this thing is a good thing, but at that time, Net Dragon Company to the bottom are done, contact is directly call their good interface. I do not know the socket and other network programming, and now ready to go to Shanghai to find a job, he pondered the next.

For personal reasons, I prefer the Linux system, I chose the Linux platform Qt to do this demo. Installing things with Linux is also a lot easier.

First, let's install Googleprotobuf.

wget http://protobuf.googlecode.com/files/protobuf-your version number. tar.gz

Tar zxvf protobuf-your version number. tar.gz

CD protobuf-your version number

./configure--prefix=/usr/

Make

sudo make install

/sbin/ldconfig-v

The last command is to avoid problems with the library when you use the PROTOC command.

Now let's write a proto file named: Qtpeople.proto, which reads as follows:

Packageqtpeople;

Messagepeople

{

Required String name=1;

Required Int32 id=2;

Required String passwd=3;

}

Now we need to generate the H and CC files with the following command:

Protoc-i=. --cpp_out=./Qtpeople.proto (the parameter meaning of this command manprotoc found)

QTPeople.pb.cc

Qtpeople.pb.h

Qtpeople.proto
Now we use QT to create two new projects, named: Qtprotoc, Qtprotos, and add Libs +=-l/usr/lib-lprotobuf in the pro file,

Because this is the demo of the test, I will directly use the project generated main file, in the main file include Qtpeople.pb.h

File.

The contents of the main file of Qtprotoc are as follows:

#include <QCoreApplication>

/*

-----know how to be silent------

*/

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

#include <sys/socket.h>

#include <sys/wait.h>

#include <iostream>

#include <QDebug>

#include "Qtpeople.pb.h"

#define MAXSIZE 1024

#define SERVERIP "127.0.0.1"

#define ServerPort 2236

#define DATA "This is guest"

using namespace Std;

void Client ()

{

int mysock,recvbytes;

string data;

Char Buf[maxsize];

struct sockaddr_in serveraddr;

Qtpeople::P eople peo;

PEO.SET_ID (1002);

Peo.set_name ("Def");

PEO.SET_PASSWD ("1234567");

Peo. Serializetostring (&data);

Char Sendmsg[maxsize];

strcpy (Sendmsg,data.c_str ());

if ((Mysock=socket (af_inet,sock_stream,0)) ==-1)

{

Qdebug () << "Sockt error";

Exit (-1);

}

Bzero (&serveraddr,sizeof (SERVERADDR));

Serveraddr.sin_family=af_inet;

Serveraddr.sin_port=htons (ServerPort);

SERVERADDR.SIN_ADDR.S_ADDR=INET_ADDR (ServerIP);

if (Connect (mysock, (struct sockaddr *) &serveraddr,sizeof (struct sockaddr)) = =-1)

{

Qdebug () << "Connect error";

Exit (-1);

}

Write (mysock,sendmsg,sizeof (sendmsg));

if ((Recvbytes=recv (mysock,buf,maxsize,0)) ==-1)

{

Qdebug () << "received error";

Exit (-1);

}

Qdebug () << "sucess";

buf[recvbytes]= ' + ';

String Getmsg=buf;

cout<<getmsg<<endl;

BOOL Isget=peo. Parsefromstring (getmsg);

Qdebug () <<isGet;

cout<< "id=" <<peo.id () <<endl;

cout<< "Name=" <<peo.name () <<endl;

cout<< "passwd=" <<peo.passwd () <<endl;

Close (Mysock);

Google::p rotobuf::shutdownprotobuflibrary ();

}

int main (int argc, char *argv[])

{

Qcoreapplication A (argc, argv);

Client ();

return A.exec ();

}

The contents of the Qtprotosmain file are as follows:

#include <QCoreApplication>

/*

-----know how to be silent------

*/

#include <stdio.h>

#include <stdlib.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

#include <sys/socket.h>

#include <sys/wait.h>

#include <iostream>

#include <QDebug>

#include "Qtpeople.pb.h"

#define Servport 2236

#define BACKLOG 10

#define MAXSIZE 1024

using namespace Std;

void Server () {

int SOCKFD, CLIENT_FD;

string data;

struct sockaddr_in my_addr;

struct sockaddr_in remote_addr;

Qtpeople::P eople peo;

PEO.SET_ID (1001);

Peo.set_name ("abc");

PEO.SET_PASSWD ("7654321");

Peo. Serializetostring (&data);

Char Sendmsg[maxsize];

strcpy (Sendmsg,data.c_str ());

Creating sockets

if ((SOCKFD = socket (af_inet, sock_stream, 0)) ==-1) {

Perror ("Socket create failed!");

Exit (1);

}

Bound Port Address

my_addr.sin_family = af_inet;

My_addr.sin_port = htons (Servport);

MY_ADDR.SIN_ADDR.S_ADDR = Inaddr_any;

Bzero (& (My_addr.sin_zero), 8);

if (Bind (SOCKFD, (struct sockaddr*) &my_addr, sizeof (my_addr)) = =-1) {

Perror ("Bind error!");

Exit (1);

}

Listening port

if (Listen (SOCKFD, BACKLOG) = =-1) {

Perror ("Listen error");

Exit (1);

}

Qdebug () << "before while OK";

while (1) {

Qdebug () << "while first OK";

socklen_t sin_size = sizeof (REMOTE_ADDR);

Qdebug () << "before accept OK";

/* if ((client_fd = Accept (SOCKFD, (struct sockaddr *) &remote_addr,&sin_size)) {

Perror ("Accept error!");

Continue

}

*/

Client_fd=accept (SOCKFD, (struct sockaddr *) &remote_addr,&sin_size);

Qdebug () <<client_fd;

if (client_fd==-1)

{

Perror ("Accept error");

Continue

}

Qdebug () << "Accept OK";

CLIENT_FD = Accept (SOCKFD, (struct sockaddr *) &remote_addr,&sin_size);

printf ("Received a connection from%s\n", (char*) Inet_ntoa (REMOTE_ADDR.SIN_ADDR));

Qdebug () << "before fork OK";

Child process Segments

if (!fork ()) {

Qdebug () << "fork OK";

Accept instructions sent by the client

int rval;

Char Buf[maxsize];

if ((Rval = Read (CLIENT_FD, buf, MAXSIZE)) < 0) {

Perror ("Reading stream error!");

Continue

}

Qdebug () << "read OK";

buf[maxsize]= ' + ';

String Getmsg=buf;

BOOL Isget=peo. Parsefromstring (getmsg);

Qdebug () <<isGet;

cout<< "id=" <<peo.id () <<endl;

cout<< "Name=" <<peo.name () <<endl;

cout<< "passwd=" <<peo.passwd () <<endl;

Send a message to the client

if (Send (Client_fd,sendmsg, strlen (sendmsg), 0) = =-1)

Perror ("Send error!");

Close (CLIENT_FD);

Exit (0);

}

Close (CLIENT_FD);

}

Close (SOCKFD);

}

int main (int argc, char *argv[])

{

Qcoreapplication A (argc, argv);

Server ();

return A.exec ();

}

Linux under Qt+googleprotobuf+socket

Related Article

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.