Hi, a friend recommended the following winsock code to me.
#include <stdio.h>
#include <winsock2.h>
void abc(char *p)
{
char *ptr;
p[1000] = 0;
ptr = strchr(p,(int) "\r\n\r\n");
if(ptr)
ptr[4] =0;
printf("%s", p);
}
int main(void)
{
WSADATA ws;
int d;
char aa[1001];
struct sockaddr_in a;
SOCKET s;
int ii;
HOSTENT *he;
int i;
char ipaddr[64];
d = WSAStartup(0x101,&ws);
he = gethostbyname("www.personal.leeds.ac.uk");
if(he == 0)
{
printf("bad host\n");
goto abortme;
}
for(i=0;he->h_addr_list[i];i++)
{
sprintf(ipaddr,"%d.%d.%d.%d", (unsigned char) he->h_addr_list[i][0], (unsigned char) he->h_addr_list[i][1],
(unsigned char) he->h_addr_list[i][2], (unsigned char) he->h_addr_list[i][3]);
}
printf("%s\n", ipaddr);
s = socket(AF_INET,SOCK_STREAM,0);
sprintf(aa," SOCKET = %d",s);
abc(aa);
a.sin_family = AF_INET;
a.sin_port = htons(80);
a.sin_addr.s_addr = inet_addr("202.54.1.18");
a.sin_addr.s_addr = inet_addr("208.77.188.166");
a.sin_addr.s_addr = inet_addr(ipaddr);
d = connect(s, (struct sockaddr *)&a, sizeof( a));
printf("connection %d\n", d);
strcpy(aa,"GET /~bgy1mm/index.html\r\n");
strcat(aa,"HTTP 1.0 \r\n\r\n");
send(s,aa,sizeof(aa),0);
ii = 1;
while (ii != 0)
{
ii = recv(s,aa,1000,0);
aa[ii] = 0;
abc(aa);
}
closesocket(s);
abortme:
WSACleanup();
}
Only problem is in the line
ptr = strchr(p, "\r\n\r\n");
which if I type cast as:
ptr = strchr(p,(int) "\r\n\r\n");
still gives me an error code of:
[Linker error] undefined reference to `WSAStartup@8'
[Linker error] undefined reference to `gethostbyname@4'
[Linker error] undefined reference to `socket@12'
[Linker error] undefined reference to `htons@4'
[Linker error] undefined reference to `inet_addr@4'
[Linker error] undefined reference to `connect@12'
[Linker error] undefined reference to `send@16'
etc......
Plus I don't know what this code means:
a.sin_family = AF_INET;
a.sin_port = htons(80);
a.sin_addr.s_addr = inet_addr("202.54.1.18");
a.sin_addr.s_addr = inet_addr("208.77.188.166");
a.sin_addr.s_addr = inet_addr(ipaddr);
d = connect(s, (struct sockaddr *)&a, sizeof( a));
Is there any way to make this compile and run?