-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient404.c
119 lines (98 loc) · 2.45 KB
/
client404.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
char filename[1024];
//char *filename
void str_trim_lf (char* arr, int length) {
int i;
for (i = 0; i < length; i++) { // trim \n
if (arr[i] == '\n') { //if there is a '\n or enter given by the user
arr[i] = '\0'; //it means your msg is end ('\0' the string termination)
break;
}
}
}
void INThandler(int sig)
{
signal(sig, SIG_IGN); //catch the ctrl+c command from user
exit(0);
}
int main(int argc, char **argv){
char str[100];
if(argc != 2){
printf("Usage: %s <port>\n", argv[0]);
return EXIT_FAILURE;
}
int sockfd=0;
size_t len=0;
char sbuf1[1024];
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
signal(SIGINT, INThandler);
struct sockaddr_in servAddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = inet_addr(ip);
servAddr.sin_port = htons(port);
//Connect to the server
int err=connect(sockfd, (struct sockaddr *)&servAddr, sizeof(struct sockaddr));
if(err<0)
{
perror("Failure to connect to the server");
exit(1);
}
else
{
printf("Accepted\n");
g: printf("Enter the Filename: ");
signal(SIGINT,INThandler);
fgets(filename,1024,stdin);
//scanf("%m[^\n]",&filename);
str_trim_lf(filename,strlen(filename));
if(strlen(filename)>1024 || strlen(filename)<1)
{
printf("Please Enter a valid file name between 1 to 1024 characters\n");
goto g;
//free(filename);
// return EXIT_FAILURE;
}
FILE *in;
in = fopen(filename, "r");
if(in==NULL)
{
printf("File Dosen't Exist, Please enter an existing filename\n");
goto g;
//free(filename);
}
send(sockfd,filename,1024,0);
/*if((read(sockfd,sbuf1,sizeof(sbuf1)))>0)
{
printf("%s\n",sbuf1);
memset(sbuf1,0,sizeof(sbuf1)+1);
goto A;
}
*/
int n=0;
//Client begins to write and read from the server
memset(sbuf1,0,sizeof(sbuf1)+1);
printf("Client: transferring file\n");
while((n = fread(sbuf1,1,1024,in)) != 0){
printf("File contents: %s \n", sbuf1);
send(sockfd, sbuf1, 1024,0);
bzero(sbuf1,1024);
}
fclose(in);
// free(filename);
printf("File transfer complete. Closing socket.\n");
//Close socket
close(sockfd);
return 0;
}
}