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 98 99 100 101 102 103 104
|
#include "SDL.h"
#include "SDL/SDL_net.h"
#include "net.h"
#include <iostream.h>
#include <stdio.h>
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
Connection::Connection(){
//init de la lib sdl
if(SDL_Init(0)==-1) {
cout << "SDL_Init: " << SDL_GetError() << endl;
exit(1);
}
atexit(SDL_Quit);
//init de lib sdllib
if(SDLNet_Init()==-1) {
cout << "SDLNet_Init: " << SDLNet_GetError() << endl;
exit(2);
}
atexit(SDLNet_Quit);
}
Connection::~Connection() {
SDLNet_Quit();
SDL_Quit();
}
bool Connection::open(char* host, int port) {
IPaddress serverIP;
if (SDLNet_ResolveHost(&serverIP, host, port) == -1){
printf ("serveur inconnu :\n %s\n ",SDLNet_GetError());
return false;
}
socket=SDLNet_TCP_Open(&serverIP);
if (!socket) {
printf ("connexion impossible :\n %s\n ",SDLNet_GetError());
return false;
}
return true;
}
void Connection::deconnection() {
if (socket)
SDLNet_TCP_Close(socket);
}
bool Connection::send( char * msg){
int len=strlen(msg);
int result=SDLNet_TCP_Send(socket, msg, len);
if ( result < len) {
printf ("probleme dans l'envoie :\n %s\n ",SDLNet_GetError());
return false;
}
return true;
}
int Connection::receive(char * msg, int taille) {
int r= SDLNet_TCP_Recv(socket, msg, taille);
if (r<=0)
{
printf ("probleme de lecture :\n %s\n ",SDLNet_GetError());
return r;
}
return r;
}
///////////////////////////////////////////////////////////////////////////////
/*
Debut de main
*/
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
Connection con;
char msg[16384];
[b]con.open("127.0.0.1", 80);[/b] // la ligne contreversé
printf("1\n");
con.send("Lets try !");
printf("2\n");
con.receive(msg, 16384);
printf("3");
return 0;
} |
Partager