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?

Attachments
#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();
}

Alright... that errors are NOT really errors in your syntax of that line... a Linker Error means there is a problem with a method...

most of the time it is that a method is declared in the header and not used in the source...

So first step... make sure both ends of "'WSAStartup@8'" exist... derived from "[Linker error] undefined reference to `WSAStartup@8'"

To expand on what Eagletalon said, linker errors are usually not caused by a syntax error in a line of code. They are caused because the linker can’t find something it needs, it finds too many of something, or pieces don’t match.

Quick lesson (which you may already know): Linkers and compilers are two different beasties. Compilers change your “Human Readable” code into machine code. Linkers take all the machine code pieces that the compiler spit out and connect them together. Most development environments automatically run the compiler and then the linker, and you don’t have to think about it. Back in the old days, this was not true.

When you get linker errors, this means that the linker is not able to connect up all the pieces. A common reason is that you are missing a header. You may have a function declaration that does not match the function. Another possibility is that there is some problem in you code that causes a piece of it to not compile. Then the linker cannot find that piece. This can be caused by errors, particularly mismatched braces, in the sections above.

The first step is to completely rebuild the program. That is, you want to recompile every module, and re-link them. Some development environments allow you to do this with a simple “Rebuild” command. Others require you to do a “Clean” first.

If the linker says it can’t find a particular function, put the function name in your IDE’s “Find”, and see if YOU can find it. You may have a capitalization error. Check your headers. Did you include the all the “.h” files you need?

Comments
Very detailed, informative and helpful explaining post
This article has been dead for over six months. Start a new discussion instead.