-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
169 lines (98 loc) · 2.75 KB
/
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
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
#include"stdio.h"
#include"stdlib.h"
#include"sys/types.h"
#include"sys/socket.h"
#include"string.h"
#include"netinet/in.h"
#include"pthread.h"
#define PORT 4444 //port number to bindto
#define BUF_SIZE 2000 // size of message to be reieved or sent
#define CLADDR_LEN 100 //size of client address
int mb;
int i=0;
struct clientIp{
char add[CLADDR_LEN];
};
struct clientIp clip[50];
void * acceptConn(void * sockfd)
{
}
void * receiveMessage(void * socket) {
int sockfd, ret;
char buffer[BUF_SIZE];
sockfd = (int) socket; //derefrencing sockfd as address of scoekd is passed to void pointer
//empty the buffer
memset(buffer, 0, BUF_SIZE);
//infinte loop
for (;;)
{
//recvfrom it takes 6 arguments and return number of bytes recieved
ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, NULL, NULL);
if (ret < 0) {
printf("Error receiving data!\n");
} else {
printf("%s: ",clip[i].add);
fputs(buffer, stdout);
//printf("\n");
}
int kb=ret/1000; //convert ret(in bytes to kilobytes)
mb+=kb; //add in total kb
printf("\nKB Recieved is:%d\n",mb);
}
}
void main() {
struct sockaddr_in addr, cl_addr;
int sockfd, len, ret, newsockfd;
char buffer[BUF_SIZE];
pid_t childpid;
i=0;
pthread_t rThread;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("Error creating socket!\n");
exit(1);
}
printf("Socket created...\n");
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = PORT;
ret = bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
if (ret < 0) {
printf("Error binding!\n");
exit(1);
}
printf("Binding done...\n");
printf("Waiting for a connection...\n");
listen(sockfd, 1000);
len = sizeof(cl_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cl_addr, &len);
if (newsockfd < 0) {
printf("Error accepting connection!\n");
exit(1);
}
inet_ntop(AF_INET, &(cl_addr.sin_addr),clip[i].add, CLADDR_LEN);
printf("Connection accepted from %s...\n", clip[0].add);
memset(buffer, 0, BUF_SIZE);
printf("Enter your messages one by one and press return key!\n");
//creating a new thread for receiving messages from the client
ret = pthread_create(&rThread, NULL, receiveMessage, (void *) newsockfd);
if (ret) {
printf("ERROR: Return Code from pthread_create() is %d\n", ret);
exit(1);
}
printf("\nServerr:");
while (fgets(buffer, BUF_SIZE, stdin) != NULL) {
printf("\nServer:");
ret = sendto(newsockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr, len);
if (ret < 0) {
printf("Error sending data!\n");
exit(1);
}
int asd=ret/1000;
}
close(newsockfd);
close(sockfd);
pthread_exit(NULL);
return;
}