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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| class Socket
{
public:
Socket(bool var, char *ip)
{
serveur = var;
error = false;
news = false;
connected = false;
#if defined (WIN32)
WSADATA WSAData;
WSAStartup(MAKEWORD(2,2), &WSAData);
cout << "Initialisation socket : WIN32\n";
#endif
#if defined (linux)
cout << "Initialisation socket : linux\n";
#endif
if(serveur == true && !error)
{
cout << "SERVEUR : \n";
sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_family = AF_INET;
sin.sin_port = htons(23);
if(bind(sock, (SOCKADDR*)&sin, sizeof(sin)) == SOCKET_ERROR)// < associe les parametre de sin a la socket
error = true;
else
error = false;
}
if(serveur == false && !error)
{
cout << "CLIENT : \n";
sock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_addr.s_addr = inet_addr(ip);
sin.sin_family = AF_INET;//protocol TCP/IP
sin.sin_port = htons(23);//port
}
cout << "Fin Initialisation " << error << endl;
}
bool connecter()
{
if(serveur == true && !error)
{
}
if(serveur == false && !error)
{
if(connect(sock, (SOCKADDR *)&sin, sizeof(sin)) == SOCKET_ERROR)
error = true;
else
{
error = false;
connected = true;
}
}
cout << "Fin connect " << error << endl;
return error;
}
bool ecoute(int max)
{
if(serveur == true && !error)
{
if(listen(sock, max) == SOCKET_ERROR)
error = true;
else
error = false;
max_connect = max;
}
if(serveur == false && !error)
{
}
cout << "Fin ecoute " << error << endl;
return error;
}
bool accepter()
{
cout << "Debut accepeter " << error << endl;
if(serveur == true)
{
socklen_t taille = sizeof(csin);
if(error == false)
{
csock = accept(sock, (SOCKADDR*)&csin, &taille);
if(csock == INVALID_SOCKET)
error = true;
else
error = false;
}
}
if(serveur == false)
{
}
cout << "Fin accepter " << error << endl;
return error;
}
bool envoyer(char *buff)
{
news = false;
if(serveur == true)
{
if(send(csock, buff, strlen(buff), 0) >= 0)
news = true;
}
if(serveur == false && connected)
{
if(send(sock, buff, strlen(buff), 0) >= 0)
news = true;
}
return news;
}
bool recevoir(char *buff)
{
news = false;
if(serveur == true)
{
if(recv(csock, buff, sizeof(buff), 0) >= 0)
news = true;
}
if(serveur == false && connected)
{
if(recv(sock, buff, sizeof(buff), 0) >= 0)
news = true;
}
return news;
}
~Socket()
{
closesocket(sock);
if(serveur == true)
{
shutdown(csock, 2);
closesocket(csock);
}
#if defined (WIN32)
WSACleanup();
cout << "Destruction socket : WIN32\n";
#endif
#if defined (linux)
cout << "Destruction socket : linux\n";
#endif
}
private:
int max_connect;
SOCKET sock;
SOCKET csock;
SOCKADDR_IN sin;
SOCKADDR_IN csin;
bool error;
bool news;
bool connected;
bool serveur;
}; |
Partager