diff --git a/puit.c b/puit.c new file mode 100644 index 0000000..d36501e --- /dev/null +++ b/puit.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define _OE_SOCKETS +#define PORT_NUM 9000 + +void initSocket(); + +int sock; +char buffer[1024]; +struct hostent *hp; +struct sockaddr_in socketClient,socketServer; + +int main(void) +{ + if((sock=socket(AF_INET,SOCK_DGRAM,0)) == -1) + { + printf("échec création du socket\n"); + exit(1); + } + memset(&socketClient, 0, sizeof(socketClient)); + memset(&socketServer, 0, sizeof(socketServer)); + + initSocket(); + + + if (bind(sock, (const struct sockaddr *)&socketServer, sizeof(socketServer)) < 0 ) + { + perror("bind failed"); + exit(EXIT_FAILURE); + } + + int n; + unsigned int lg_socketClient = sizeof(socketClient); + + n = recvfrom(sock, (char *)buffer, 1024, 0, (struct sockaddr*) &socketClient,&lg_socketClient); + buffer[n] = '\0'; + printf("Server : %s\n", buffer); + close(sock); + return 0; +} + +void initSocket() +{ + socketServer.sin_family=AF_INET; + socketServer.sin_port=htons(PORT_NUM); + if((hp = gethostbyname("localhost")) == NULL) + { + printf("erreur gethostbyname\n"); + exit(1); + } + memcpy((char*)&(socketServer.sin_addr.s_addr),hp->h_addr_list[0],hp->h_length); +} \ No newline at end of file diff --git a/source.c b/source.c new file mode 100644 index 0000000..45e5e4e --- /dev/null +++ b/source.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PORT_NUM 9000 + +void initSocket(); + +int sock; +int lg_emis; +struct hostent *hp; +struct sockaddr_in socketServer; + +int main(void) +{ + if((sock=socket(AF_INET,SOCK_DGRAM,0)) == -1) + { + printf("échec création du socket\n"); + exit(1); + } + memset(&socketServer, 0, sizeof(socketServer)); + initSocket(); + lg_emis=sendto(sock, "aaaaa", 6, 0, (struct sockaddr*)&socketServer, sizeof(socketServer)); + printf("lgemis = %d\n",lg_emis); + close(sock); + return 0; +} + +void initSocket() +{ + socketServer.sin_family=AF_INET; + socketServer.sin_port=htons(PORT_NUM); + if((hp = gethostbyname("localhost")) == NULL) + { + printf("erreur gethostbyname\n"); + exit(1); + } + memcpy((char*)&(socketServer.sin_addr.s_addr),hp->h_addr_list[0],hp->h_length); +} \ No newline at end of file