Salut, Ci joint un bout de code qui se connecte a http://www.facultyof1000.com, qui effectue la requete http://www.facultyof1000.com/whatsmyip.asp, et qui l'affiche.
Alors, j'ai jamais touché a linux donc je sais pas vraiment si c'est "portable". Mais c'est codé avec les sockets simples donc je pense que tu n'aura pas trop de mal a adapter tout ca selon tes besoins.
#include <stdio.h> #include <winsock2.h> #include <windows.h> #pragma comment(lib, "ws2_32.lib")
SOCKET s; in_addr addr; sockaddr_in sin; hostent * host; WSADATA wsa; char buf[1024]; int octet = 0;
int main(void) { WSAStartup(0x0202, &wsa);
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(s == SOCKET_ERROR){ printf("socket : %d\n", WSAGetLastError()); }
host = gethostbyname("www.facultyof1000.com");
memcpy(&addr, host->h_addr, host->h_length); sin.sin_family = AF_INET; sin.sin_port = htons(80); // HTTP sin.sin_addr = addr;
if(SOCKET_ERROR == connect(s,(sockaddr*)&sin, sizeof(sin))){ printf("connect : %d\n", WSAGetLastError()); }
strcpy(buf, "GET http://www.facultyof1000.com/whatsmyip.asp HTTP/1.0\n\n"); // la requete http send(s, buf, strlen(buf), 0);
octet = recv(s, buf, sizeof(buf), 0);
for(int i=0; i<octet; i++) printf("%c", buf[i]);
closesocket(s); WSACleanup(); return 0; }
|