-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.cpp
145 lines (119 loc) · 4.34 KB
/
server.cpp
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
#include<iostream>
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<string.h>
#include<stdlib.h>
#include<fstream>
#define maxlen 70000
#define mlen 100000
int main()
{
char fileName[100];
char fileBuffer[2000],caufile[maxlen];
char *vfilep;
int aufile[70000],vfile[mlen]; //aufile is for audio and vfile is for video
int sd,connfd; //sd-Socket Decriptor
socklen_t len;
for(int i=0;i<=100;i++)
fileName[i]='\0';
struct sockaddr_in servaddr,cliaddr; //struct class and it's member variables
// ------------------------------Creation of Socket-----------------------------
sd=socket(AF_INET,SOCK_DGRAM,0);
if(sd==-1)
{
std::cout<<"Socket not created in server"<<std::endl;
exit(0);
}
else
std::cout<<"Socket created in server"<<std::endl;
bzero(&servaddr,sizeof(servaddr));
//Adding parameter to socket //Initialisation Member variables
servaddr.sin_family=AF_INET; //for UDP connection
servaddr.sin_addr.s_addr=INADDR_ANY; //for local host(server and client are both on the local host and the communicaton will take place)
servaddr.sin_port=htons(6666); //set the port number
memset(&(servaddr.sin_zero),'\0',8); //setting the sin_zero parameter as null using function memset(memoryset)
// ----------------------------Binding the Socket-------------------------------------
if( bind(sd,(struct sockaddr *)&servaddr,sizeof(servaddr))!=0)
std::cout<<"Not binded"<<std::endl;
else
std::cout<<"Binded"<<std::endl;
len=sizeof(cliaddr);
int choice=1;
while(1)
{
char num;
recvfrom(sd,&num,sizeof(num),0,(struct sockaddr *)&cliaddr,&len); //recvfrom is a function used for UDP transfer 1024 is memory of flag 0 and it's length
//file name will be saved in variale fileName
choice=num;
//int fsize =strlen(fileBuffer);
if(choice==1)
{
//Case for Text files
recvfrom(sd,fileName,1024,0,(struct sockaddr *)&cliaddr,&len);
std::cout<<"NAME OF THE TEXT FILE RECEIVED: " <<std::endl<<fileName;
FILE *fp;
std::cout<<"\nContents in the received text file: "<<std::endl;
recvfrom(sd,fileBuffer,1024,0,(struct sockaddr *)&cliaddr,&len); //recvfunction is a function used for UDP transfer 1024 is memory of flag 0 and it's length
std::cout<<std::endl<<fileBuffer;
int fsize =strlen(fileBuffer); //fsize is used to save the memory of filebuffer
fp=fopen(fileName,"w");
if(fp)
{
fwrite(fileBuffer,fsize,1,fp);
std::cout<<"\n\nFile received successfully"<<std::endl;
}
else
std::cout<<"Can't create to output file";
memset(fileName,'\0',sizeof(fileName));
fclose(fp);
}
//Case for audio file
else if(choice==2)
{
recvfrom(sd,fileName,1024,0,(struct sockaddr *)&cliaddr,&len); //to recieve filename
std::cout<<"Name of audio file received"<<std::endl<<fileName;
FILE *afp; //afp is pointer varible
int numbytes;
afp=fopen(fileName,"w"); //Opening audio file in write format
size_t afsize; //datatype of unsigned intergral type
afsize=recvfrom(sd,aufile,700000,0,(struct sockaddr *)&cliaddr,&len); //To receive contents of the file name which will be saved in the aufile
if(afp)
{
fwrite(aufile,afsize,1,afp); //running fwrite to save the file(aufile) onto the server side and If it's non zero,then the operation was successfull
std::cout<<"File received successfully"<<std::endl;
}
else
std::cout<<"Can't open output file";
memset(fileName,'\0',sizeof(fileName)); //memset, fileName contents are set to null so that we can use it again
fclose(afp);
}
//Case for receiving Video file
else if(choice==3)
{
recvfrom(sd,fileName,1024,0,(struct sockaddr *)&cliaddr,&len);
std::cout<<"Video file name received : "<<std::endl<<fileName;
FILE *vfp;
vfp=fopen(fileName,"w");
size_t vfsize;
vfsize=recvfrom(sd,vfile,100000,0,(struct sockaddr *)&cliaddr,&len);
if(vfp)
{
fwrite(vfile,vfsize,1,vfp);
std::cout<<"File received successfully"<<std::endl;
}
else
std::cout<<"Can't open output file"<<std::endl;
fclose(vfp);
}
else if(choice==4)
{
close(sd);
break;
}
}
return 0;
}