title: C-Socket Programming-"undefined reference"
body: <p>I am a beginner in socket programming in C. I got the code in the book and when I compiled, these are following error with undefined reference. Please give a tips to correct this!Thank you!</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;winsock.h&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  #define RCVBUFSIZE 32
  int main(int argc, char *argv[]){
  int sock;
  struct sockaddr_in echoServAddr;
  unsigned short echoServPort;
  char *servIP;
  char *echoString;
  char echoBuffer[RCVBUFSIZE];
  unsigned int echoStringLen;
  int bytesRcvd, totalBytesRcvd;
  if(argc&gt;3 || argc&gt;4){
      printf("Usage: %s &lt;Server IP&gt; &lt;Echo Word&gt; [&lt;Echo Port&gt;\n",argv[0]);
      exit(1);
  }
  servIP=argv[1];
  echoString=argv[2];
  if(argc==4){
      echoServPort=atoi(argv[3]);
  }
  else{
      echoServPort=7;
  }
  if((sock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))&lt;0){
      printf("socket() failed!");
  }
  memset(&amp;echoServAddr,0,sizeof(echoServAddr));
  echoServAddr.sin_family=AF_INET;
  echoServAddr.sin_addr.s_addr=inet_addr(servIP);
  echoServAddr.sin_port=htons(echoServPort);
  if (connect(sock, (struct sockaddr *) &amp;echoServAddr, sizeof(echoServAddr)) &lt; 0){
      printf("connect() failed!");
  }
  echoStringLen=strlen(echoString);
  if(send(sock,echoString, echoStringLen,0)!=echoStringLen){
      printf("send() send maximum bytes than expected");
  }
  totalBytesRcvd=0;
  printf("Received");
  while(totalBytesRcvd&lt;echoStringLen){
      if((bytesRcvd=recv(sock,echoBuffer,RCVBUFSIZE-1,0))&lt;=0){
          printf("recv() failed!");
      }
      totalBytesRcvd+=bytesRcvd;
      echoBuffer[bytesRcvd]='\0';
      printf(echoBuffer);
  }
  close(sock);
  exit(1);    
  }
  </code></pre>
  
  <p>I got errors as follow:</p>
  
  <pre><code>In function `main':
          client.cpp:34: undefined reference to `_socket@12'
          client.cpp:39: undefined reference to `_inet_addr@4'
          client.cpp:40: undefined reference to `_htons@4'
          client.cpp:41: undefined reference to `_connect@12'
          client.cpp:45: undefined reference to `_send@16'
          client.cpp:51: undefined reference to `_recv@16'
          collect2.exe    [Error] ld returned 1 exit status
  </code></pre>