Historique des modifications - Message

Message #4288

Sujet: C++ CHttp et CNet (classe réseau)


Type Date Auteur Contenu
Création du message 18-06-2008 20:05:49 maitrelame12545
Beaucoup de code en ce moment sur le forum je vous propose aussi du code.
Cette classe vous permettras d'utiliser la méthode GET en http et vous permet aussi d'utiliser le réseau en mode TCP/IP facilement.




CNet.cppCNet.hexemple.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (C) 2008 plutech 
 */

#include "CNet.h"


CNet* CNet::singleton = NULL;


int CNet::connection(char* server_name,int port)
{
	struct sockaddr_in serverSockAddr;
	struct hostent *serverHostEnt;
	memset(&serverSockAddr,0,sizeof(serverSockAddr));
	serverHostEnt = gethostbyname(server_name);
	if (serverHostEnt == NULL)
	{
		printf("Unable to look up server : %s\
",server_name);
		return -1;
	}
	memcpy(&serverSockAddr.sin_addr,serverHostEnt->h_addr,serverHostEnt->h_length);
	serverSockAddr.sin_port = htons(port);
	serverSockAddr.sin_family = AF_INET;

	sock = socket(AF_INET,SOCK_STREAM,0);
	if (sock<0)
	{
		printf("Socket creation failed\
");
		return -1;
	}
	if(connect(sock,(struct sockaddr *)&serverSockAddr,sizeof(serverSockAddr))<0)
	{
		printf("Connection to server failed\
");
		return -1;
	}
	return 1;
}

void CNet::send_p(char* packet)
{
    int n = send (sock, packet, sizeof (*packet), 0);
}

char* CNet::recv_p(SOCKET sock)
{
    char text[1500]; //mtu max
    int n = recv (sock, text, sizeof text - 1, 0);
    if(n == -1)
    {
        closesocket(sock);
        return "crash";
    }
    text[n] = 0;
    return text;
}

int CNet::poschaine(char *chaine1, char *chaine2, int depart){
    unsigned int i,j;
    for( i = depart; i < strlen(chaine1); i++)
    {
       if(chaine1[i] == chaine2[0])
       {
          int rate = 0;
          for(j = 0; j < strlen(chaine2); j++)if( chaine1[i+j] != chaine2[j])
              rate=1;
          if(rate == 0)return i;
       }
    }
    return -1;
}

int CNet::hextoint2(char *hex)
{
   if(strlen(hex)>0)
   {
      if(hex[0]>'0'&&hex[0]<='9')
         return hextoint2(hex+1)*16+hex[0]-'0';
      else if(hex[0]>='a'&&hex[0]<='f')
         return hextoint2(hex+1)*16+hex[0]-'a'+ 10;
      else if(hex[0]>='A'&&hex[0]<='F')
         return hextoint2(hex+1)*16+hex[0]-'A'+ 10;
      else return hextoint2(hex+1)*16;
   }
   else return 0;
}
int CNet::hextoint(char *hex)
{
   char *temp = (char*)malloc(strlen(hex));
   unsigned int i;
   for(i=0;i<strlen(hex);i++)
      temp[i]=hex[strlen(hex)-i-1];
   return hextoint2(temp);
}

int CNet::InitNetwork()
{
    #if defined WIN32
        WSADATA wsa;
        WSAStartup(MAKEWORD(2,0),&wsa);
	#endif
	return 0;
}

int CNet::CloseNetwork()
{
    #if defined WIN32
        WSACleanup();
    #endif
    return 0;
}

CHttp::CHttp()
{
    InitNetwork();
}
CHttp::~CHttp()
{
    CloseNetwork();
    delete data;
}

char *CHttp::chunked_decode(char *buffer)
{
   char *buf = (char*)malloc(256*256);
   unsigned int i=0,j,k=0;
   while(i<strlen(buffer))
   {
      char hex[5];
      unsigned int size;
      sscanf(buffer+i,"%s",hex);
      size = hextoint(hex);
      i+=strlen(hex)+2;
      for(j=0;j<size;j++)
      {
         buf[k] = buffer[i+j];
         k++;
      }
      i+=size+2;
   }
   buf[k]='\\0';
   return buf;
}

void CHttp::read_commande(char *buffer)
{
   if(poschaine(buffer,"HTTP/1.1",0)>-1)
   {
      int num;
      sscanf(buffer,"HTTP/1.1 %d",&num);
      status = num;
   }
   else if(poschaine(buffer,"Transfer-Encoding",0)>-1)
   {
      if(poschaine(buffer,"chunked",0)>-1)
         encoding = chunked;
      else encoding = normal;
   }
}

int CHttp::read_header(char *buffer)
{
      unsigned int i=0,j;
      while(i<strlen(buffer))
      {
         char *buf = (char*)malloc(256);
         for(j=0;buffer[i+j]!='\
'||buffer[i+j+1]!='\
';j++)
            buf[j] = buffer[i+j];
         buf[j]='\\0';
         read_commande(buf);
         i+=j+2;
         if(strlen(buf)==0)
            break;
      }
      return i;
}

void CHttp::save(char *filename)
{
    // Not needed for actual enginer
}

void CHttp::load(char* adress)
{
    char* host;
    char str[1024];
    strcpy(str,adress);
    char* file;
    if (strstr(adress, "http://") != NULL){
        strtok(str,"/");
        host = strtok(NULL,"/");
        file = strtok(NULL," /");
    }
    else{
        host = strtok(str," /");
        file = strtok(NULL," /");
    }
    cout << host << endl;
    cout << file << endl;
    char commande[500];
    char *buffer = (char*)malloc(256*256);
	char buf[BUFSIZE];
	int lu;
	connection(host,80);
    sprintf(commande,"GET /%s HTTP/1.1\
\
Host: %s\
\
Accept: */*\
\
\
\
",file,host);
    send(sock,commande,strlen(commande)+1,0);
	int pos=0;
	do
	{
		lu=recv(sock,&(buf[0]),BUFSIZE,0);
		strncpy(&(buffer[pos]),&(buf[0]),lu);
		pos+=lu;
	} while(lu>0);
	data = new char[strlen(buffer)+1];
	strcpy(data,buffer);
	shutdown(sock,2);
	closesocket(sock);
}

Retour

Options Liens officiels Caractéristiques Statistiques Communauté
Préférences cookies
Corrections
irrlicht
irrklang
irredit
irrxml
Propulsé par Django
xhtml 1.0
css 2.1
884 membres
1440 sujets
11337 messages
Dernier membre inscrit: Saidov17
130 invités en ligne
membre en ligne: -
RSS Feed