Probleme de socket sur dev c++

Signaler
Messages postés
99
Date d'inscription
lundi 8 septembre 2003
Statut
Membre
Dernière intervention
1 septembre 2010
-
Messages postés
99
Date d'inscription
lundi 8 septembre 2003
Statut
Membre
Dernière intervention
1 septembre 2010
-
bonjour,
donc voila g un gros probleme avec les socket. dès que je compile un code avec un socket, le linker me dit des truc et n'execute pas le prog. je vais vous donner le code source:

#include
using namespace std ;
#include <stdlib.h>
#include <winsock.h>
#include <string.h>

#define PORT 15 // Port arbitraire de communication

//#define DEBUG // Si vous voulez pouvoir afficher les codes d'erreur

void ErreurWSA();

void main()
{

cout << "Serveur pret." << endl;

WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(1, 1);
if (WSAStartup(wVersionRequested, &wsaData))
{
cerr << "Erreur WSAStartup : " ;
ErreurWSA();
system("PAUSE") ;
exit(1) ;
}


SOCKET s;
s = socket(AF_INET,SOCK_STREAM,0);
if(s==INVALID_SOCKET)
{
cerr << "ERREUR SOCKET : ";
ErreurWSA();
system("PAUSE") ;
exit(1) ;
}
else
cout << "SOCKET : " << s << endl;


struct sockaddr_in sain;
sain.sin_family = AF_INET;
sain.sin_port = htons(PORT);
sain.sin_addr.s_addr = INADDR_ANY;

if(bind(s, (struct sockaddr *) &sain, sizeof(struct sockaddr_in)) == SOCKET_ERROR)
{
cerr << "ERREUR BIND : ";
ErreurWSA();
system("PAUSE") ;
exit(1) ;
}

if (listen(s, 5)==SOCKET_ERROR)
{
cerr << "ERREUR LISTEN : ";
ErreurWSA();
system("PAUSE") ;
exit(1) ;
}

cout << "En attente de connection." << endl;

SOCKET ns;
struct sockaddr_in fsain;
int fromlen = sizeof(struct sockaddr_in);
ns = accept(s,(struct sockaddr *) &fsain, &fromlen);

if(ns==SOCKET_ERROR)
{
cerr << "ERREUR ACCEPT : ";
ErreurWSA();
system("PAUSE") ;
exit(1) ;
}

cout << "Connection avec le client etablie." << endl;

char buffer[64];
int rec;

do
{
rec = recv(ns,buffer,64*sizeof(char),0);
buffer[rec] = '\0' ;
cout << buffer << endl;
memset(buffer, 0, 64*sizeof(char));

}while(rec>0);

cout << "Connection fermee par le client" << endl;

if(closesocket(s)==SOCKET_ERROR)
{
cerr << "ERREUR CLOSE : ";
ErreurWSA();
system("PAUSE") ;
exit(8) ;
}

WSACleanup();

cout << endl ;
system("PAUSE");

}

#ifdef DEBUG
void ErreurWSA()
{
int error = WSAGetLastError();

switch(error)
{
case WSAEINTR:
{
cerr << "WSAEINTR" << endl;
break;
}
case WSAEBADF:
{
cerr << "WSAEBADF" << endl;
break;
}
case WSAEACCES:
{
cerr << "WSAEACCES" << endl;
break;
}
case WSAEDISCON:
{
cerr << "WSAEDISCON" << endl;
break;
}
case WSAEFAULT:
{
cerr << "WSAEFAULT" << endl;
break;
}
case WSAEINVAL:
{
cerr << "WSAEINVAL" << endl;
break;
}
case WSAEMFILE:
{
cerr << "WSAEMFILE" << endl;
break;
}
case WSAEWOULDBLOCK:
{
cerr << "WSAEWOULDBLOCK" << endl;
break;
}
case WSAEINPROGRESS:
{
cerr << "WSAEINPROGRESS" << endl;
break;
}
case WSAEALREADY:
{
cerr << "WSAEALREADY" << endl;
break;
}
case WSAENOTSOCK:
{
cerr << "WSAENOTSOCK" << endl;
break;
}
case WSAEDESTADDRREQ:
{
cerr << "WSAEDESTADDRREQ" << endl;
break;
}
case WSAEMSGSIZE:
{
cerr << "WSAEMSGSIZE" << endl;
break;
}
case WSAEPROTOTYPE:
{
cerr << "WSAEPROTOTYPE" << endl;
break;
}
case WSAENOPROTOOPT:
{
cerr << "WSAENOPROTOOPT" << endl;
break;
}
case WSAEPROTONOSUPPORT:
{
cerr << "WSAEPROTONOSUPPORT" << endl;
break;
}
case WSAESOCKTNOSUPPORT:
{
cerr << "WSAESOCKTNOSUPPORT" << endl;
break;
}
case WSAEOPNOTSUPP:
{
cerr << "WSAEOPNOTSUPP" << endl;
break;
}
case WSAEPFNOSUPPORT:
{
cerr << "WSAEPFNOSUPPORT" << endl;
break;
}
case WSAEAFNOSUPPORT:
{
cerr << "WSAEAFNOSUPPORT" << endl;
break;
}
case WSAEADDRINUSE:
{
cerr << "WSAEADDRINUSE" << endl;
break;
}
case WSAEADDRNOTAVAIL:
{
cerr << "WSAEADDRNOTAVAIL" << endl;
break;
}
case WSAENETDOWN:
{
cerr << "WSAENETDOWN" << endl;
break;
}
case WSAENETUNREACH:
{
cerr << "WSAENETUNREACH" << endl;
break;
}
case WSAENETRESET:
{
cerr << "WSAENETRESET" << endl;
break;
}
case WSAECONNABORTED:
{
cerr << "WSAECONNABORTED" << endl;
break;
}
case WSAECONNRESET:
{
cerr << "WSAECONNRESET" << endl;
break;
}
case WSAENOBUFS:
{
cerr << "WSAENOBUFS" << endl;
break;
}
case WSAEISCONN:
{
cerr << "WSAEISCONN" << endl;
break;
}
case WSAENOTCONN:
{
cerr << "WSAENOTCONN" << endl;
break;
}
case WSAESHUTDOWN:
{
cerr << "WSAESHUTDOWN" << endl;
break;
}
case WSAETOOMANYREFS:
{
cerr << "WSAETOOMANYREFS" << endl;
break;
}
case WSAETIMEDOUT:
{
cerr << "WSAETIMEDOUT" << endl;
break;
}
case WSAECONNREFUSED:
{
cerr << "WSAECONNREFUSED" << endl;
break;
}
case WSAELOOP:
{
cerr << "WSAELOOP" << endl;
break;
}
case WSAENAMETOOLONG:
{
cerr << "WSAENAMETOOLONG" << endl;
break;
}
case WSAEHOSTDOWN:
{
cerr << "WSAEHOSTDOWN" << endl;
break;
}
case WSAEHOSTUNREACH:
{
cerr << "WSAEHOSTUNREACH" << endl;
break;
}
case WSASYSNOTREADY:
{
cerr << "WSASYSNOTREADY" << endl;
break;
}
case WSAVERNOTSUPPORTED:
{
cerr << "WSAVERNOTSUPPORTED" << endl;
break;
}
case WSANOTINITIALISED:
{
cerr << "WSANOTINITIALISED" << endl;
break;
}
case WSAHOST_NOT_FOUND:
{
cerr << "WSAHOST_NOT_FOUND" << endl;
break;
}
case WSATRY_AGAIN:
{
cerr << "WSATRY_AGAIN" << endl;
break;
}
case WSANO_RECOVERY:
{
cerr << "WSANO_RECOVERY" << endl;
break;
}
case WSANO_DATA:
{
cerr << "WSANO_DATA" << endl;
break;
}
default:
{
cerr << "Erreur inconnue" << endl;
break;
}
}
}

#else //DEBUG
void ErreurWSA(){}

#endif //DEBUG

voila je le compile et il y a une fenetre qui me met :
-undefined reference to "WSAstartup@8"
- undefined reference to "socket@12"
- undefined reference to htons@4"
-undefined reference to "bind@12"
et il continue avec toute les autres fonction socket.

que dois-je faire??

SVP aidez-moi

THE THUNDERFEAR 8-) http://www.chachou-team.be.tf

3 réponses

Messages postés
21041
Date d'inscription
jeudi 23 janvier 2003
Statut
Modérateur
Dernière intervention
21 août 2019
30
manque ref sur ws2_32.lib

ciao...
BruNews, Admin CS, MVP Visual C++
Messages postés
546
Date d'inscription
vendredi 13 février 2004
Statut
Membre
Dernière intervention
9 janvier 2008
1
Je pari que t'as pas lié les librairies : libwsock32.a ( quand tu inclu winsock.h) ou libws2_32.a ( quand tu inclu winsock2.h) a ton projet

pour le faire tu vas dans les propriétés du projet puis "add library or object" et vas chercher libwsock32.a dans le repertoire lib de devcpp

voila! ;)
Messages postés
99
Date d'inscription
lundi 8 septembre 2003
Statut
Membre
Dernière intervention
1 septembre 2010

merci :)

THE THUNDERFEAR 8-) www.thunderfear.be.tf