-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsender.h
More file actions
143 lines (106 loc) · 2.68 KB
/
sender.h
File metadata and controls
143 lines (106 loc) · 2.68 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
void sendFileName(char path[], int sockfd );
void sendfile(char path[], int sockfd );
int validateInput(char receiver_ip[], char path[]);
int socketConnect(char receiver_ip[], char path[])
{
// WSADATA wsaData;
int sockfd;
sockfd = iniSocket();
struct sockaddr_in serveraddr;
memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVERPORT);
serveraddr.sin_addr.s_addr = inet_addr(receiver_ip);
if (connect(sockfd, (const struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0)
{
perror("\n Connection Error ");
WSACleanup();
getch();
exit(1);
}
printf("\n Connection Request Sent............");
sendFileName(path,sockfd);
//puts("Send Success");
close(sockfd);
// WSACleanup();
// getch();
return 0;
}
void sendFileName(char path[], int sockfd)
{
char* filename;
char buff[BUFFSIZE] = {0};
filename = (char *)basename(path);
if (filename == NULL)
{
perror("\n Can't get filename");
WSACleanup();
getch();
exit(1);
}
strncpy(buff, filename, strlen(filename));
if (send(sockfd, buff, BUFFSIZE, 0) == -1)
{
perror("\n Can't send filename");
WSACleanup();
getch();
exit(1);
}
printf("\n File Name Sent ............");
sendfile(path, sockfd );
}
int validateInput(char receiver_ip[], char path[])
{
if(strlen(receiver_ip)<7 || strlen(receiver_ip)>15)
{
perror("Invalid IP ");
WSACleanup();
getch();
exit(1);
}
if(strlen(path) <=1)
{
perror("Invalid Path ");
WSACleanup();
getch();
exit(1);
}
socketConnect(receiver_ip, path);
return 0;
}
void sendfile(char path[], int sockfd )
{
int n;
char sendline[MAX_LINE] = {0};
ssize_t total=0;
FILE *fp = fopen(path, "rb");
if (fp == NULL)
{
perror("\n Can't open file");
WSACleanup();
getch();
exit(1);
}
while ((n = fread(sendline, sizeof(char), MAX_LINE, fp)) > 0)
{
total+=n;
if (n != MAX_LINE && ferror(fp))
{
perror("\n Read File Error");
WSACleanup();
getch();
exit(1);
}
if (send(sockfd, sendline, n, 0) == -1)
{
perror("\n Can't send file");
WSACleanup();
getch();
exit(1);
}
printf("\n %ld Byte sent ",total);
memset(sendline, 0, MAX_LINE);
}
fclose(fp);
printf("\n Send Success, Number of Bytes Sent= %ld\n", total);
}