42-archive/nmap/libft/srcs/net/create_server.c
2017-10-08 21:30:28 +02:00

31 lines
1.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_server.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 18:02:51 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:02:51 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int create_server(int port, int backlog, char *protoname)
{
int sock;
struct protoent *proto;
struct sockaddr_in sin;
if (!(proto = getprotobyname(protoname)))
return (-1);
sock = socket(PF_INET, SOCK_STREAM, proto->p_proto);
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (const struct sockaddr *)&sin, sizeof(sin)) < 0)
return (-1);
listen(sock, backlog);
return (sock);
}