-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
84 lines (63 loc) · 2.23 KB
/
Copy pathclient.c
File metadata and controls
84 lines (63 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#define PORT 8005
// Client task function executed by each thread
void *client_task(void *arg) {
int sock = 0;
struct sockaddr_in serv_addr;
const char *hello = "Hello from client";
char buffer[1024] = {0};
pthread_t this_id = pthread_self(); // Getting thread ID in C (using pthread_self())
// Create socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Thread %ld: Socket creation error\n", (long)this_id);
return NULL;
}
// Set server address
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("Thread %ld: Invalid address / Address not supported\n", (long)this_id);
return NULL;
}
// Connect to the server
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("Thread %ld: Connection failed\n", (long)this_id);
return NULL;
}
// Send a message to the server (if needed)
// send(sock, hello, strlen(hello), 0);
// printf("Thread %ld: Hello message sent\n", (long)this_id);
// Read response from server
read(sock, buffer, 1024);
printf("Thread %ld: Message received: %s\n", (long)this_id, buffer);
// Close the socket
close(sock);
return NULL;
}
int main(int argc, char const *argv[]) {
if (argc != 2) {
printf("Usage: %s <number_of_clients>\n", argv[0]);
return -1;
}
// Number of clients specified as command-line argument
int num_clients = atoi(argv[1]);
// Allocate memory for the threads
pthread_t *threads = (pthread_t *)malloc(num_clients * sizeof(pthread_t));
// Create the specified number of threads
for (int i = 0; i < num_clients; i++) {
pthread_create(&threads[i], NULL, client_task, NULL);
}
// Join the threads to ensure the main function waits for their completion
for (int i = 0; i < num_clients; i++) {
pthread_join(threads[i], NULL);
}
// Free allocated memory
free(threads);
return 0;
}