0% found this document useful (0 votes)
7 views3 pages

Message

This C++ code defines a client that connects to a server on a specified port to request and receive train schedule information. It defines a struct to store train details, and includes functions to decrypt messages from the server and display the train details. The main function establishes a connection to the server, reads commands from the user and sends requests, receives and displays responses, and continues the interactive session until the user enters "quit".

Uploaded by

s2z45t6fsc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Message

This C++ code defines a client that connects to a server on a specified port to request and receive train schedule information. It defines a struct to store train details, and includes functions to decrypt messages from the server and display the train details. The main function establishes a connection to the server, reads commands from the user and sends requests, receives and displays responses, and continues the interactive session until the user enters "quit".

Uploaded by

s2z45t6fsc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

client v5 —> 2 ian

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <arpa/inet.h>
using namespace std;

int PORT;

void elim_reziduri(char *sir)


{
for (int i = 0; i <= strlen(sir) - 1; i++)
{
if (sir[i] == '.')
{
sir[i + 1] = '\0';
break;
}
}
}

struct TrenInfo {
char id_plecare[20];
char ziua[20];
char statie[50];
char id_tren[20];
char destinatie[50];
int ora;
int intarziere;
};

void decripteazaSiAfiseaza(char *mesajCriptat) {

TrenInfo trenInfo;
sscanf(mesajCriptat, "mersul_trenurilorID: %s, Ziua: %s, Statie: %s, Tren: %s,
Destinatie: %s, Ora: %d, Intarziere: %d",
trenInfo.id_plecare, trenInfo.ziua, trenInfo.statie, trenInfo.id_tren,
trenInfo.destinatie, &trenInfo.ora, &trenInfo.intarziere);

std::cout << "ID Plecare: " << trenInfo.id_plecare << std::endl;


std::cout << "Ziua: " << trenInfo.ziua << std::endl;
std::cout << "Statie: " << trenInfo.statie << std::endl;
std::cout << "ID Tren: " << trenInfo.id_tren << std::endl;
std::cout << "Destinatie: " << trenInfo.destinatie << std::endl;
std::cout << "Ora: " << trenInfo.ora << std::endl;
std::cout << "Intarziere: " << trenInfo.intarziere << std::endl;
}

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


{
int socketfd;
sockaddr_in serverAddr;

if (argc != 3)
{
cout << "Sintaxa este : " << argv[0] << " <adresa_server> <port>" << endl;
return -1;
}

PORT = atoi(argv[2]);

socketfd = socket(AF_INET, SOCK_STREAM, 0);


if (socketfd < 0)
{
perror("Eroare la socket().\n");
exit(1);
}

serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr(argv[1]);
serverAddr.sin_port = htons(PORT);

int conn = connect(socketfd, (struct sockaddr *)&serverAddr, sizeof(struct


sockaddr));
if (conn < 0)
{
perror("[client]Eroare la connect().\n");
exit(1);
}

cout << "[client]Conectat la server" << endl;

char buffer[2056];
while (1)
{
cout << "[client] Introduceti un mesaj : ";
cin.getline(buffer, 1024);

int len = strlen(buffer);

if (len > 0 && buffer[len - 1] == '\n')


{
buffer[len - 1] = '\0';
}

cout << "Lungimea bufferului este: " << static_cast<int>(strlen(buffer)) <<


endl;

char raspuns[2056];
raspuns[0] = '\0';

if (strcmp(buffer, "quit") == 0)
{
if (write(socketfd, buffer, len+1) < 0)
{
perror("eroare la scrierea spre server");
exit(1);
}
cout << "Te-ai deconectat." << endl;
break;
}
else if(strcmp(buffer,"mersul_trenurilor")==0)
{
if (write(socketfd, buffer, len+1) < 0)
{
perror("eroare la scrierea spre server");
exit(1);
}
while(1)
{
if (read(socketfd, raspuns, sizeof(raspuns)) < 0)
{
perror("eroare la citirea de la server");
exit(1);
}

if(strcmp(raspuns,"end.")==0)
{
cout<<raspuns<<endl;
break;
}

//elim_reziduri(raspuns);
cout<<raspuns<<endl;
raspuns[0] = '\0';
}
}
else{
if (write(socketfd, buffer, len+1) < 0)
{
perror("eroare la scrierea spre server");
exit(1);
}
if (read(socketfd, raspuns, sizeof(raspuns)) < 0)
{
perror("eroare la citirea de la server");
exit(1);
}
elim_reziduri(raspuns);
cout << "Raspunsul de la server este : " << raspuns << endl;
raspuns[0] = '\0';
}

//decripteazaSiAfiseaza(raspuns);

return 0;
}

You might also like