挂断HTTP Get / Request

时间:2015-01-25 05:19:59

标签: html c sockets get

我不确定我的节目在哪里/如何挂断。我非常有信心这是发送或接收链接...但即使在互联网上搜索一些帮助并得到我的教授的帮助,我仍然迷失了,为什么这不起作用。

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define LENGTH 500 

int main(int argc, char** argv) 
{   
  // holds the value of the command-line argument
  char link[LENGTH]; 
  strcpy (link , argv[1]);  
  printf ("%s\n", link); 

  // break up link into the host and path -- use the "/"
  char _host[LENGTH];
  char _path[LENGTH]; 
  int slashCount = 0;
  int i; 

  for (i=0 ; i<strlen(link); i++)
  {
    printf ("%c , slashcount: %d\n", link[i], slashCount);
    if (link[i] == '/') slashCount ++; 
    if (slashCount > 2)
    {
       // i is at the end of the host   
       strncpy (_host , link , i);
       _host[i] = '\0';
       break; 
    }

  } // end for

 if (slashCount == 2)
 {
    // given only a host, copy the entire string into the host
   strncpy(_host, link, i); 
   _host[i] = '\0'; 
 }

 else  // get the path for slashCount of 3 
 {
   for (i ; strlen(link); i++ )
   {
       strcat (_path, &link[i]);
       // if slash at the end of host name 
       if (strlen(_path) == 1)
       {
           // set the file field to be index.html
           char t[11] = {'i','n','d','e','x','.','h','t','m','l','\0'}; 
           for (int c=0; c< 10 ; c++)
           strncpy(_path, t, 11);
       }
       break; 
   }
 } // end if/else 

 printf ("%s\n" , _host); /*success*/
 printf ("%s\n" , _path); /*success*/ 

 struct addrinfo hints;
 struct addrinfo * result, * rp;
 int sock_fd, s;

 hints.ai_socktype = SOCK_STREAM;
 memset(&hints,0,sizeof(struct addrinfo)); // setting the values to 0 

 s = getaddrinfo("::1","http", &hints,&result); /////////////// 
 if (0 != s)
 {
   perror("error populating address structure");
   exit(1);
 }
 printf ("entering tcp setup connections"); 

 // check the host name’s IP address — has a dynamic configuration(DNS)
 // but does not change very frequently 
 for (rp = result; rp != NULL; rp = rp->ai_next) 
 {
   sock_fd = socket(rp->ai_family, rp->ai_socktype,
    rp->ai_protocol);
   if (sock_fd == -1)
      continue;

   if (connect(sock_fd, rp->ai_addr, rp->ai_addrlen) != -1)
     break; 

   close(sock_fd);
 }

 if (rp == NULL) 
 {
   // you didn’t find a good ip address to connect with 
   fprintf(stderr, "could not connect\n");
   exit(1);
 } // end for 

 freeaddrinfo(result);

 // sending the request
 sprintf (link, "GET %s HTTP/1.0\r\n" "Host: %s\r\n\r\n", _path,    _host);  
 if (send (sock_fd, link, strlen(link), 0 )< 0)
   fprintf (stderr, "Error with send\n"); 
 else 
   fprintf (stderr, "Successfully send the fetch request\n");  

 // responding to the send 
 char buf[500];
 memset(&buf,0,sizeof(buf));
  /*  int recv_count = recv(sock_fd, buf, 500, 0);

 if(recv_count<0) 
 { 
   perror("Receive failed");    
   exit(1); 
 }

 printf("%s",buf); */                   
/*  while (0 > recv (sock_fd, buf, 500, 0))
 {
    printf ("%s", buf); 
 }
 shutdown(sock_fd,SHUT_RDWR); */ 
 exit (1); 
}

我知道我必须发送一个get请求,并且我注释掉了响应部分,并看到成功进入tcp连接设置。我很困惑应该如何设置响应。我理解如何使用读/写,但我也想尝试send / recv。

非常感谢任何帮助。

我目前得到的输出:

http://google.com/
h , slashcount: 0
t , slashcount: 0
t , slashcount: 0
p , slashcount: 0
: , slashcount: 0
/ , slashcount: 0
/ , slashcount: 1
g , slashcount: 2
o , slashcount: 2
o , slashcount: 2
g , slashcount: 2
l , slashcount: 2
e , slashcount: 2
. , slashcount: 2
c , slashcount: 2
o , slashcount: 2
m , slashcount: 2
/ , slashcount: 2
http://google.com
index.html
Successfully send the fetch request
entering tcp setup connections

1 个答案:

答案 0 :(得分:0)

recv()的返回值表示Connection refused,这表示套接字设置存在问题。

您需要颠倒两行的顺序:

hints.ai_socktype = SOCK_STREAM;
memset(&hints,0,sizeof(struct addrinfo)); // setting the values to 0

以便在将内存设置为零后分配ai_socktype

在作为第一个参数的getaddrinfo()调用传递中,从cli收到的字符串link::1localhost的别名)。

s = getaddrinfo(link,"http", &hints,&result); ///////////////

更改代码会导致:

woof@fth ~ % ./test.o google.com        
google.com
sock_fd=3
socket opened
Successfully sent the fetch request
HTTP/1.0 404 Not Found
Content-Type: text/html; charset=UTF-8
X-Content-Type-Options: nosniff
Date: Sun, 25 Jan 2015 11:07:09 GMT
Server: sffe
Content-Length: 1425
X-XSS-Protection: 1; mode=block
Alternate-Protocol: 80:quic,p=0.02

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}%

表示服务器已响应。

找不到该文件,因为在

行中
sprintf (link, "GET %s HTTP/1.0\r\n" "Host: %s\r\n\r\n", _path,    _host);  

您请求不存在的路径;如果你试试

sprintf (link, "GET / HTTP/1.0\n\n");  

你应该看到不同的东西(可能是另一个错误)。如果你想获得一个不同的路径,你需要在请求中正确插入它(从输入字符串中识别它)。


我编译的代码与您的代码几乎相同:

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>


#define LENGTH 500 

int main(int argc, char** argv) {
    // holds the value of the command-line argument
    char link[LENGTH]; 
    strcpy (link , argv[1]);  
    printf ("%s\n", link); 

    // break up link into the host and path -- use the "/"
    char _host[LENGTH];
    char _path[LENGTH]; 
    int slashCount = 0;
    int i,c; 

    for (i=0 ; i<strlen(link); i++) {

        if (link[i] == '/')
            slashCount ++; 

        if (slashCount > 2) {
            // i is at the end of the host   
            strncpy (_host , link , i);
            _host[i] = '\0';
            break; 
        }
    }

    if (slashCount == 2) {
        // given only a host, copy the entire string into the host
        strncpy(_host, link, i); 
        _host[i] = '\0'; 
    }
    else {
        // get the path for slashCount of 3 
        for (i ; strlen(link); i++ ) {
            strcat (_path, &link[i]);
            // if slash at the end of host name 
            if (strlen(_path) == 1)
            {
                // set the file field to be index.html
                char t[11] = {'i','n','d','e','x','.','h','t','m','l','\0'}; 
                strncpy(_path, t, 11);
            }
            break; 
        }
    } // end if/else 

    struct addrinfo hints;
    struct addrinfo *result, *rp;
    int sock_fd, s;

    memset(&hints,0,sizeof(struct addrinfo)); // setting the values to 0 
    hints.ai_socktype = SOCK_STREAM; // tcp socket

    // create addrinfo structure with parameters to initialize a socket
    s = getaddrinfo("::1","8080", &hints,&result); /////////////// 

    if( s!=0 ) {
        perror("error populating address structure");
        exit(1);
    }

    char temp[5000];

    // check the host name’s IP address — has a dynamic configuration(DNS)
    // but does not change very frequently 
    for( rp=result; rp!=NULL; rp=rp->ai_next ) {
        sock_fd = socket(rp->ai_family, rp->ai_socktype,rp->ai_protocol);
        fprintf(stderr,"sock_fd=%d\n",sock_fd);

        if (sock_fd == -1)
            continue;

        // exit loop on success
        if( connect(sock_fd, rp->ai_addr, rp->ai_addrlen)==0 )
            break;
        else
            close(sock_fd);
    }

    if( rp==NULL ) {
        // you didn’t find a good ip address to connect with 
        fprintf(stderr, "could not connect\n");
        exit(1);
    }
    else
        printf("socket opened\n");

    freeaddrinfo(result);


    // formatting the request string
    sprintf (link, "GET %s HTTP/1.0\r\n" "Host: %s\r\n\r\n", _path,    _host);  
    // sending the request
    if( send(sock_fd, link, strlen(link), 0)<0 )
        fprintf(stderr, "Error with send\n");
    else 
        fprintf (stderr, "Successfully sent the fetch request\n");  

    // response buffer
    char buf[500];
    memset(&buf,0,sizeof(buf));

    int recv_count = recv(sock_fd, buf, 500, 0);

    if( recv_count<0 ){
       perror("Receive failed");    
       exit(1); 
    }

    printf("%s",buf);
/*  while (0 > recv (sock_fd, buf, 500, 0))
 {
    printf ("%s", buf); 
 }
 shutdown(sock_fd,SHUT_RDWR); */ 
    exit (0); 
}

更改了函数调用并恢复了sprintf格式化GET请求。您应该在单独的终端上启动命令

echo "Here is a message" | nc -l -6 8080

暂停执行直到您在端口上收到请求(标记-l,有关详细信息,请参阅netcat manual)。

请求打印在netcat终端上并且进程终止,而在启动编译代码的终端上,您应该看到消息在netcat中回显和管道传输。