title: Send message to socket C
body: <p>I am totally new to socket programming. Right now I'm just trying out random stuff I found. So I have this program (code below) that uses <code>csapp.h</code> and <code>csapp.c</code>, in particular the <code>open_clientfd(hostname, port)</code> function, and RIO IO to print out whatever you enter.</p>
  
  <p><a href="https://github.com/tlozoot/cs-262-bank-protocol/blob/master/csapp.c" rel="nofollow">https://github.com/tlozoot/cs-262-bank-protocol/blob/master/csapp.c</a></p>
  
  <p>Well, when I tried running the program ./prog www.google.com 80 (I'm not even sure about the hostname and port), it is printing out weird stuff and then stop.</p>
  
  <p>Expected output:</p>
  
  <pre><code>Enter message:abc
  Echo:abc
  </code></pre>
  
  <p>Actual output:</p>
  
  <pre><code>Enter message:abc
  Echo:HTTP/1.0 400 Bad Request
  </code></pre>
  
  <p>Below is my code:</p>
  
  <pre><code>int main(int argc, char **argv)
  {
     int clientfd, port;
     char *host, buf[MAXLINE];
     rio_t rio;
     host = argv[1];  port = atoi(argv[2]);
  
     clientfd = Open_clientfd(host, port);
  
     Rio_readinitb(&amp;rio, clientfd);
     printf("Enter message:"); fflush(stdout);
  
     while (Fgets(buf, MAXLINE, stdin) != NULL) {
        Rio_writen(clientfd, buf, strlen(buf));
        Rio_readlineb(&amp;rio, buf, MAXLINE);
  
        printf("Echo:");
  
        Fputs(buf, stdout);
        printf("Enter message:");
  
        fflush(stdout);
     }
  
     Close(clientfd);
     exit(0);
  }
  </code></pre>
  
  <p>And I have another question. How do I send something like this to a socket?</p>
  
  <pre><code>GET /index.html HTTP/1.1\r\n
  Host: www.google.com\r\n
  \r\n
  </code></pre>