-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraX.c
74 lines (57 loc) · 1.76 KB
/
TraX.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#define BUFFER_SIZE 1024
void *receive_messages(void *arg);
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <server_ip>\n", argv[0]);
return -1;
}
char *server_ip = argv[1];
int client_socket;
struct sockaddr_in server_addr;
pthread_t tid;
char buffer[BUFFER_SIZE];
printf("Enter your username: ");
char username[32];
fgets(username, 32, stdin);
username[strcspn(username, "\n")] = 0;
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation error");
return -1;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(57644);
if (inet_pton(AF_INET, server_ip, &server_addr.sin_addr) <= 0) {
perror("Invalid address or address not supported");
return -1;
}
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Connection failed");
return -1;
}
pthread_create(&tid, NULL, receive_messages, (void*)&client_socket);
while (1) {
fgets(buffer, BUFFER_SIZE, stdin);
buffer[strcspn(buffer, "\n")] = 0;
char message[BUFFER_SIZE + 32];
sprintf(message, "%s: %s", username, buffer);
send(client_socket, message, strlen(message), 0);
}
close(client_socket);
return 0;
}
void *receive_messages(void *arg) {
int client_socket = *(int*)arg;
char buffer[BUFFER_SIZE];
int bytes_received;
while ((bytes_received = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0) {
buffer[bytes_received] = '\0';
printf("%s\n", buffer);
}
return NULL;
}