-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
145 lines (106 loc) · 3.51 KB
/
client.c
File metadata and controls
145 lines (106 loc) · 3.51 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Created by pranath on 7/9/19.
//
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <pthread.h>
#include <inttypes.h>
bool server_listener = true;
void *reader(void *arg) {
int connected_socket = *((int *)arg);
int receive_message_size = 0;
char msg_size_buffer[5];
while( (receive_message_size = recv(connected_socket , msg_size_buffer , sizeof(msg_size_buffer) , 0)) > 0 ) {
if ( receive_message_size <= 0 ) {
perror("Error Receiving the Message\n");
}
int msg_size[5]={msg_size_buffer[0] - '0', msg_size_buffer[1] - '0', msg_size_buffer[2] - '0', msg_size_buffer[3] - '0', msg_size_buffer[4] - '0'};
char msg_size_str[6];
int i=0;
int index = 0;
for (i=0; i<5; i++) {
index += snprintf(&msg_size_str[index], 6-index, "%d", msg_size[i]);
}
memset(msg_size_buffer, 0, 5);
char* strtoumax_endptr;
int msg_size_int = strtoumax(msg_size_str, &strtoumax_endptr, 10);
char *buffer = NULL;
buffer = malloc( (msg_size_int + 27)*sizeof(char) + 1);
int receive_message_content_size = recv(connected_socket, buffer, (msg_size_int + 27)*sizeof(char), 0);
if (receive_message_content_size <= 0) {
perror("Error with message");
}
printf("%s\n", buffer);
memset(buffer, 0, (msg_size_int + 27)* sizeof(char));
free(buffer);
}
}
char *input_string(FILE* fp, size_t size){
char *str = NULL;
int ch;
size_t len = 0;
str = malloc(sizeof(char)*size);
if(!str) {
return str;
}
while(EOF != (ch = fgetc(fp)) && ch != '\n'){
str[len++] = ch;
if(len == size){
str = realloc(str, sizeof(char)*(size += 16));
if(!str)return str;
}
}
str[len++] = '\0';
return realloc(str, sizeof(char)*len);
}
int main() {
char message[2000];
// Create a socket
int network_socket;
network_socket = socket(AF_INET, SOCK_STREAM, 0);
if (network_socket == -1) {
perror("Error in creating the network socket");
return 0;
}
printf("Network Socket was Created Successfully!\n");
// Specify an address for the socket
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9002);
server_address.sin_addr.s_addr = INADDR_ANY;
int connection_status = connect (network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
if (connection_status == -1) {
perror("There was a connection error");
return 0;
}
printf("Connection created successfully!\n");
pthread_t reader_thread_id;
pthread_create(&reader_thread_id, NULL, reader, &network_socket);
while(server_listener) {
// scanf("%s", message);
char *m;
char msg_size_str[6];
char *full_msg;
m = input_string(stdin, 10);
int strlen_msg = strlen(m) + 27;
sprintf(msg_size_str, "%05d", strlen_msg);
full_msg = malloc(sizeof(char)*(strlen(m) + 27) + 1);
strcpy(full_msg, msg_size_str);
strcat(full_msg, m);
free(m);
memset(msg_size_str, 0, 6);
if(strlen_msg != 27) {
send(network_socket, full_msg, strlen(full_msg), 0);
free(full_msg);
}
usleep(100000);
}
close(network_socket);
return 0;
}