0

I have spring-boot server that I am running in AWS. The server works fine. I can access it using chrome, postman, and curl with no issues. However, I have an embedded device that is running C and I am using sockets to try to connect to my server. The embedded device is running Linux so I can use curl to talk to the server with no issues. However, my C sockets code cannot seem to find the resource on the server. I keep getting 404's.

Here is my embedded client code,

 #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;
}

Here is what I get back,

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:~$

If I use the curl command on the embedded device,

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

I get the correct response from the server. So I am confident that the embedded device is talking to my server. Just can't seem to get the sockets code to work. Any help would be appreciated.

skmansfield
  • 1,413
  • 3
  • 19
  • 41
  • If you execute curl with the verbose flag: curl -v 52.200.39.81:8080/hello, you can see the header it sent. Try using "GET /hello HTTP/1.1\r\n" instead of "GET http://52.200.39.81:8080/hello HTTP/1.1\r\n" – Lilás Apr 02 '16 at 19:07
  • I tried "Get /hello HTTP/1.1\r\n" with no luck. Good recommendation with curl -v ... I tried it and copied the header exactly into my program with no luck. This is very troubling. I have done this exact process before on other platforms. – skmansfield Apr 02 '16 at 19:29
  • I modified the code in this question http://stackoverflow.com/questions/11208299/http-get-request-using-c-without-libcurl. Try this code: http://pastebin.com/MNHLS77c, make hello, ./hello 52.200.39.81, it works, you can search now what is going wrong with your code by comparing – Lilás Apr 02 '16 at 19:34
  • I downloaded your code and included my url's. It worked fine. Let me spend a few minutes trying to figure out what I did wrong. I will get back to you with what I find. – skmansfield Apr 02 '16 at 19:48

1 Answers1

0

Ok, I found out what the problem was. I did not set the socket address. A very simple omission but that what the problem was. I have updated the code and it is now working. I added gethostbyname to make the code more standardized. The new lines of code are in the comment section "// Initialize server address/port struct". I would like to thank Lilas for providing me the reference code that led me to the omission of the dest.sin_addr.s_addr line. Below is the corrected code.

#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;
}
skmansfield
  • 1,413
  • 3
  • 19
  • 41