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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#define SERVEUR_DEFAULT ("localhost")
#define SZ_BUF (256)
int main(
int argc,
char *argv[])
{
ushort i;
ushort j;
int sk_dialog;
char buf[SZ_BUF];
char *pt;
char *serveur;
struct sockaddr_in adr_serveur;
struct hostent *host_info;
if (argc > 1)
serveur=argv[1];
else
{
serveur=SERVEUR_DEFAULT;
printf("Pas d'argument pour %s - Utilisation de %s\n", *argv, serveur);
}
if ((host_info=gethostbyname(serveur)) == NULL)
{
fprintf(stderr, "ligne %u - gethostbyname(%s) - %s\n", __LINE__, serveur, strerror(errno));
exit(errno);
}
fputc('\n', stdout);
printf("host_info.h_name='%s'\n", host_info->h_name);
for (i=0; host_info->h_aliases[i] != NULL; i++)
printf("host_info.h_aliase[%hu]='%s'\n", i, host_info->h_aliases[i]);
printf("host_info.h_addrtype=%u\n", host_info->h_addrtype);
printf("host_info.h_length=%u\n", host_info->h_length);
for (i=0; host_info->h_addr_list[i] != NULL; i++)
{
printf("host_info.h_addr_list[%hu]=", i);
for (j=0; j < host_info->h_length; j++)
printf("%hu ", (unsigned char)host_info->h_addr_list[i][j]);
fputc('\n', stdout);
}
if ((sk_dialog=socket(AF_INET, SOCK_DGRAM, 0)) == (-1))
{
fprintf(stderr, "ligne %u - socket() - %s\n", __LINE__, strerror(errno));
exit(errno);
}
printf("Socket créée\n");
memset(&adr_serveur, 0, sizeof(struct sockaddr_in));
adr_serveur.sin_family=AF_INET;
adr_serveur.sin_port=htons(12546);
memcpy(&adr_serveur.sin_addr.s_addr, host_info->h_addr, host_info->h_length);
do
{
fputs("Entrer chaine (EOT pour finir) :", stdout); fflush(stdout);
fgets(buf, SZ_BUF, stdin);
if ((pt=strchr(buf, '\n')) != NULL) *pt='\0';
if (sendto(sk_dialog, buf, strlen(buf) + 1, 0, (struct sockaddr*)&adr_serveur, sizeof(struct sockaddr_in)) == (-1))
fprintf(stderr, "ligne %u - sendto(%s) - %s\n", __LINE__, buf, strerror(errno));
}
while (strcmp(buf, "EOT") != 0);
close(sk_dialog);
return(0);
} |
Partager