-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
206 lines (167 loc) · 5 KB
/
server.c
File metadata and controls
206 lines (167 loc) · 5 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<pthread.h>
#include <math.h>
#include <sys/stat.h>
struct thread_args {
int *soc;
FILE *fileptr;
int chars_toread;
};
void *sendChunks(void *);
int main(int argc , char *argv[])
{
struct thread_args *args = (struct thread_args *)malloc(sizeof(struct thread_args));
int choice;
printf("Do you want to send text, image or video file?\n");
printf("Enter 1 for text, 2 for image and 3 for video:");
scanf("%d", &choice);
FILE *readptr;
if (choice == 1){
readptr= fopen("textfileSent.txt", "rb");
}
else if (choice == 2)
{
readptr = fopen("imageSent.jpg","rb");
}
else if (choice == 3)
{
readptr = fopen("vidSent.mp4","rb");
}
int socket_desc , new_socket , c,i ;
int start = 0;
int *new_sock;
struct sockaddr_in server , client;
fseek(readptr, 0, SEEK_END);
int size = ftell(readptr);
fseek(readptr, 0, SEEK_SET);
args->fileptr = readptr;
int num_of_threads;
int read_chars = 0;
// Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
// Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons( 8080 );
// Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("[-]Bind failed");
return 1;
}
puts("[+]Bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
// ##
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("[-] Accept failed");
return 1;
}
printf("[+] Connection accepted\n");
// receiving threads
if(recv(new_socket, &num_of_threads, sizeof(num_of_threads), 0)<=0)
{
printf("[-]Value not received.");
exit(1);
}
printf("[+] Threads Number Received: %d.\n", num_of_threads);
close(new_socket);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("[-] Accept failed");
return 1;
}
printf("[+] Connection accepted\n");
// sending file size
if(send(new_socket, &size, sizeof(size), 0)== -1)
{
perror("[-] Error in sending count.\n");
exit(1);
}
printf("[+] Filesize sent successfully: %d. \n", size);
close(new_socket);
// creating chunks
int chunk = ceil((float) size / num_of_threads);
pthread_t threads[num_of_threads];
args -> chars_toread = chunk;
for (int t = 0; t< num_of_threads;t++)
{
char buffer[chunk + 1];
read_chars = fread(buffer, 1,chunk,readptr);
}
fseek(readptr, 0, SEEK_SET);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("[-] Accept failed");
return 1;
}
printf("[+] Connection accepted\n");
// sending end chars
if(send(new_socket, &read_chars, sizeof(read_chars), 0)== -1)
{
perror("[-] Error in sending end chars.\n");
exit(1);
}
close(new_socket);
// ##
// multi threading
for (i = 0; i < num_of_threads; i++){
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("[-] Accept failed");
return 1;
}
printf("[+] Connection accepted\n");
new_sock = malloc(sizeof(int));
*new_sock = new_socket;
args->soc = new_sock;
if( pthread_create( &threads[i], NULL, sendChunks, args) < 0)
{
perror("[-] Could not create thread at server side.");
return 1;
}
printf("[+] Server Thread %d created...\n", i);
sleep(1);
}
for (int x = 0; x < num_of_threads; x++){
pthread_join(threads[x], NULL); //waits for thread to end
}
fclose(readptr);
return 0;
}
void *sendChunks(void *args)
{
struct thread_args *threadArgs = (struct thread_args *)malloc(sizeof(struct thread_args));
threadArgs = args;
int *s = threadArgs->soc;
int new_socket = *(int*)s;
int chunksize = threadArgs -> chars_toread;
FILE *readptr = threadArgs -> fileptr;
char chunkBuffer[chunksize +1];
int file_chars = fread(chunkBuffer, sizeof(char), chunksize, readptr);
// sending file chunk to client
if(send(new_socket, chunkBuffer, sizeof(chunkBuffer), 0)== -1)
{
perror("[-] Error in sending Buffer.\n");
exit(1);
}
threadArgs -> fileptr = readptr;
return 0;
}