C network programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • niks660097
    New Member
    • Aug 2012
    • 5

    C network programming

    I am getting linker error in this program, while compiling it dev c++ 5.2.0.3
    What should i do.



    Code:
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include<winsock.h>
    #include<string.h>
    #include<stdio.h>
    #include <sys/types.h>
    
    #pragma comment(lib,"Ws2_32.lib")
    
    
    int main(int argc,char *argv[])
    {void *addr;
    WSADATA wsaData;
    	
        WSAStartup(MAKEWORD(1,1),&wsaData);
    struct sockaddr_in *ip4;
    struct addrinfo hints;
    struct addrinfo *result=NULL;
    struct addrinfo *p=NULL;
    
    char *ipver;
    struct sockaddr_in6 *ip6;
     char ipstr[46];
    
    
    hints.ai_family=AF_UNSPEC;
    hints.ai_socktype=SOCK_STREAM;
    getaddrinfo(argv[1],NULL,&hints,&result);
    printf("ip address for sever %s",argv[1]);
    printf("\n");
    for(p=result;p!=NULL;p=p->ai_next)
    {
    	if(p->ai_family==AF_INET)
    	{
    	ipver="IP4";
    	  ip4 =(struct sockaddr_in*)(p->ai_addr);
    	  addr=&(ip4->sin_addr);
    	}
    	if(p->ai_family==AF_INET6)
    	{
    	ipver="IP6";
    ip6 =(struct sockaddr_in6*)p->ai_addr;
    	addr=&(ip6->sin6_addr);
    	}
    	inet_ntop(p->ai_family,addr,ipstr,sizeof(ipstr));
    }
    freeaddrinfo(result);
    WSAcleanup();
    return 0;
        
    }
    Last edited by Banfa; Aug 24 '12, 09:30 AM. Reason: Corrected the code tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    What error are you getting?

    Comment

    • niks660097
      New Member
      • Aug 2012
      • 5

      #3
      [Linker error] C:\Users\nirmal \AppData\Local\ Temp\ccuoQO7h.o :network.c:(.te xt+0x31): undefined reference to `__imp_WSAStart up'


      [Linker error] C:\Users\nirmal \AppData\Local\ Temp\ccuoQO7h.o :network.c:(.te xt+0x7f): undefined reference to `__imp_getaddri nfo'


      [Linker error] C:\Users\nirmal \AppData\Local\ Temp\ccuoQO7h.o :network.c:(.te xt+0x15f): undefined reference to `inet_ntop'


      [Linker error] C:\Users\nirmal \AppData\Local\ Temp\ccuoQO7h.o :network.c:(.te xt+0x18d): undefined reference to `__imp_freeaddr info'


      [Linker error] C:\Users\nirmal \AppData\Local\ Temp\ccuoQO7h.o :network.c:(.te xt+0x194): undefined reference to `WSAcleanup'

      collect2: ld returned 1 exit status

      are the errors i am getting in the compile log

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        It looks like you are missing a library. Verify you have all the libraries you need available for the linker.

        Comment

        Working...