gpt4 book ai didi

c - 简单的 C 套接字 HTTP 程序找不到资源 : 404

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:40 25 4
gpt4 key购买 nike

我有在 AWS 中运行的 spring-boot 服务器。服务器工作正常。我可以使用 chrome、postman 和 curl 毫无问题地访问它。但是,我有一个运行 C 的嵌入式设备,我正在使用套接字尝试连接到我的服务器。嵌入式设备运行的是 Linux,因此我可以使用 curl 毫无问题地与服务器通信。但是,我的 C 套接字代码似乎无法在服务器上找到资源。我不断收到 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;
}

这是我得到的结果,

debian@beaglebone:~$ ./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>
debian@beaglebone:~$

如果我在嵌入式设备上使用curl命令,

debian@beaglebone:~$ curl 52.200.39.81:8080/hello
Greetings WebServiceTest1 -- ServiceCount = 11
debian@beaglebone:~$

我从服务器得到了正确的响应。所以我确信嵌入式设备正在与我的服务器通信。只是似乎无法让套接字代码工作。任何帮助将不胜感激。

最佳答案

好的,我发现问题出在哪里了。我没有设置套接字地址。一个非常简单的遗漏,但这就是问题所在。我已经更新了代码,现在可以使用了。我添加了 gethostbyname 以使代码更加规范。新的代码行位于注释部分“//Initialize server address/port struct”。我要感谢 Lilas 为我提供了引用代码,使我省略了 dest.sin_addr.s_addr 行。以下是更正后的代码。

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>

int main() {

// Define some parameters
int sockfd, bytes_read;
struct sockaddr_in dest;
char *hostname = "skmdev1.net";
struct hostent *hostent;
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: skmdev1.net\r\n\r\n");

// Clean things up a bit before sarting
printf("\n\n");

// Build the address
hostent = gethostbyname(hostname);
if (hostent == NULL) {
printf("error: gethostbyname(\"%s\")\n", hostname);
return 0;
}

// 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));
dest.sin_family = AF_INET;
dest.sin_port = htons(8080);
dest.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr*)hostent->h_addr));

if ( dest.sin_addr.s_addr == INADDR_NONE ) {
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\n\n", buffer);
}

if (bytes_read == 0) {
printf("No Read Bytes Received\n");
}

/*---Clean up---*/
close(sockfd);
return 0;
}

关于c - 简单的 C 套接字 HTTP 程序找不到资源 : 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36377230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com