-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_socket.c
46 lines (37 loc) · 1.14 KB
/
init_socket.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
/*file : init_socket.c */
#include <stdio.h>
#include <strings.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "init_socket.h"
int init_socket(int *listen_fd ,struct sockaddr_in *server_addr)
{
if((*listen_fd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket() error, in init_socket.c");//perror???
return -1;
}
//set reuse the port on server machine
int opt = SO_REUSEADDR;
if(setsockopt(*listen_fd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)) == -1)//setsockopt?????
{
perror("setsockopt() error. in init_socket.c");
return -1;
}
server_addr->sin_family = AF_INET;
server_addr->sin_port=htons(PORT);
server_addr->sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(*listen_fd,(struct sockaddr *)server_addr,sizeof(struct sockaddr_in)) == -1)
{
perror("bind() error. in init_socket.c");
return -1;
}
if(listen(*listen_fd,BACKLOG) == -1) //listen ??
{
perror("listen() error. in init_socket.c");
return -1;
}
return 0;
}