#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define DEBUG 1
#pragma comment(lib,"ws2_32.lib")
SOCKET sock;
SOCKADDR_IN i_sock;
WSADATA Data;
void NetPError(void) {
switch(WSAGetLastError()) {
case WSANOTINITIALISED:
printf("A successful WSAStartup call must occur before using this function.\n");
break;
case WSAENETDOWN:
printf("The network subsystem has failed.\n");
break;
case WSAEFAULT:
printf("The buf parameter is not completely contained in a valid part of the user address space.\n");
break;
case WSAENOTCONN:
printf("The socket is not connected.\n");
break;
case WSAEINTR:
printf("The (blocking) call was canceled through WSACancelBlockingCall.\n");
break;
case WSAEINPROGRESS:
printf("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.\n");
break;
case WSAESHUTDOWN:
printf("The socket has been shut down.\n");
break;
case WSAEMSGSIZE:
printf("The message was too large to fit into the specified buffer and was truncated.\n");
break;
case WSAETIMEDOUT:
printf("The connection has been dropped because of a network failure or because the peer system failed to respond.\n");
break;
default:
printf("Unknown failure.\n");
}
}
int Connect(char *Host, char *Url, int Port) {
char RecvBuf[2048]={0}, SendBuf[2048]={0}, HTMLSource[2048]={0};
int ss=0, i=0;
struct sockaddr_in addr;
struct hostent *hp;
memset((char *) &addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(Host);
addr.sin_port= htons(Port);
WSAStartup(MAKEWORD(2,2), &Data);
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock!=INVALID_SOCKET) {
i_sock.sin_family = AF_INET;
i_sock.sin_addr.s_addr = inet_ntoa(addr.sin_addr);
if ((hp = gethostbyname(Host)) != NULL) {
if (hp->h_length > sizeof(addr.sin_addr)) {
hp->h_length = sizeof(addr.sin_addr);
}
memcpy((char *) &addr.sin_addr, hp->h_addr, hp->h_length);
printf("1.) %s : %s\n",Host,inet_ntoa(addr.sin_addr));
}
else {
if ((addr.sin_addr.s_addr = inet_addr(Host)) < 0) {
return 0;
}
else {
printf("2.) %s : %s\n",Host,inet_ntoa(addr.sin_addr));
}
}
i_sock.sin_port = htons(Port);
ss = connect(sock, (struct sockaddr *)&i_sock, sizeof(i_sock));
if(ss<0) {
printf("Connection failed : %i\n",ss);
NetPError();
return 0;
}
sprintf(Url,"http://%s/index.html",Host);
sprintf(SendBuf, "GET %s\r\nHTTP/1.0 Host: http://%s\r\n", Url, Host);
printf("%s\n",SendBuf);
if(send(sock, SendBuf, sizeof(SendBuf), 0)<0) {
printf("Unable to trasmit : %i\n",ss);
NetPError();
return 0;
}
if(recv(sock, RecvBuf, 2048, 0)<0) {
printf("Unable to recieve\n");
NetPError();
return 0;
}
closesocket(sock);
printf("%s\n",RecvBuf);
return 1;
}
return 1;
}
int main(void) {
char website[32]="example.com"; //akroncdnr.com
if(Connect(website, "index.html", 80)) {
//if(Connect("66.93.12.16", "index.html", 80)) {
printf("\nCompleted!\n");
}
return 0;
}
Quote
Connection failed : -1
Unknown failure.
Quote
GET http://example.com/index.html
HTTP/1.0 Host: http://example.com
HTTP/1.0 400 Bad Request
Pragma: no-cache
Cache-Control: no-cache
Content-Type: text/html
Content-Length: 74
<TITLE>Error</TITLE>
<BODY>
<H1>Error</H1>
Invalid HTTP request.</BODY>
ple.com/index.html
What am I missing? Before today I thought I knew how to do this

New Topic/Question
Reply



MultiQuote





|