Solved

Trouble with sockets CGI program

Posted on 1999-01-07
4
249 Views
Last Modified: 2013-12-26
I have written a rather simple C program to submit certain information to a CGI script. However, I have tried and the program fails to work properly... Are there any problems with the program which will affect its ability to send the information to the CGI script?
(The cgi script is located at: http://www-cgi.monash.edu.au/cgi-bin/cgiwrap/~tatjana/TestCGI, and all it does is print out the contents of the form passed to it, either GET or POST is supported. This program is meant to do exactly what the browser does, and send the information to the CGI script.)

Here is the program:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PORT 80    /* the port client will be connecting to */

#define MAXDATASIZE 2000 /* max number of bytes we can get at once */

int main(int argc, char *argv[])
{
int sockfd, numbytes;
int len, bytes_sent, sent, total_sent = 0;
char buf[MAXDATASIZE];
struct hostent *he;
char *msg = "POST /cgi-bin/cgiwrap/~tatjana/TestAnswer HTTP/1.0\r\nContent_type:
application/x-www-form-urlencoded\r\nContent_length:52\r\n\r\nintlPrefix=%2B6100
4&TEL=%2B61000123454&Message=TEST2";
struct sockaddr_in their_addr; /* connector's address information */

if (argc != 2) {
     fprintf(stderr,"usage: client hostname\n");
     exit(1);
}

if ((he=gethostbyname(argv[1])) == NULL) {  /* get the host info */
      herror("gethostbyname");
      exit(1);
}

if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
      perror("socket");
      exit(1);
}

their_addr.sin_family = AF_INET;      /* host byte order */
their_addr.sin_port = htons(PORT);    /* short, network byte order */
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8);     /* zero the rest of the struct */

if (connect(sockfd, (struct sockaddr *)&their_addr, \
                             sizeof(struct sockaddr)) == -1) {
      perror("connect");
      exit(1);
}

len = strlen(msg);
bytes_sent = send(sockfd, msg, len, 0);
if (bytes_sent < len)
{
        total_sent += bytes_sent;
        sent = bytes_sent;
        do {
                len = strlen(msg);
                bytes_sent = send(sockfd, msg+sent, len, 0);
        } while (bytes_sent < len);
}
else
        total_sent = bytes_sent;
printf("\nBYTES SENT: %d", total_sent);
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
      perror("recv");
      exit(1);
}

buf[numbytes] = '\0';
printf("NUMBER OF BYTES RECEIVED: %d", numbytes);
printf("Received: %s",buf);
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
      perror("recv");
      exit(1);
}
close(sockfd);

return 0;
}

0
Comment
Question by:idek1
  • 2
4 Comments
 
LVL 2

Expert Comment

by:rdelfino
Comment Utility

Instead of trying to receive all the response in one/two recv() call(s)
do the following to receive the server response

----8<----------

while ((numbytes = recv(sockfd, buf, MAXDATASIZE, 0)) > 0)
{
    /*
      do whatever you need with the buffer,
      I suggest you append the buffer to a file and procces the file
      after the loop ends
   */
}

close(sockfd);

----8<-----

this way you won't miss anything the server sends you.

I hope this helps

Reginaldo

0
 

Author Comment

by:idek1
Comment Utility
rdelfino,
The code is appreciated, however, it only tunes up the receiving part of the program. I believe the problem must lie either in the sending part of the program or in the information being sent to the server...
At the moment, when the program is run, the server responds with "Bad Request, Your browser sent a query that this server could not understand."
When running the program I use command:
client www-cgi.monash.edu.au

0
 
LVL 2

Expert Comment

by:rdelfino
Comment Utility

So the problem isn't a socket problem. It's a protocol error. The request you sent
to the server is not correct.

See RFC2068 for complete http1.1 specification. You can find it at

http://www.rfc-editor.org/

I hope now it helps

Reginaldo

0
 
LVL 3

Accepted Solution

by:
mliberi earned 100 total points
Comment Utility
Yes, it is a protocol error.
You declared: char *msg=" ... Content_length:52 ..."

please replace the underscore with a minus.
So declare  : char *msg=" ... Content-length:52 ..."
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
zeroMAx challenge 20 76
MVC and Angular 2 70
Annoying "thing" blocks my view 4 49
wordappend challenge 8 83
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now