-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer4.c
82 lines (62 loc) · 1.57 KB
/
Server4.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
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>
#include<time.h>
int main()
{
int sd,sd2,nsd,clilen,sport=6000,len,port;
int check=1;
time_t ticks;
sd = socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
printf("Can't create\n");
else
printf("Socket is created\n");
struct sockaddr_in serveraddr;
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(sport);
sd2 = bind(sd,(struct sockaddr*)&servaddr,sizeof(servaddr));
if(sd2<0)
printf("Can't bind\n");
else
printf("Binded\n");
listen(sd,5);
struct sockaddr_in cliaddr;
clilen = sizeof(cliaddr);
nsd = accept(sd,(struct sockaddr*)&cliaddr,&clilen);
if(nsd<0)
printf("Can't accept\n");
else
printf("Accepted\n");
printf("Client addres:%s",inet_ntoa(cliaddr.sin_addr));
printf("\n");
char sendmsg[100];char rcvmsg[100];
while(strcmp(rcvmsg,"BYE"))
{
recv(nsd,rcvmsg,20,0);
printf("Client msg : %s\n",rcvmsg);
if(!strcmp(rcvmsg,"time"))
{
ticks = time(NULL);
strcpy(sendmsg,ctime(&ticks));
printf("Time Sent\n");
}
else
{
printf("Enter Message: ");
fgets(sendmsg,20,stdin);
}
len=strlen(sendmsg);
sendmsg[len-1]='\0';
send(nsd,sendmsg,20,0);
}
printf("Chat Terminated\n");
close(sd);
close(nsd);
return 0;
}