-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_client.c
104 lines (85 loc) · 2.25 KB
/
tcp_client.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
#include "tcp.h"
void recvFile(int sockfd, const char *infilename, const char *outfilename)
{
char buff[MAX]; // to store message from client
int recv_fd, read_len, file_name_len;
int ret = 0;
int exist;
// strcpy(filename_buff, filename);
file_name_len = strlen(infilename) + 1;
printf("sending filename > %s with size > %d\n", infilename, file_name_len);
ret = send(sockfd, infilename, file_name_len, 0);
if (ret < 0)
{
printf("Failed to send filename\n");
return;
}
ret = read(sockfd, &exist, sizeof(exist));
if (ret < 0)
{
printf("Failed to receive data about file existence\n");
}
//printf("Value of exits in client > %d\n", ntohl(exist));
if (exist < 0)
{
printf("Failed: File doesn't exist at the server\n");
return;
}
recv_fd = open(outfilename, O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (!recv_fd)
{
printf("Error IN Opening File ");
return;
}
while (1)
{
memset(buff, 0x00, MAX);
read_len = read(sockfd, buff, MAX);
ret = write(recv_fd, buff, read_len);
if (ret < 0)
{
printf("Failed to recieve/write data to the server\n");
}
if (read_len == 0)
{
printf("Download finish\n");
printf("File received successfully!!\n");
break;
}
}
}
int run_tcp_client(const char *infilename, const char * outfilename)
{
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
printf("file requested by client: %s\n", infilename);
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0)
printf("setsockopt(SO_REUSEADDR) failed");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0)
{
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
// function for sending File
recvFile(sockfd, infilename, outfilename);
// close the socket
close(sockfd);
}