-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
203 lines (148 loc) · 3.83 KB
/
server.c
File metadata and controls
203 lines (148 loc) · 3.83 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
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <sys/stat.h>
#define MAX 1024
#define PORT 8012
#define SA struct sockaddr
// GENERATE MEANINGLESS RANDOM STRING
void gen_random(char *s, const int len) {
static const char alphanum[] = " ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz ";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] ='\n';
s[len+1]=0;
}
// FILE TRANSFER
int sendfile(int connfd)
{
struct stat st;
char buffer[80];
stat("myfile.mp4", &st);
int size = st.st_size;
bzero(buffer,sizeof(buffer));
snprintf(buffer,sizeof(buffer),"%d",size);
send(connfd,buffer,80,0);
FILE *fp = fopen("myfile.mp4","r");
if(fp==NULL)
{
printf("File open error");
return 1;
}
while(1)
{
char buff[500];
bzero(buff, 500);
int nread = fread(buff,1,500,fp);
if(nread > 0)
{
write(connfd, buff, nread);
}
if (nread < 500)
{
if (feof(fp))
{
printf("End of file\n");
printf("File transfer completed\n");
}
if (ferror(fp))
printf("Error reading\n");
break;
}
}
printf("This Video is sent\n");
return 0;
}
// Function designed for chat between client and server.
void func(int newsockfd)
{
char buff[1024];
int n;
// infinite loop for chat
while(1) {
bzero(buff, 1024);
// read the message from client and copy it in buffer
int s=read(newsockfd, buff, sizeof(buff));
// if msg contains "Bye" then server will exit and chat's ended.
if (strncmp("Bye", buff, 3) == 0||s==0)
{
if(s)
{
printf("From CLIENT : %s", buff);
}
printf("END OF CLIENT \n");
break;
}
printf("From CLIENT : %s", buff);
if((strncmp("GivemeyourVideo",buff,15))==0)
{
printf("\nInitiating the Sending Process...\n");
sendfile(newsockfd);
}
else
{
bzero(buff, 1024);
gen_random(buff,rand()%10);
printf("To CLIENT : %s",buff);
write(newsockfd, buff, sizeof(buff));
}
}
}
// Main Function
int main()
{
int sockfd, newsockfd, len;
struct sockaddr_in servaddr, cli;
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("SOCKET CREATION FAILED \n");
exit(0);
}
else
printf("SOCKET IS CREATED\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0)
{
printf("SOCKET BINDING IS FAILED\n");
exit(0);
}
else
printf("SOCKET IS BINDED \n");
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0)
{
printf("LISTENING IS FAILED\n");
exit(0);
}
else
printf("SERVER IS LISTENING\n");
len = sizeof(cli);
// Accept the data packet from client and verification
newsockfd = accept(sockfd, (SA*)&cli, &len);
if (newsockfd < 0)
{
printf("SERVER ACCEPTANCE IS FAILED\n");
exit(0);
}
else
printf("SERVER ACCEPTED THE CLIENT\n");
// Function for chatting between client and server
func(newsockfd);
// After chatting close the socket
close(sockfd);
}