-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_server.c
127 lines (103 loc) · 2.73 KB
/
tcp_server.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
#include "tcp.h"
void sentFile(int sockfd)
{
char file_name[MAX], buff[MAX]; // for read operation from file and used to sent operation
int send_fd, read_len;
int exist, ret;
memset(buff, 0x00, MAX);
read_len = read(sockfd, buff, MAX);
if (read_len == 0)
{
return;
}
strcpy(file_name, buff);
printf("File requested > %s\n", file_name);
if (access(file_name, F_OK) < 0)
{
exist = -1;
}
ret = send(sockfd, &exist, sizeof(exist), 0);
if (ret < 0)
{
printf("Failed to make aware(send) client if the file exists or not\n");
}
if (exist < 0)
{
printf("%s doesn't exist in server\n", file_name);
return;
}
send_fd = open(file_name, O_RDONLY); // open file uses both stdio and stdin header files
// file should be present at the program directory
if (!send_fd)
{
printf("Error in opening File\n");
return;
}
while (1)
{
memset(buff, 0x00, MAX);
read_len = read(send_fd, buff, MAX);
ret = send(sockfd, buff, read_len, 0);
if (ret < 0)
{
printf("Failed to send data\n");
}
if (read_len == 0)
{
printf("File Sent successfully !!! \n");
break;
}
}
}
int main()
{
int sockfd, connfd, len; // create socket file descriptor
struct sockaddr_in servaddr, cli; // create structure object of sockaddr_in for client and server
// socket create and verification
sockfd = socket(AF_INET, SOCK_STREAM, 0); // creating a TCP socket ( SOCK_STREAM )
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");
// empty the
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET; // specifies address family with IPv4 Protocol
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); // binds to any address
servaddr.sin_port = htons(PORT); // binds to PORT specified
// Binding newly created socket to given IP and verification
if ((bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) != 0)
{
printf("socket bind failed...\n");
exit(0);
}
else
printf("Socket successfully binded..\n");
// Now server is ready to listen and verification
if ((listen(sockfd, 5)) != 0)
{
printf("Listen failed...\n");
exit(0);
}
else
printf("Server listening..\n");
len = sizeof(cli);
// Accept the data packet from client and verification
connfd = accept(sockfd, (struct sockaddr *)&cli, &len); // accepts connection from socket
if (connfd < 0)
{
printf("server acccept failed...\n");
exit(0);
}
else
printf("server acccept the client...\n");
// Function for chatting between client and server
sentFile(connfd);
// After transfer close the socket
close(sockfd);
}