forked from wshgithub/UNP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cc
More file actions
61 lines (52 loc) · 1.13 KB
/
server.cc
File metadata and controls
61 lines (52 loc) · 1.13 KB
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
#include "doevent.h"
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#define ERR_EXIT(info)\
do\
{\
perror(info);\
exit(EXIT_FAILURE);\
} while(0)
//deal with connect in client
extern void do_service(int listenfd);
int main(void)
{
//listening for client
int listenfd = socket(AF_INET, SOCK_STREM, 0);
if (listenfd == -1)
{
ERR_EXIT("socket");
}
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
//open a port and an address for client
servaddr.sin_port = htons(5588);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
int on = 1;
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on,sizeof(on)) == -1)
{
ERR_EXIT("setsockopt");
}
//bind the port and address
if (bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) == -1)
{
ERR_EXIT("bind");
}
//listening for client
if (listen(listenfd,SOMAXCONN) == -1)
{
ERR_EXIT("listen");
}
do_service(listenfd);
return 0
}