0
我有我在AWS中運行的spring-boot服務器。服務器工作正常。我可以使用chrome,postman和curl訪問它,沒有任何問題。但是,我有一個運行C的嵌入式設備,我正在使用套接字嘗試連接到我的服務器。嵌入式設備正在運行Linux,因此我可以使用curl與服務器通信而不會出現問題。但是,我的C套接字代碼似乎無法找到服務器上的資源。我一直在404。簡單的C套接字HTTP程序找不到資源:404
這裏是我的嵌入式客戶端的代碼,
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <errno.h>
int main() {
// Define some parameters
int sockfd, bytes_read;
struct sockaddr_in dest;
char buffer[4000];
char hdr[1000];
// Create Server Client Strings
bzero(hdr, sizeof(hdr));
strcpy(hdr, "GET /hello HTTP/1.1\r\n");
strcat(hdr, "Host: 52.200.39.81\r\n\r\n");
// Clean things up a bit before sarting
printf("\n\n");
// Create Socket
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Socket not created\n");
return 0;
}
// Initialize server address/port struct
bzero(&dest, sizeof(dest));
// *** Added this line to fix the code ***
dest.sin_addr.s_addr = inet_addr("52.200.39.81", &dest.sin_addr.s_addr);
dest.sin_family = AF_INET;
dest.sin_port = htons(8080);
if (inet_addr("52.200.39.81", &dest.sin_addr.s_addr) == 0) {
printf("Incorrect Address Expression\n");
return 0;
}
// Connect Socket
if (connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0) {
printf("Socket Connection Failed\n");
close(sockfd);
return 0;
}
// Send data
if (send(sockfd, hdr, strlen(hdr), 0) < 0) {
printf("Send Data Failed\n");
return 0;
}
printf("Socket successfully sent\n");
printf("\nSend Message - TxBufferSize = %d\n\n",strlen(hdr));
printf("%s", hdr);
bzero(buffer, sizeof(buffer));
bytes_read = recv(sockfd, buffer, sizeof(buffer), 0);
if (bytes_read < 0) {
printf("Read Data Failed\n");
}
if (bytes_read > 0) {
// Print out receive buffer
printf("\n\nReceived Message -- RxSize = %d \n\n", strlen(buffer));
printf("%s", buffer);
}
if (bytes_read == 0) {
printf("No Read Bytes Received\n");
}
/*---Clean up---*/
close(sockfd);
return 0;
}
下面是我回來後,
[email protected]:~$ ./helloworld
Socket successfully sent
***** Send Message -- TxBufferSize = 80 *****
GET http://52.200.39.81:8080/hello HTTP/1.1
Host: 52.200.39.81
accept: */*
***** Received Message -- RxBufferSize = 467 *****
HTTP/1.1 404 Not Found
Date: Sat, 02 Apr 2016 17:36:37 GMT
Server: Apache/2.2.22 (Debian)
Vary: Accept-Encoding
Content-Length: 283
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /hello was not found on this server.</p>
<hr>
<address>Apache/2.2.22 (Debian) Server at 52.200.39.81 Port 8080</address>
</body></html>
[email protected]:~$
如果我使用curl命令在嵌入式設備上,
[email protected]:~$ curl 52.200.39.81:8080/hello
Greetings WebServiceTest1 -- ServiceCount = 11
[email protected]:~$
我從服務器得到正確的迴應。所以我相信嵌入式設備正在與我的服務器通話。只是似乎無法讓套接字代碼工作。任何幫助,將不勝感激。
如果使用verbose標誌執行curl:curl -v 52.200.39.81:8080/hello,您可以看到它發送的標頭。嘗試使用「GET/hello HTTP/1.1 \ r \ n」而不是「GET http://52.200.39.81:8080/hello HTTP/1.1 \ r \ n」 –
我嘗試了「Get/hello HTTP/1.1 \ r \ ñ「沒有運氣。好的建議curl -v ...我試了一下,並將標題完全複製到我的程序中,沒有運氣。這很麻煩。我在其他平臺上完成了這個確切的過程。 – skmansfield
我修改了這個問題中的代碼http://stackoverflow.com/questions/11208299/http-get-request-using-c-without-libcurl。試試這個代碼:http://pastebin.com/MNHLS77c,打個招呼,./hello 52.200.39.81,它的工作原理,你可以通過比較 –