Salve, ho scritto un piccolo client ma non funziona.
In run-time crasha improvvisamente.
Ho compilato con Code::Blocks + MinGW...
Sapreste darmi una mano?
Sono abb. disperato! :berto:
CommManager.c
---------------
codice:
#include "CommManager.h"
/* ////////////////////////////////////////////////////////////////////////// */
const char *SITE_ADDRESS = "www.site.com";
const char *SITE_PORT = "80";
const char *SITE_FILE = "/index.php";
/* ////////////////////////////////////////////////////////////////////////// */
BOOL commToWebSite(void) {
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char *sendbuf = strcat(strcat("GET ", SITE_FILE), " \r\n");
int iResult;
/* -------------------- */
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
return FALSE;
/* -------------------- */
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
/* -------------------- */
iResult = getaddrinfo(SITE_ADDRESS, SITE_PORT, &hints, &result);
if (iResult != 0) {
WSACleanup();
return FALSE;
}
/* -------------------- */
for(ptr=result; ptr!=NULL; ptr=ptr->ai_next) {
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
freeaddrinfo(result);
WSACleanup();
return FALSE;
}
/* -------------------- */
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if(iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
/* -------------------- */
break;
}
/* -------------------- */
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
WSACleanup();
return FALSE;
}
/* -------------------- */
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
WSACleanup();
return FALSE;
}
/* -------------------- */
closesocket(ConnectSocket);
WSACleanup();
/* -------------------- */
return TRUE;
}
CommManager.h
---------------
codice:
#include <string.h> /* Per effettuare operazioni sulle stringhe. */
#include <winsock2.h> /* Per usare le socket */
#include <ws2tcpip.h> /* " " */
/* ////////////////////////////////////////////////////////////////////////// */
/*
La funzione comunica con un sito web.
-------------------------
Valori di ritorno:
- FALSE, Comunicazione fallita.
- TRUE, Comunicazione effettuata.
-------------------------
Parametri:
- void
*/
BOOL commToWebSite(void);
Main.c
------
codice:
#include <windows.h>
#include "CommManager.h"
/* ////////////////////////////////////////////////////////////////////////// */
void errMsgBox(char *msg) {
MessageBox(NULL, msg, "Error", MB_OK|MB_ICONERROR);
}
int WINAPI WinMain(HINSTANCE i, HINSTANCE pi, LPSTR cl, int cs) {
if(!commToWebSite())
errMsgBox("Comunicazione fallita!");
return 0;
}
La cosa che mi fa più ridere è che ho preso spunto dal sorgente pubblicato su MSDN!?
Inoltre ho linkato tutte le librerie necessarie, tipo la ws2_32, user32, kernel32...
Ditemi un po' voi cosa ne pensate.