-
Notifications
You must be signed in to change notification settings - Fork 3
/
server16.c
73 lines (64 loc) · 1.6 KB
/
server16.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
void main()
{
int ssock,csock;
int t = 0; // creating server and clinet socket discriptor
char s1[30], s2[30], c[30], h[30];
unsigned int len;
strcpy(s2, "oyeoye");
struct sockaddr_in server,client; // creating server & client socket object
if((ssock=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket: is not created");
exit(-1);
}
server.sin_family=AF_INET;
server.sin_port=htons(10000); // initializing server socket parameters..
server.sin_addr.s_addr=INADDR_ANY;
//inet_addr("127.0.0.1");
bzero(&server.sin_zero,0); //appending 8 byte zeroes to 'struct sockaddr_in', to make it equal in size with 'struct sockaddr'..
len=sizeof(struct sockaddr_in);
if((bind(ssock,(struct sockaddr *)&server,len))==-1)
{ // binding port & IP
perror("bind: ");
exit(-1);
}
if((listen(ssock,5))==-1)
{ // listening for client
perror("listen: ");
exit(-1);
}
if((csock=accept(ssock,(struct sockaddr *)&client,&len))==-1)
{ // accepting connectn
perror("accept: ");
exit(-1);
}
while(1)
{
recv(csock,s1,sizeof(s1),0);
if((strcmp(s1, s2) == 0))
{
t = 1;
send(csock,&t,sizeof(t),0);
recv(csock,h,sizeof(h),0);
strcpy(c, "Hello");
send(csock,c,sizeof(c),0);
}
else
{
send(csock,&t,sizeof(t),0);
strcpy(c, "IncorrectPassword");
send(csock,c,sizeof(c),0);
} // sending data to client...
printf("\nsent value=: %s\n",c);
}
close(ssock);
}