-
Notifications
You must be signed in to change notification settings - Fork 0
/
serv.c
224 lines (182 loc) · 4.51 KB
/
serv.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <signal.h>
#include<sys/stat.h>
int cli_count = 0;
int uid =1;
//char *msg="FILE ALREADY EXISTS";
/* Client structure */
typedef struct
{
struct sockaddr_in address;
int sockfd;
int uid;
char filename[1024];
} client_t;
/* Thread Function */
void *connection_handler(void *arg)
{
printf("Connection established to client\n");
client_t *cli = (client_t *)arg;
int rb=0;
char rbuf[1024];
char filename[1024];
char string[1024];
int leave_flag = 0;
cli_count++;
int z=0;
z=recv(cli->sockfd, string, 1024, 0);
if(z<=0)
{
printf("Filename not received\n");
// goto S;
}
char *ptr=string+strlen(string);
int len=strlen(string);
while(len)
{
ptr--;
len--;
if(*ptr=='/')
{
ptr++;
//filename=(char *)malloc(sizeof(ptr));
strcpy(filename,ptr);
break;
}
}
//char str[1024];
char *str;
char num[3];
char *name;
//strcpy(cli->filename,filename);
//strcpy(str,filename);
str=strdup(filename);
bzero(filename,1024);
name=strdup(str);
//overwrite file
//char str1[]=cli->filename;
//sprintf(num,"%d",uid);
//*str=(char *)malloc((strlen(str)+1)*sizeof(char));
FILE *file;
char ch[7]="(copy)";
//no=strdup(
int i=1;
int a=0;
P:if((file=fopen(str,"r"))!=NULL)
{
//remove file contents(over write)->
// fclose(fopen(cli->filename, "w"));
// fclose(file);
/*---------------------------------------------*/
//stop two clients to write at a same time on same file
//goto S;
/*------------------------------------------------*/
//if same file then rename
bzero(str,strlen(str));
str=strdup(name);
//bzero(name,strlen(name));
//strcpy(str,filename);
strncat(str,ch,strlen(ch));
a=i;
i++;
sprintf(num,"%d",a);
//strncat(ch,num,strlen(num));
//bzero(num,strlen(num));
//strncat(str,ch,strlen(ch));
//bzero(ch,strlen(ch));
strncat(str,num,strlen(num));
//memset(ch,0,strlen(ch));
goto P;
//memset(str,0,strlen(str));
//free(str);
}
char location[1024]="/home/antar/Documents/FileTransfer/Server_/";
strncat(location,str,strlen(str));
//strcpy(cli->filename,str);
memset(rbuf,0,sizeof(rbuf));
while ((rb = recv(cli->sockfd, rbuf, 1024,0))>0)
{
FILE *out;
//out = fopen(cli->filename, "a");
out=fopen(location,"a");
printf("%s\n", rbuf);
fwrite(rbuf,1,strlen(rbuf),out);
bzero(rbuf,1024);
fclose(out);
}
S:printf("Closing thread socket.\n");
close(cli->sockfd);
cli_count--;
free(cli);
free(str);
return 0;
}
int main(int argc, char **argv)
{
if(argc != 2)
{
printf("Usage: %s <port>\n", argv[0]);
return EXIT_FAILURE;
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
int option = 1;
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
pthread_t tid;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(ip);
serv_addr.sin_port = htons(port);
/* Ignore pipe signals */
signal(SIGPIPE, SIG_IGN);
if(setsockopt(listenfd, SOL_SOCKET,(SO_REUSEPORT | SO_REUSEADDR),(char*)&option,sizeof(option)) < 0)
{
perror("ERROR: setsockopt failed");
return EXIT_FAILURE;
}
/* Bind */
if(bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
perror("ERROR: Socket binding failed");
return EXIT_FAILURE;
}
/* Listen */
if (listen(listenfd, 10) < 0)
{
perror("ERROR: Socket listening failed");
return EXIT_FAILURE;
}
while(1)
{
socklen_t clilen=sizeof(cli_addr);
connfd=accept(listenfd,(struct sockaddr *)&cli_addr,&clilen);
if(connfd<0)
{
printf("server accept fail");
exit(0);
}
else
{
printf("sever accept the client\n");
}
/* Client settings */
client_t *cli = (client_t *)malloc(sizeof(client_t));// dynamically giving memory for the client
cli->address = cli_addr;
cli->sockfd = connfd;
cli->uid = uid++;
/* Creating thread */
pthread_create(&tid, NULL, &connection_handler, (void*)cli);
}
return EXIT_SUCCESS;
}