Added puit and source codes

This commit is contained in:
Yohan Boujon 2023-01-10 20:57:18 +01:00
parent b255d6be06
commit c9d18d17f6
2 changed files with 105 additions and 0 deletions

60
puit.c Normal file
View file

@ -0,0 +1,60 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#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);
}

45
source.c Normal file
View file

@ -0,0 +1,45 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#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);
}