Bonjour,

Sur une machine Linux (192.168.0.15), j'ai un serveur UDP de base:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/perl -w
 
use strict;
use IO::Socket;
 
my($sock, $newmsg, $hishost, $MAXLEN, $PORTNO);
 
$MAXLEN = 1024;
$PORTNO = 2009;
 
$sock = IO::Socket::INET->new( LocalPort => $PORTNO, Proto => 'udp') or die "socket: $@";
 
print "Awaiting UDP messages on port $PORTNO\n";
 
while ($sock->recv($newmsg, $MAXLEN))
{
	my($port, $ipaddr) = sockaddr_in($sock->peername);
	$hishost = gethostbyaddr($ipaddr, AF_INET);
	print "Client $hishost said $newmsg\n";
	$sock->send("CONFIRMED: $newmsg ");
} 
die "recv: $!";
Écrit en Perl, j'ai un client que je fais tourner via Active Perl sur une machine Windows (192.168.0.12) dont voici le code et qui marche:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#!/usr/bin/perl -w
 
use IO::Socket;
use strict;
 
my($sock, $msg, $port, $ipaddr, $hishost, $MAXLEN, $PORTNO, $TIMEOUT);
 
$MAXLEN  = 1024;
$PORTNO  = 2009;
$TIMEOUT = 5;
 
$sock = IO::Socket::INET->new(Proto => 'udp', PeerPort => $PORTNO, PeerAddr => '192.168.0.15') or die "Creating socket: $!\n";
 
$msg = 'testmessage'.time;
 
$sock->send($msg) or die "send: $!";
 
eval
{
	local $SIG{ALRM} = sub { die "alarm time out" };
	alarm $TIMEOUT;
	$sock->recv($msg, $MAXLEN) or die "recv: $!";
	alarm 0;
	1;  # return value from eval on normalcy
} or die "recv from localhost timed out after $TIMEOUT seconds.\n";
 
print "Server $hishost responded $msg\n";
Mon problème: J'essaie d'écrire un client en C, dont voici le code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <winsock2.h>
#include <windows.h>
#include <iostream>
 
#pragma comment(lib, "ws2_32.lib")
 
using namespace std;
 
SOCKADDR_IN remote;
SOCKET sock;
 
int connexionUDP( char *ip, int port )
{
	sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (!sock)
	{
		cout << "[udp.cpp][connexionUDP] Connexion UDP impossible" << endl;
		return(0);
	}
	remote.sin_family = AF_INET;
	remote.sin_addr.s_addr = inet_addr(ip);
	remote.sin_port = htons(port);
}
 
void setMessageUDP( char *message )
{
	char msg[255];
	sprintf ( msg, "%s\n", message );
	memset(msg, '\0', sizeof(msg));
	sendto(sock, msg, strlen(msg), 0, (SOCKADDR *)&remote, sizeof(SOCKADDR));
}
 
void fermeConnexionUDP()
{
	closesocket(sock);
}
Je l'exécute dans mon main par:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
connexionUDP( "192.168.0.15", 2009 );
setMessageUDP( "Message Visual C++" );
fermeConnexionUDP();
Problème, je n'ai aucune erreur mais mon serveur ne reçoit rien, alors qu'avec le script Perl sur la même machine ça marche.
Avez-vous une idée ?

Cordialement.