Like Tree1Likes
  • 1 Post By Jimmy

Socket Programming

This is a discussion on Socket Programming within the C Programming forums, part of the General Programming Boards category; I wanna create client. c for socket programming but when I compile the code I encountered a problem. And I ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Question Socket Programming

    I wanna create client. c for socket programming but when I compile the code I encountered a problem. And I use codeblocks for ide.What's wrong?

    Errors:

    codeBlocks\SocketProgram\main.c|35|undefined reference to `WSAStartup@8'|
    codeBlocks\SocketProgram\main.c|48|undefined reference to `htons@4'|
    codeBlocks\SocketProgram\main.c|63|undefined reference to `gethostbyname@4'|
    codeBlocks\SocketProgram\main.c|71|undefined reference to `getprotobyname@4'|

    client.c
    Code:
    #ifndef unix
    #define WIN32
    #include <windows.h>
    #include <winsock.h>
    #else
    #define closesocket close
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #endif
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define PROTOPORT  5555;    /*Default protocol port */
    extern int  errno;
    char localhost[]="localhost";    /*default localhost name */
    
    main(argc, argv)
    int argc;
    char *argv[];
    {
        struct hostent *ptrh;
        struct protoent *ptrp;
        struct sockaddr_in sad;
        int sd;
        int port;
        char *host;
        int n;
        char buf[1000];
    #ifdef WIN32
        WSADATA wsaData;
        WSAStartup(0x0101,&wsaData);
    #endif
        memset((char*)&sad,0,sizeof(sad)); /* clear sockaddr structure*/
        sad.sin_family =AF_INET;
    
        if(argc>2)
        {
            port=atoi(argv[2]);
        }
        else{
        port=PROTOPORT;
        }
        if(port>0)
            sad.sin_port=htons((u_short)port);
        else
        {
            fprintf(stderr,"*bad port number %s\n",argv[2]);
            exit(1);
        }
        if(argc>1)
        {
            host=argv[1];
        }
        else {
        host=localhost;
        }
        /*convert host name to equivalent ip address and copy to sad */
    
        ptrh=gethostbyname(host);
        if(((char*)ptrh)==NULL)
        {
            fprintf(stderr,"invalid host: %s\n",host);
            exit(1);
        }
        memcpy(&sad.sin_addr,ptrh->h_addr,ptrh->h_length);
    
        if(((int)(ptrp=getprotobyname("tcp")))==0)
        {
            fprintf(stderr,"cannot map \"tcp\"to protocol number");
            exit(1);
    
        }
        /*Create a socket*/
        sd=socket(PF_INET,SOCK_STREAM,ptrp->p_proto);
        if(sd<0)
        {
        fprintf(stderr,"socket creation failed");
        exit(1);
        }
        if(connect(sd,(struct sockaddr *)&sad,sizeof(sad))<0)
        {
        fprintf(stderr,"connection failed\n");
        exit(1);
        }
        n=recv(sd,buf,sizeof(buf),0);
        while(n>0)
        {
            write(1,buf,n);
            n=recv(sd,buf,sizeof(buf),0);
        }
        closesocket(sd);
        exit(0);
        }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    For the errors it seems that you have forgotten to link the winsock library.

    Niara

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    I changed first part of code for link the winsock library.
    Code:
    #ifndef unix
    #define WIN32
    #include <windows.h>
    #pragma comment(lib, "wsock32.lib")
    #include <winsock.h>
    But the problem still continues.And i encountered same errrors.What's wrong?
    Last edited by freddydog; 07-14-2012 at 05:09 AM. Reason: fix

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    63
    Quote Originally Posted by freddydog View Post
    I changed first part of code for link the winsock library.
    Code:
     #ifndef unix #define WIN32 #include  #pragma comment(lib, "wsock32.lib") #include
    But the problem still continues.And i encountered same errrors.What's wrong?
    I don't mean to be rude, but what is wrong is that you apparently wrote about 100 lines of code without testing or even compiling once. Compile early and test early.

    Start with a minimal program that actually compiles. Add a few lines of code. Compile again. Fix problems. Test that the code does what you intended. Rinse and repeat.

    If you do something similar to this you will be able to find your own problems because you have fewer lines of code to debug. You will also learn quicker, it will be more fun to write code and you will feel a sense of achievement when you don't have to ask random people "Why doesn't my program compile?!?" all the time.
    Last edited by Jimmy; 07-14-2012 at 03:37 PM. Reason: Clarification
    laserlight likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket programming
    By ajitteli in forum C Programming
    Replies: 1
    Last Post: 04-18-2011, 12:19 AM
  2. About Device Driver Programming And Socket Programming
    By pritesh in forum Linux Programming
    Replies: 6
    Last Post: 01-23-2010, 02:46 AM
  3. HELP socket programming!!
    By Rishi. in forum Linux Programming
    Replies: 12
    Last Post: 08-13-2009, 03:15 AM
  4. Socket programming in C with socket.h
    By funzy in forum Networking/Device Communication
    Replies: 13
    Last Post: 08-29-2008, 04:12 AM
  5. which programming language should be used for socket programming?
    By albert_wong_bmw in forum Networking/Device Communication
    Replies: 8
    Last Post: 06-04-2004, 08:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21