2013-06-21 57 views
0

我是套接字編程的新手。我正在嘗試創建一個HTTPS網站的套接字。套接字創建成功,我能夠連接使用和發送標題...但作爲來自recv()的迴應我沒有得到任何標題響應既沒有我得到-1 ...程序只是阻止那裏..該網站有HTTPS打開端口443 ......沒有數據收到recv()爲https請求在c + +

#include<iostream> 
#include<cstring> 
#include<sys/socket.h> 
#include<netdb.h> 
using namespace std; 
int main() 
{ 
    int status; 
    struct addrinfo hostInfo; 
    struct addrinfo *hostList; 
    memset(&hostInfo, 0, sizeof(hostInfo)); 
    hostInfo.ai_family = AF_INET;  
     hostInfo.ai_socktype = SOCK_STREAM; 
    cout<<"Setting up the structs..."<<endl; 
    status = getaddrinfo("academics.vit.ac.in", "https", &hostInfo, &hostList); 
    if(status == 0) 
    { 
     cout<<"Success"<<endl; 
     //cout<<hostList->ai_addr<<" "<<hostList->ai_socktype; 
    } 
    else 
    { 
     cout<<"Failled!"; 
    } 
    int socketd; 
    socketd = socket(hostList->ai_family, hostList->ai_socktype, hostList->ai_protocol); 
    if(socketd == -1) 
    { 
     cout<<"Socket Error\n"; 
    } 
    else 
    { 
     cout<<"Socket Success\n"; 
    } 
    cout<<"Connecting\n"; 
    status = connect(socketd, hostList->ai_addr, hostList->ai_addrlen); 
    if(status == 0) 
    { 
     cout<<"Success"<<endl; 
     //cout<<hostList->ai_addr<<" "<<hostList->ai_socktype; 
    } 
    else 
    { 
     cout<<"Failled!"; 
    } 
    cout<<"\nSending Header\n"; 
    char *msg = "GET /student/stud_login.asp HTTP/1.1\nhost: academics.vit.ac.in\nConnection: keep-alive\nCache-Control: max-age=0\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36\nReferer: https://academics.vit.ac.in/\nAccept-Encoding: gzip,deflate,sdch\nAccept-Language: en-US,en;q=0.8\nCookie: ASPSESSIONIDAWRSBBBS=HJBNNABBBAIADHKAGEFLELJK; ASPSESSIONIDCUTRADBT=GNMNDGBBFDJHBLHABHDGCCOO; ASPSESSIONIDCUQTABAT=IOAAMMNBACANEDJFMGMLMNJA; ASPSESSIONIDAUSQBCAS=GBNFBCOBEKIOJJHFPMJNMJII\n\n"; 
cout<<"\n"<<msg<<endl; 
    int len = strlen(msg); 
    ssize_t msgSize; 
    msgSize = send(socketd, msg, len, 0); 
    if(msgSize == len) 
    { 
     cout<<"Sending Header Successful\n"; 
    } 
    else 
    { 
     cout<<"Error Sending Header\n"; 
    } 
    cout<<"Waiting to recieve data\n"; 
    char rmsg[1000]; 
    msgSize = recv(socketd, rmsg, 10, 0); 
    cout<<msgSize<<rmsg<<endl; 
} 

感謝提前:)

+2

您正在寫入期望SSL協議併發送純文本的服務器端口。您需要使用適當的庫來執行協議握手和加密。 –

+0

我相信標準做法是爲此使用openssl庫。 – Wug

+0

那麼這是否意味着我無法直接發送https請求的標頭? –

回答

-1

你應該(用fnctl())的插槽設置爲非阻塞,然後看看有什麼的recv()返回。 檢查errno。這應該給你更多的信息。

查看man 2 recv()@http://linux.die.net/man/2/recv

+0

我得到的錯誤是「資源暫時不可用」... bt同樣的事情在函數getaddrinfo我更改「https」到「80」 –

相關問題