-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.c
38 lines (37 loc) · 1.05 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
#include "Server.h"
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
Server server_constructor(int domain, int service, int protocol, u_long interface, int port, int backlog)
{
Server server;
server.domain = domain;
server.service = service;
server.protocol = protocol;
server.interface = interface;
server.port = port;
server.backlog = backlog;
server.address.sin_family = domain;
server.address.sin_port = htons(port);
server.address.sin_addr.s_addr = htonl(interface);
server.socket = socket(domain, service, protocol);
if (server.socket == 0)
{
perror("Failed to connect socket...\n");
exit(1);
}
if ((bind(server.socket, (struct sockaddr *)&server.address, sizeof(server.address))) < 0)
{
perror("Failed to bind socket...\n");
exit(1);
}
if ((listen(server.socket, server.backlog)) < 0)
{
perror("Failed to start listening...\n");
exit(1);
}
return server;
}