Skip to content
Snippets Groups Projects
Commit 7a58c8ed authored by Vamp1reSnake's avatar Vamp1reSnake
Browse files

Exo 1-3 fait

parent 434a99b4
Branches
No related merge requests found
No preview for this file type
File added
File added
......@@ -13,19 +13,46 @@
int main (int argc, char *argv [])
{
/* test arg number */
if(argc!=2)
{
perror("Invalid argument's number\n");
exit(EXIT_FAILURE);
}
const char* phrase="hello world";
/* convert and check port number */
int port_number = atoi(argv[1]);
if(port_number < 10000 || port_number > 65000){
perror("Invalid port number\n");
exit(EXIT_FAILURE);
}
/* create socket */
int new_socket=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
CHECK(new_socket);
/* complete sockaddr struct */
int error;
struct addrinfo *res;
error =getaddrinfo(IP,argv[1],NULL,&res);
if(error!=0){
perror("getaddrinfo\n");
exit(EXIT_FAILURE);
}
/* send message to remote peer */
struct addrinfo *list_udp=res;
while (list_udp->ai_protocol != IPPROTO_UDP)
{
list_udp = list_udp->ai_next;
if (list_udp == NULL)
{
perror("Problème pas de protocole UDP");
exit(EXIT_FAILURE);
}
}
CHECK(sendto(new_socket,phrase,strlen(phrase),0,res->ai_addr,res->ai_addrlen));
/* close socket */
/* free memory */
close(new_socket);
/* free memory */
freeaddrinfo(res);
return 0;
}
File added
File added
File added
......@@ -16,22 +16,51 @@
int main (int argc, char *argv [])
{
/* test arg number */
if(argc!=2)
{
perror("Invalid argument's number\n");
exit(EXIT_FAILURE);
}
/* convert and check port number */
int port_number = atoi(argv[1]);
if(port_number < 10000 || port_number > 65000){
perror("Invalid port number\n");
exit(EXIT_FAILURE);
}
/* create socket */
int new_socket=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
CHECK(new_socket);
int error;
struct addrinfo *res;
error =getaddrinfo(IP,argv[1],NULL,&res);
if(error!=0){
perror("getaddrinfo\n");
exit(EXIT_FAILURE);
}
/* complete struct sockaddr */
/* link socket to local IP and PORT */
struct addrinfo *list_udp=res;
while (list_udp->ai_protocol != IPPROTO_UDP)
{
list_udp = list_udp->ai_next;
if (list_udp == NULL)
{
perror("Problème pas de protocole UDP");
exit(EXIT_FAILURE);
}
}
/* link socket to local IP and PORT */
CHECK(bind(new_socket,res->ai_addr,res->ai_addrlen));
/* wait for incoming message */
char buffer[SIZE];
CHECK(recvfrom(new_socket,buffer,SIZE,0,NULL,NULL));
/* close socket */
close(new_socket);
/* free memory */
freeaddrinfo(res);
/* print received message */
fprintf(stdout,"%s",buffer);
return 0;
}
File added
KOLESNIKOV @ 434a99b4
Subproject commit 434a99b4595bdc3d2f5cbd1ebdf0dd85b80a8753
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment