Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #define PORT 8080
- int main() {
- int client_socket;
- struct sockaddr_in server_addr;
- char buffer[1024] = {0};
- // Creare socket
- if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- perror("Socket creation failed");
- exit(EXIT_FAILURE);
- }
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(PORT);
- // Conectare la server
- if (inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr) <= 0) {
- perror("Invalid address/ Address not supported");
- exit(EXIT_FAILURE);
- }
- if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
- perror("Connection failed");
- exit(EXIT_FAILURE);
- }
- // Primirea meniului de la server
- if (recv(client_socket, buffer, sizeof(buffer), 0) <= 0) {
- perror("Receive failed");
- exit(EXIT_FAILURE);
- }
- printf("Meniu:\n%s\n", buffer);
- printf("Alege optiune: ");
- int choice;
- scanf("%d", &choice);
- sprintf(buffer, "%d", choice);
- send(client_socket, buffer, strlen(buffer), 0);
- // Recepționarea și afișarea informațiilor de la server
- while (recv(client_socket, buffer, sizeof(buffer), 0) > 0) {
- printf("%s", buffer);
- }
- close(client_socket);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <pthread.h>
- #define PORT 8080
- #define MAX_CLIENTS 5
- void *handle_client(void *arg) {
- int client_socket = *((int *)arg);
- char menu[] = "1. Specificatii CPU\n2. Specificatii Memorie\n3. Listare dispozitive PCI\nAlegere: ";
- char buffer[1024] = {0};
- int choice;
- // Trimitem meniul către client
- send(client_socket, menu, strlen(menu), 0);
- // Recepționăm opțiunea de la client
- if (recv(client_socket, buffer, sizeof(buffer), 0) <= 0) {
- perror("Receive failed");
- close(client_socket);
- pthread_exit(NULL);
- }
- choice = atoi(buffer);
- // Verificăm opțiunea aleasă de client și trimitem informațiile corespunzătoare
- FILE *fp;
- switch (choice) {
- case 1:
- fp = popen("cat /proc/cpuinfo", "r");
- break;
- case 2:
- fp = popen("cat /proc/meminfo", "r");
- break;
- case 3:
- fp = popen("lspci", "r");
- break;
- default:
- printf("Optiune invalida.\n");
- close(client_socket);
- pthread_exit(NULL);
- }
- // Citim informațiile din comandă și le trimitem clientului
- while (fgets(buffer, sizeof(buffer), fp) != NULL) {
- send(client_socket, buffer, strlen(buffer), 0);
- }
- pclose(fp);
- close(client_socket);
- pthread_exit(NULL);
- }
- int main() {
- int server_socket, client_socket, addr_size;
- struct sockaddr_in server_addr, client_addr;
- pthread_t tid[MAX_CLIENTS];
- // Creare socket
- if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
- perror("Socket creation failed");
- exit(EXIT_FAILURE);
- }
- // Setare structura de adresa a serverului
- server_addr.sin_family = AF_INET;
- server_addr.sin_addr.s_addr = INADDR_ANY;
- server_addr.sin_port = htons(PORT);
- // Legare
- if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
- perror("Binding failed");
- exit(EXIT_FAILURE);
- }
- // Ascultare pentru conexiuni
- if (listen(server_socket, MAX_CLIENTS) < 0) {
- perror("Listening failed");
- exit(EXIT_FAILURE);
- }
- printf("Serverul este activ. Astept clienti...\n");
- int i = 0;
- while (1) {
- addr_size = sizeof(client_addr);
- // Acceptare conexiune
- if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, (socklen_t *)&addr_size)) < 0) {
- perror("Acceptance failed");
- exit(EXIT_FAILURE);
- }
- printf("Clientul conectat\n");
- // Creare fir de execuție pentru fiecare client
- if (pthread_create(&tid[i++], NULL, handle_client, (void *)&client_socket) != 0) {
- perror("Thread creation failed");
- exit(EXIT_FAILURE);
- }
- // Limitare la MAX_CLIENTS
- if (i >= MAX_CLIENTS) {
- i = 0;
- while (i < MAX_CLIENTS) {
- pthread_join(tid[i++], NULL);
- }
- i = 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement