Thread: Help with server code !!!

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Lucknow, India
    Posts
    72

    Help with server code !!!

    hi. I'm just trying to write a Simple Client and Server code. But I got an Error in Server Program. Since I'm new in Socket Programming So I can't get this Error.
    Someone please Help and Please Explain About this Error.
    Code:
    #include<windows.h>
    #include<winsock2.h>
    #include<winsock.h>
    #include<stdio.h>
    #define PROTOPORT 5193
    #define QLEN 6
    int visits=0;
    main(argc, argv)
    int argc;
    char *argv[];
    { 
    struct hostent *ptrh;
    struct protoent *ptrp;
    struct sockaddr_in sad;
    struct sockaddr_in cad;
    int sd,sd2,port,alen;
    char buf[1000];
    #ifdef WIN32
     WSADATA wsaData;
     WSAStartup(0*0101,&wsaData);    //20
    #endif
     memset((char *)&sad,0,sizeof(sad));
     sad.sin_family=AF_INET;
     sad.sin_addr.s_addr=INADDR_ANY;
    if(argc>1)
     port=atoi(argv[1]);
    else
     port=PROTOPORT;
    if(port>0)
     sad.sin_port=htons((u_short)port);
    else
     {fprintf(stderr,"bad port number %s\n",argv[1]);
     exit(1);
     }
    if(((int)(ptrp=getprotobyname("tcp")))==0)
     {fprintf(stderr,"cannot map \"tcp\" to protocol number");   //I get Error here.
     exit(1);
     }
    sd=socket(PF_INET,SOCK_STREAM,ptrp->p_proto);
    if(sd<0)              //40
     {fprintf(stderr,"socket creation failed\n");
      exit(1);
     }
    if(bind(sd,(struct sockaddr *)&sad, sizeof(sad))<0)
     {fprintf(stderr,"bind failed\n");
      exit(1);
     }
    if(listen(sd,QLEN)<0)
     {fprintf(stderr,"listen failed\n");
      exit(1);
     }
    while(1)
    {alen=sizeof(cad);
    if((sd2=accept(sd,(struct sockaddr *)&cad,&alen))<0)
     {fprintf(stderr,"accept failed\n");
      exit(1);
     }
    visits++;
    sprintf(buf,"This server has been contacted %d times%s\n",visits,visits==1?".":"s.");
    send(sd2,buf,strlen(buf),0);
    closesocket(sd2);
    }}
    Every time I get the Error "cannot map tcp to protocol number". Someone Please Explain this Error and Help me To get out of it.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It just means that 'tcp' isn't a recognized protocol by getprotobyname(). Try "IPv4" instead maybe.
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > main(argc, argv)
    > int argc;
    > char *argv[];
    Which ancient book are you reading?
    This style is obsolete - do not use it for new code.

    Your indentation needs work as well.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Location
    Lucknow, India
    Posts
    72
    Salem..
    I just got a book from one of my Teacher and all the Programs in this Book follow the Same Pattern. Anyways I'll change this book for sure because there are so many errors in it.
    hey can Anyone modify the above Code in a Working one.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Which compiler are you using?

    > if(((int)(ptrp=getprotobyname("tcp")))==0)
    getprotobyname Function (Windows)
    Why are you casting to int, what should be a pointer?

    Also, call GetLastError() to get a more meaningful analysis of what went wrong - more so than "it didn't work".

    > WSAStartup(0*0101,&wsaData);
    What exactly is this all about?
    WSAStartup Function (Windows)
    1. Why are you writing an octal constant, when the manual makes it clear that the value has MSB/LSB components, which would make hex the natural choice.
    2. Why then make it a moot point by multiplying it with zero? Perhaps you actually meant 0x0101 and mis-read the typo horrors in your book.
    3. Why then also ignore the return result to make sure you were successful.
    4. Ideally, you should be using version 2.2 of winsock, not 1.1 (your book is really showing it's age here).

    In general, you're better off reading say beej than listening to anything your tutor has to say (especially since they're the kind of tutor who hasn't even grasped the basics of ANSI/ISO-C yet).

    Oh, and while I'm on a roll, your Camel Case Style Of Writing is annoying as well.
    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Gaurav Singh View Post
    Salem..
    I just got a book from one of my Teacher and all the Programs in this Book follow the Same Pattern.
    The same teacher that started you off on TurboC?

    Look inside the front cover of that book...
    What is the publication date?

    In the mean time...
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    http://tangentsoft.net/wskfaq/
    Last edited by CommonTater; 03-21-2011 at 09:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cygwin on win64
    By Vanzemljak in forum Tech Board
    Replies: 3
    Last Post: 01-12-2011, 04:28 PM
  2. TCP Server cannot save buffer data to mysql !
    By syedmuhd in forum C Programming
    Replies: 5
    Last Post: 09-18-2010, 08:54 PM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. [SOCKETS] Solaris server in C, NT client in VB
    By Daveg27 in forum C Programming
    Replies: 3
    Last Post: 05-23-2002, 10:02 AM
  5. Linking Apache Server Source Code
    By edwardtisdale in forum C Programming
    Replies: 2
    Last Post: 05-19-2002, 03:22 PM