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 62 63 64 65 66 67
| #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> /* close */
#include <netdb.h> /* gethostbyname */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define PORT 80
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
int main()
{
printf("coucou");
SOCKET sock = socket(AF_LOCAL, SOCK_STREAM, 0);
if(sock == INVALID_SOCKET)
{
perror("socket()");
exit(errno);
}
SOCKADDR_IN sin = { 0 };
sin.sin_addr.s_addr = htonl(INADDR_ANY); /* nous sommes un serveur, nous acceptons n'importe quelle adresse */
sin.sin_family = AF_LOCAL;
sin.sin_port = htons(PORT);
if(bind (sock, (SOCKADDR *) &sin, sizeof sin) == SOCKET_ERROR)
{
perror("bind()");
exit(errno);
}
if(listen(sock, 5) == SOCKET_ERROR)
{
perror("listen()");
exit(errno);
}
SOCKADDR_IN csin = { 0 };
SOCKET csock;
int sinsize = sizeof csin;
csock = accept(sock, (SOCKADDR *)&csin, &sinsize);
if(csock == INVALID_SOCKET)
{
perror("accept()");
exit(errno);
}
closesocket(sock);
closesocket(csock);
} |
Partager