Skip to content

Instantly share code, notes, and snippets.

@alimovlex
Last active August 12, 2024 15:58
Show Gist options
  • Select an option

  • Save alimovlex/a7cd6dbec25482f81330851ab058fcfb to your computer and use it in GitHub Desktop.

Select an option

Save alimovlex/a7cd6dbec25482f81330851ab058fcfb to your computer and use it in GitHub Desktop.
A simple TCP C++ client that sends a serialized message to the server on Enter pressed.
/*
* Copyright (C) 2024 Recompile.me.
* All rights reserved.
*/
#include <iostream>
#include <string>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <cstdint>
#include <netdb.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <fstream>
#include <thread>
#include <vector>
#include "person.pb.h"
using namespace std;
int client() {
char *serverIp = "127.0.0.1";
int port = 8080;
//create a message buffer
char msg[1500];
//setup a socket and connection tools
struct hostent* host = gethostbyname(serverIp);
sockaddr_in sendSockAddr;
bzero((char*)&sendSockAddr, sizeof(sendSockAddr));
sendSockAddr.sin_family = AF_INET;
sendSockAddr.sin_addr.s_addr =
inet_addr(inet_ntoa(*(struct in_addr*)*host->h_addr_list));
sendSockAddr.sin_port = htons(port);
int clientSd = socket(AF_INET, SOCK_STREAM, 0);
//try to connect...
int status = connect(clientSd,
(sockaddr*) &sendSockAddr, sizeof(sendSockAddr));
if(status < 0)
{
cout<<"Error connecting to socket!"<<endl;
return -1;
}
cout << "Connected to the server!" << endl;
int bytesRead, bytesWritten = 0;
struct timeval start1, end1;
gettimeofday(&start1, NULL);
while(1)
{
cout << ">";
string data;
PERSON::Person proto_message;
proto_message.set_name("James Brown");
proto_message.set_id(7777000);
proto_message.set_email("[email protected]");
//string binary;
proto_message.SerializeToString(&data);
vector<uint8_t> binary_message(data.begin(), data.end());
//proto_message.SerializeToArray(binary_message.data(), binary_message.max_size());
cout << "The binary message size to send: " << proto_message.ByteSizeLong() << "bytes" << endl;
cout << proto_message.Utf8DebugString() << endl;
cout << "The serialized binary message: " << endl;
for (auto byte: binary_message) {
cout << hex << setw(2) << setfill('0') << static_cast<int>(byte);
}
cout << endl;
cout << "Press Enter to send the default protobuf message..." << endl;
cin.get();
memset(&msg, 0, sizeof(msg));//clear the buffer
strcpy(msg, data.c_str());
if(data == "exit")
{
send(clientSd, (char*)&msg, strlen(msg), 0);
break;
}
bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0);
cout << "Awaiting server response..." << endl;
memset(&msg, 0, sizeof(msg));//clear the buffer
bytesRead += recv(clientSd, (char*)&msg, sizeof(msg), 0);
if(!strcmp(msg, "exit"))
{
cout << "Server has quit the session" << endl;
break;
}
cout << "Server: " << msg << endl;
}
gettimeofday(&end1, NULL);
close(clientSd);
cout << "********Session********" << endl;
cout << "Bytes written: " << bytesWritten <<
" Bytes read: " << bytesRead << endl;
cout << "Elapsed time: " << (end1.tv_sec- start1.tv_sec)
<< " secs" << endl;
cout << "Connection closed" << endl;
return 0;
}
int main() {
GOOGLE_PROTOBUF_VERIFY_VERSION;
client();
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment