This repository was archived by the owner on Aug 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCClientTCP.cc
More file actions
139 lines (104 loc) · 3.08 KB
/
CClientTCP.cc
File metadata and controls
139 lines (104 loc) · 3.08 KB
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
#include <iostream>
#include <iomanip>
#include <sstream>
#include "CClientTCP.h"
using namespace std;
CClientTCP::CClientTCP( std::string pIp, unsigned short pPort )
{
#if defined (WIN32)
WSADATA WSAData;
mError = WSAStartup (MAKEWORD (2, 0), &WSAData);
#else
mError = 0;
#endif
mSock = socket( AF_INET, SOCK_STREAM, 0 );
// connecte la socket
connectClient( pIp, pPort );
}
CClientTCP::~CClientTCP( )
{
#if defined (WIN32)
WSACleanup ();
#endif
}
void CClientTCP::connectClient()
{
if(( mError = connect( mSock, (SOCKADDR *) &mSin, sizeof( mSin ))) < 0 )
{
cout << "Erreur lors de la connection au serveur : \n";
cout << "Verifier l'adresse ip et le port, sinon verifier que le serveur est correctement lancé\n";
}
}
void CClientTCP::connectClient( std::string pIp, unsigned short pPort )
{
mSin.sin_addr.s_addr = inet_addr( pIp.c_str() );
mSin.sin_family = AF_INET;
mSin.sin_port = htons( pPort );
connectClient();
}
void CClientTCP::disconnect()
{
closesocket( mSock );
}
void CClientTCP::sendQuery( string pQuery )
{
if( mError = send( mSock, pQuery.c_str(), pQuery.size(), 0 ) < 0 )
{
cout << "Erreur lors de l'envoie de la socket au serveur\n";
}
}
int CClientTCP::getLastError()
{
return mError;
}
void CClientTCP::clear()
{
ostringstream lStream;
lStream << "10;";
string lQuery = lStream.str();
sendQuery( lQuery );
}
void CClientTCP::drawPoint( int pX, int pY, CColor pColor )
{
ostringstream lStream;
lStream << "20;" << pColor.getRed() << ";" << pColor.getGreen() << ";" << pColor.getBlue() << ";" << pX << ";" << pY << "\n";
string lQuery = lStream.str();
sendQuery( lQuery );
}
void CClientTCP::drawSegment( int pX1, int pY1, int pX2, int pY2, CColor pColor )
{
ostringstream lStream;
lStream << "30;" << pColor.getRed() << ";" << pColor.getGreen() << ";" << pColor.getBlue() << ";" << pX1 << ";" << pY1 << ";" << pX2 << ";" << pY2 << "\n";
string lQuery = lStream.str();
sendQuery( lQuery );
}
void CClientTCP::drawCircle( int pX, int pY, int pRadius, CColor pColor, bool pFull )
{
ostringstream lStream;
if( !pFull )
{
lStream << "40;";
}
else
{
lStream << "50;";
}
lStream << pColor.getRed() << ";" << pColor.getGreen() << ";" << pColor.getBlue() << ";" << pX << ";" << pY << ";" << pRadius << "\n";
string lQuery = lStream.str();
sendQuery( lQuery );
}
void CClientTCP::drawRectangle( int pX1, int pY1, int pX2, int pY2, CColor pColor, bool pFull )
{
ostringstream lStream;
if( !pFull )
{
lStream << "60;";
}
else
{
lStream << "70;";
}
lStream << pColor.getRed() << ";" << pColor.getGreen() << ";" << pColor.getBlue() << ";" << pX1 << ";" << pY1 << ";" << pX2 << ";" << pY2 << "\n";
string lQuery = lStream.str();
sendQuery( lQuery );
}