Problème de réception de message simple avec recvfrom() (socket)
Boujour à tous,
Dans le cadre d'un projet, j'ai besoin de faire communiquer un "client" et un "serveur" à l'aide d'envoie de socket. Sans vous expliquez en détail ce projet, j'essaie dans un premier temps d'envoyer un simple "hello!" en unicast à une addresse IP local. J'ai crée pour cela une thread (ci dessous):
main:
Code:
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
| #include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "unicast_socket_sendto.h"
/*Returns 0 if OK, a negative value if EOF.*/
int fpurge(FILE *f)
{
int c;
while((c=fgetc(f))!=EOF && c!='\n')
{ }
return (c==EOF ? -1 : 0);
}
void *send_uni(void *arg)
{
char *dest_ip = "100.0.0.0";
char *message = "hello";
int port = 7;
while (1)
{
printf("Push a letter S or Q:\r\n ");
char c = getchar();
fpurge(stdin);
if ((c=='s')||(c=='S'))
{
printf("Message sent: %s\n", message);
unicast_socket_sendto(port, dest_ip, message);
system("BREAK");
return EXIT_SUCCESS;
}
else if ((c=='q')||(c=='Q'))
{
printf("QUIT");
break;
}
else
{
printf("Invalid key, tape 'S' to send a message, 'Q' to exit.");
}
}
return NULL;
}
int main(void)
{
/* this variable is our reference to the second thread */
pthread_t thread;
/* create a second thread which executes send_uni(void *arg) */
if(pthread_create(&thread, NULL, send_uni, NULL)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
/* wait for the second thread to finish */
if(pthread_join(thread, NULL)) {
fprintf(stderr, "Error joining thread\n");
return 2;
}
return 0;
} |
classe "unicast_socket_sendto.c" :
Code:
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 85 86 87 88 89 90 91 92 93 94 95 96
|
#include "unicast_socket_sendto.h"
#include <errno.h> /* errno */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
/* gettimeofday */
int unicast_socket_sendto( int port, char * dest_ip, char * message) {
struct sockaddr_in sDest;
int socket_id;
WSADATA wsad; //Structure required to activate sockets
int iResult = 0; // variable to perform tests
char msg_rcv[1024]; //message received
if (WSAStartup(2,&wsad) == 0)
{
printf("Sockets are now usable...\n");
}
else
{
printf("Error n°%d : Error during initialization.\n", errno);
//exit(errno);
return errno;
}
//Initialization of the UDP socket
socket_id = socket(AF_INET, SOCK_DGRAM,0);
if(socket_id != -1)
{
printf("Initialization of the UDP socket successful.\n");
}
else
{
printf("Erreur n%d : Unable to initialize socket.\n",errno);
//exit(errno);
return errno;
}
// initialization of the destination structure
sDest.sin_family = AF_INET;
sDest.sin_port = htons(port);
sDest.sin_addr.s_addr = inet_addr(dest_ip);
int sizeSdest = sizeof(sDest); //Size of the receiving structure
// Sending the message
printf("Sending of the socket ...\n");
iResult = sendto(socket_id,message,strlen(message)+1, 0, (struct sockaddr *)&sDest, sizeof(sDest));
if (iResult != -1)
{
printf("Successful message sending.\n");
}
else
{
printf("Error n%d : Unable to send message.\n",errno);
//exit(errno);
return errno;
}
// Receiving the message
iResult = recvfrom(socket_id, msg_rcv, sizeof(msg_rcv), 0, (struct sockaddr *)&sDest, &sizeSdest);
msg_rcv[iResult]='\0'; // recvfrom renvoie le nombre d'octet lu!
if (iResult != -1)
{
printf("successful message reception.\n");
}
else
{
printf("Error n%d : Unable to receive message.\n",errno);
//exit(errno);
return errno;
}
// Display of the message without interference
printf("The message is : %s\n", msg_rcv);
//Closing the socket
if (closesocket(socket_id) == 0)
{
printf("Socket closing.\n");
}
else
{
printf("Error closing the socket.\n");
//exit(errno);
return errno;
}
return 0;
} |
J'ai rajouté la fonction "recvfrom" afin de savoir si le message était bien réceptionné mais apparemment non^^
Je pense que c'est pas rapport au choix de l’adresse IP de réception. Doit elle être la même que celle de l'ordinateur ? Le choix du port aussi est il important ?
Voici le message que le prgramme m'affiche:
Citation:
Push a letter S or Q:
S
Message sent: hello
Sockets are now usable...
Initialization of the UDP socket successful.
Sending of the socket ...
Successful message sending.
Error n34 : Unable to receive message.
Merci d'avance pour vos réponses,
Cordialement,
beaug.