Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
apsrcreatix
GitHub Repository: apsrcreatix/Socket-Programming-With-C
Path: blob/master/05-day-time-server-tcp-ip/server.c
137 views
1
/**
2
* Title : Day-Time server
3
* Name : Aditya Pratap Singh Rajput
4
* Subject : Network Protocols And Programming using C
5
*
6
* */
7
8
//program for date time server
9
#include<stdio.h>
10
#include<stdlib.h>
11
#include<sys/types.h>
12
#include<sys/socket.h>
13
#include<netinet/in.h>
14
#include<unistd.h>
15
#include<time.h>
16
17
//port value and the max size for buffer
18
#define PORT 9002 //the port users will be connecting to
19
#define BACKLOG 10 //how many pending connections queue will hold
20
21
int main(){
22
//*******************Time Setup***********************
23
time_t currentTime ;
24
time(&currentTime);
25
//****************************************************
26
27
int countClient = 0;
28
29
int socket_descriptor = socket(AF_INET, SOCK_STREAM, 0);
30
31
struct sockaddr_in serverAddress;
32
serverAddress.sin_family = AF_INET;
33
serverAddress.sin_addr.s_addr=INADDR_ANY;
34
serverAddress.sin_port=htons(PORT);
35
36
//binding address
37
bind(socket_descriptor,(struct sockaddr*)&serverAddress,sizeof(serverAddress));
38
39
//listening to the queue
40
listen(socket_descriptor,BACKLOG);
41
42
printf("\nServer Started ...");
43
44
while(1){
45
countClient++;
46
printf("\n");
47
48
int client_socket = accept(socket_descriptor, NULL, NULL);
49
// char *string = asctime(timeinfo);
50
51
printf("\nClient %d has requested for time at %s", countClient, ctime(&currentTime));
52
53
//sending time to the client side
54
// ctime(&time_from_pc)
55
send(client_socket,ctime(&currentTime), 30, 0);
56
57
}
58
return 0;
59
}
60
61