1 |
#include "Client.h" |
2 |
#include "../System/System.h" |
3 |
#include "../String/StringTools.h"
|
4 |
#include "../System/SystemDefine.h"
|
5 |
|
6 |
Client::Client(){} |
7 |
Client::~Client(){} |
8 |
|
9 |
void Client::connectToServer() |
10 |
{ |
11 |
int on = 1; |
12 |
socketDescriptor=-1; |
13 |
struct hostent* hp; |
14 |
sinSize=sizeof(sa); |
15 |
connectionStatement=true; |
16 |
char *hostName="194.47.142.206"; |
17 |
|
18 |
|
19 |
wVersionRequested = MAKEWORD( 1, 1 );
|
20 |
|
21 |
err = WSAStartup( wVersionRequested, &wsaData );
|
22 |
if ( err != 0 ) {
|
23 |
/* Tell the user that we couldn't find a useable */
|
24 |
/* winsock.dll. */
|
25 |
return;
|
26 |
}
|
27 |
|
28 |
//Create new socket that uses the ARPA Internet family of protokolls, |
29 |
//the type of communication is to be with streams. |
30 |
//Associate the new socket with a descriptor, check if creation when ok |
31 |
|
32 |
socketDescriptor = socket(AF_INET, SOCK_STREAM, 0); |
33 |
if(socketDescriptor < 0) |
34 |
{ |
35 |
System::println("Error when creating socket"); |
36 |
connectionStatement=false; |
37 |
} |
38 |
else |
39 |
{ |
40 |
System::println("Creating socket sucesfull"); |
41 |
} |
42 |
|
43 |
|
44 |
if (connectionStatement) |
45 |
{ |
46 |
hp = gethostbyname(hostName); |
47 |
if (hp == 0) |
48 |
{ |
49 |
System::println("Is a unknown host"); |
50 |
connectionStatement=false; |
51 |
} |
52 |
else |
53 |
{ |
54 |
System::println("Host is known"); |
55 |
} |
56 |
|
57 |
} |
58 |
|
59 |
//Clear struct, i.e. all bytes in the struct is inited to zero |
60 |
|
61 |
if (connectionStatement) |
62 |
{ |
63 |
//Clear struct, i.e. all bytes in the struct is inited to zero |
64 |
//memset((char*)&sa, 0, sizeof(sa)); |
65 |
|
66 |
|
67 |
|
68 |
memset(&(sa.sin_zero),0, 8);
|
69 |
sa.sin_family = AF_INET; |
70 |
sa.sin_port = htons(1000);
|
71 |
|
72 |
|
73 |
char *pointer=(char*)&sa.sin_addr;
|
74 |
|
75 |
for ( int i=0; i<hp->h_length ; i++ )
|
76 |
{
|
77 |
pointer[i]=hp->h_addr[i];
|
78 |
}
|
79 |
} |
80 |
} |
81 |
|
82 |
|
83 |
|
84 |
void Client::recieveData() |
85 |
{ |
86 |
if (connectionStatement) |
87 |
{ |
88 |
bool statement=true; |
89 |
|
90 |
while(statement) |
91 |
{ |
92 |
//Accept a connection on the socket, return a file descriptor to use |
93 |
//The struct sin will contain information about the caller |
94 |
|
95 |
int fileDescriptor = connect(socketDescriptor, (struct sockaddr *)&sa, sinSize); |
96 |
|
97 |
if(fileDescriptor >= 0) |
98 |
{ |
99 |
System::println("Accepting connection"); |
100 |
|
101 |
char *buffer=(char*)malloc(sizeof(char)*10);
|
102 |
char *inBuffer=buffer;
|
103 |
|
104 |
inBuffer[0]='b';
|
105 |
inBuffer[1]='a';
|
106 |
inBuffer[2]='j';
|
107 |
inBuffer[3]='s';
|
108 |
inBuffer[4]='a';
|
109 |
inBuffer[5]='l';
|
110 |
inBuffer[6]=0;
|
111 |
|
112 |
while(true)
|
113 |
{
|
114 |
|
115 |
if(send(fileDescriptor, inBuffer, 1,0)==-1)
|
116 |
{
|
117 |
// System::println("Send failed");
|
118 |
}
|
119 |
else
|
120 |
{
|
121 |
System::println("yes");
|
122 |
buffer++;
|
123 |
}
|
124 |
|
125 |
// sendString("blaha", fileDescriptor, 6);
|
126 |
//char *buffer=recieveString(fileDescriptor); |
127 |
//delete buffer;
|
128 |
}
|
129 |
//closesocket(fileDescriptor); |
130 |
//statement=false;
|
131 |
//System::println("yehoo"); |
132 |
} |
133 |
else |
134 |
{ |
135 |
// System::println("Error when accepting Connection"); |
136 |
// statement=false; |
137 |
} |
138 |
} |
139 |
} |
140 |
} |
141 |
|
142 |
char *Client::recieveString(int fileDescriptor) |
143 |
{ |
144 |
char *buffer=(char*)malloc(sizeof(char)*10); |
145 |
char *inBuffer=buffer; |
146 |
|
147 |
inBuffer[0]='b';
|
148 |
inBuffer[1]='a';
|
149 |
inBuffer[2]='j';
|
150 |
inBuffer[3]='s';
|
151 |
inBuffer[4]='a';
|
152 |
inBuffer[5]='l';
|
153 |
inBuffer[6]=0;
|
154 |
|
155 |
while (true)
|
156 |
{
|
157 |
if(recv(fileDescriptor, inBuffer, 1,0)==-1) |
158 |
{
|
159 |
System::println("failed");
|
160 |
}
|
161 |
else
|
162 |
{
|
163 |
System::println("sucesfull");
|
164 |
}
|
165 |
System::println(inBuffer); |
166 |
} |
167 |
|
168 |
return buffer; |
169 |
} |
170 |
|
171 |
void Client::sendString(char *buffer, int fileDescriptor, int size) |
172 |
{
|
173 |
|
174 |
} |
175 |
|
176 |
void Client::disConnectFromServer() |
177 |
{ |
178 |
if(socketDescriptor>=0 && connectionStatement == true) |
179 |
{ |
180 |
if(closesocket(socketDescriptor)>=0) |
181 |
{ |
182 |
System::println("disconnected from the server"); |
183 |
} |
184 |
else |
185 |
{ |
186 |
System::println("Couldnt disconnect from server"); |
187 |
} |
188 |
} |
189 |
else |
190 |
{ |
191 |
System::println("couldnt connect to server , so i coudnt disconnect from the server"); |
192 |
} |
193 |
} |