Envoie de struct dans une socket impossible !
Bonjour à tous,
Dans le cadre d'un projet je dois envoyé, via une socket, un "protocol request packet" en multicast à différent système, afin de pouvoir récupérer certaines informations de ces systèmes.
En simulant sur l'ordinateur: j'ai réussi tt d'abord à envoyer des simples char comme "hello, world" en multicast vers un "client", mais je n'arrive pas à envoyer des information plus complexe en struct, je vous montre mon programme:
Voici tt d'abord le main principal:
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 97
| /*
* workstation.c
*
* Created on: 3 août 2017
* Author: Aymeric
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "unicastSocketRcvfrom.h"
#include "multiSocketSendto.h"
#include "request.h"
#define PORT1 12345
#define PORT2 7
#define GROUP_IP "225.0.0.37"
#define ADDRIP "127.0.0.1"
/*Returns 0 if OK, a negative value if EOF.*/
scanf("\n\n%s", &request[2]);
return request[2];
}*/
int fpurge(FILE *f)
{
int c;
while((c=fgetc(f))!=EOF && c!='\n')
{ }
return (c==EOF ? -1 : 0);
}
void *send_multi(void *arg){
struct IPRequest *rq = {0};
rq->unicast_ip=10000010;
rq->unicast_port = 7;
rq->protocol_identifier=00;
while(1)
{
printf("Push a letter S or Q:\r\n");
char c = getchar();
fpurge(stdin);
if ((c=='s')||(c=='S'))
{
multiSocketSend(PORT1, GROUP_IP, rq);
}
else if ((c=='q')||(c=='Q'))
{
printf("QUIT");
break;
}
else
{
printf("Invalid key, tape 'S' to send a message, 'Q' to exit.");
}
}
pthread_exit(NULL); /* Fin du thread */
return NULL;
}
int main(void)
{
/* this variable is our reference to the second thread */
pthread_t threadSendmulti;
/* create a second thread which executes send_uni(void *arg) */
if(pthread_create(&threadSendmulti, NULL, send_multi, NULL))
{
fprintf(stderr, "Error creating threadSendmulti\n");
return 1;
}
/* wait for the second thread to finish */
if(pthread_join(threadSendmulti, NULL))
{
fprintf(stderr, "Error joining threadSendmulti\n");
return 2;
}
return 0;
} |
La fonction multiSocketSendto();
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
| #include "multiSocketSendto.h"
int multiSocketSend (int PORT,char *GROUP_IP, struct IPRequest *request){
struct sockaddr_in addr;
int msock;
WSADATA WSAData; // Contains details of the Winsock implementation
// Initialize Winsock.
if (WSAStartup (MAKEWORD(1,1), &WSAData) != 0)
{
printf("Error n°%d : Error during initialization.\n", SOCKET_ERRNO);
//exit(SOCKET_ERRNO);
return SOCKET_ERRNO;
}
//create what looks like an ordinary UDP socket
if ((msock=socket(AF_INET,SOCK_DGRAM,0)) < 0)
{
printf("Error n%d : Unable to initialize socket.\n",SOCKET_ERRNO);
//exit(SOCKET_ERRNO);
return SOCKET_ERRNO;
}
//set up destination address
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr(GROUP_IP);
addr.sin_port=htons(PORT);
//now just sendto() our destination!
if (sendto(msock,&request,sizeof(struct IPRequest),0,(struct sockaddr *) &addr,
sizeof(addr)) < 0)
{
printf("Error n%d : Unable to send message.\n",SOCKET_ERRNO);
//exit(SOCKET_ERRNO);
return SOCKET_ERRNO;
}
printf("Sending successfully!\n");
WSACleanup();
return 0;
} |
et le header de la requete (request.h)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef REQUEST_H_
#define REQUEST_H_
#include <stdint.h>
typedef uint8_t u_int8_t;
typedef uint16_t u_int16_t;
typedef uint32_t u_int32_t;
struct IPRequest
{
uint32_t unicast_ip;
uint8_t unicast_port;
uint8_t protocol_identifier;
};
#endif /* REQUEST_H_ */ |
A première vue mon programme ne présente pas d'erreur mais lorsque je lance le .exe du Debug, celui ci ne répond plus....
Merci d'avance pour votre aide;
bonne soirée !
beaug