-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
57 lines (45 loc) · 1.14 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
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<string.h>
int main()
{
char rcvmsg[100];
char sendmsg[100];
int sd,sd2,nsd,clilen,sport=6000,len,port;
sd = socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
printf("cant 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("cant 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 adress:%s",inet_ntoa(cliaddr.sin_addr));
recv(nsd,rcvmsg,20,0);
printf("%s\n",rcvmsg);
fgets(sendmsg,20,stdin);
len=strlen(sendmsg);
sendmsg[len-1]='\0';
send(nsd,sendmsg,20,0);
close(sd);
close(nsd);
return 0;
}