Skip to content

Instantly share code, notes, and snippets.

@froland
Created December 5, 2017 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save froland/645ab1a12c6ce5332079e4977adcaa59 to your computer and use it in GitHub Desktop.
Save froland/645ab1a12c6ce5332079e4977adcaa59 to your computer and use it in GitHub Desktop.
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
int sock, socket_client;
void handler(int sig)
{
close(socket_client);
close(sock);
exit(0);
}
int calcul(int a)
{
return a*a;
}
int main(void)
{
char hostname[100];
int size;
if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("Probleme lors du socket()");
exit(-1);
}
struct sockaddr_in adresse, adresse_client;
adresse.sin_family=AF_INET;
adresse.sin_port=htons(6969);
adresse.sin_addr.s_addr=inet_addr("0.0.0.0");
if(bind(sock,(struct sockaddr*)&adresse,sizeof(struct sockaddr_in))==-1)
{
perror("Probleme lors du bind()");
exit(-1);
}
signal(SIGINT,handler);
size=sizeof(struct sockaddr_in);
listen(sock,128);
while(1)
{
socket_client=accept(sock,(struct sockaddr *)&adresse_client,&size);
if(fork()==0)
{
read(socket_client,&hostname,100);
printf("Client %s a envoyé une trame\n", hostname);
exit(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment