Sponsored Content
Top Forums Programming C++ http GET request using sockets Post 302644359 by flagman5 on Monday 21st of May 2012 05:27:20 PM
Old 05-21-2012
C++ http GET request using sockets

Hello

I am trying to communicate with a server that is ready to accept HTTP GET requests and send back data per the request. However, I have very little experience in socket programming and I don't really know how to debug this. Googling on the web hasn't yielded much, except people saying I need to read the HTTP 1.1 spec which is the last thing I want to do.

I seem to be able to create, bind the socket and then send a GET request and that seems ok too but I am not able to receive anything.

The following are the relevant parts of the code:

Connection established:
Code:
/* connect to host */
 if(connect(hSocket,(struct sockaddr*)&Address,sizeof(Address)) 
    == SOCKET_ERROR)
 {
   verbose("Could not connect to host a socket");
  return 0;
 }
 
 verbose("Connection established")

Sending request:
Code:
char *getRequest = "GET /path/to/server.aspx?Action=GetConfig&MachineName=node1 HTTP/1.1\n";
 
 if(send(hSocket, getRequest, strlen(getRequest), 0) < 0) {
  verbose("Error with sending socket");
  return 0;
 }
 else {
  verbose("Successful with sending socket get request to get config file");
 }

The above works fine, but then below the receiving fails, in the first if-statement:
Code:
verbose("Attempting to receive from server");
 char echoBuffer[9999];
 
 /* Receive config information from server */ 
    int totalBytesRcvd = 0; 
    unsigned int echoStringLen;
    int bytesRcvd;
    printf("Received: ");                /* Setup to print the echoed string */ 
    while (totalBytesRcvd < echoStringLen) 
    { 
        /* Receive up to the buffer size (minus 1 to leave space for 
           a null terminator) bytes from the sender */ 
        if ((bytesRcvd = recv(hSocket, echoBuffer, 9999 - 1, 0)) <= 0) 
        {
            printf("recv() failed or connection closed prematurely"); 
            return 0;
        }
        totalBytesRcvd += bytesRcvd;   /* Keep tally of total bytes */ 
        echoBuffer[bytesRcvd] = '\0';  /* Terminate the string! */ 
        printf("%s", echoBuffer);      /* Print the echo buffer */ 
    }

Can anyone share some insight as to how I can re-write the receiving code to get the data that is supposed to be returned to be based on the GET request? What is wrong here?

Thanks for the help.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

unix script http request

Hi everybody, I have a *.vbs file which I want to run automatically. I want to know if there is anyway to implement the given example for e.g "http://255.255.255.55/script.vbs" what I mean is does anyone know how to make an http request from a unix script?? Thanks in advance!!!!!!!!!!! (1 Reply)
Discussion started by: arksal
1 Replies

2. Shell Programming and Scripting

Http request in Linux

Hi, i need a guide how to write a script which i can do a http request. Let say the request look like below; http://www.test.com?txid=1&type=service&server=linux I have a list of "txid" (in *.txt) and need to run all "txid" acordingly. So that mean, every transaction i have to refer "txid"... (7 Replies)
Discussion started by: malaysoul
7 Replies

3. UNIX for Dummies Questions & Answers

http request forward

Hi, Maybe it's a stupid question, anyway here goes.. I have an Apache web server on Solaris box, let's say A, with a public ip and a web application on a Linux box, uhmmm B, on a private lan with a private ip. I want the people from outside to connect to the app, but its inside the lan,... (4 Replies)
Discussion started by: piltrafa
4 Replies

4. UNIX for Dummies Questions & Answers

HTTP request

Can anybody tell about http request processing in shell script..? (3 Replies)
Discussion started by: noufal
3 Replies

5. UNIX for Dummies Questions & Answers

How to send a Head Http request from command line

Ok. I'm testing a new program component that is supposed to speed up our pipeline on the server. What it does is take in HEAD requests and shuffle them off to a different pipeline that is specially designed to handle HEAD requests quickly. The functionality works but now comes the question: how... (3 Replies)
Discussion started by: darkling235
3 Replies

6. Shell Programming and Scripting

How to send XML data using HTTP Post Request

How to hit HTTP Post Request along with sending XML data to a Remote server through command line utility like wget (or anything else). (0 Replies)
Discussion started by: sandeep reddy
0 Replies

7. UNIX for Dummies Questions & Answers

cant make a http get request using wget

Hi all, Im trying to make an http get request to a web service from a linux machine like below and i get ERROR 500 wget http://10.1.21.236:8585/pns.asmx/Sen...&msgBody=werty 25018 $ --19:06:32-- http://10.1.21.236:8585/pns.asmx/Sen...erName=serverA Connecting to 10.1.21.236:8585...... (1 Reply)
Discussion started by: elthox
1 Replies

8. Web Development

Copy and forward apache http request

Hello, I am using apache 2.2 and I need to have certain http requests (those including example.com for instance) to be executed normally and forwarded to another server. With mod_rewrite, I could easily forward but then the input request would not be executed on my server. Right? Am I... (1 Reply)
Discussion started by: JCR
1 Replies

9. Shell Programming and Scripting

http request

I am running a website but I still have problems with the "service temporarily unavailable error". I want to make a simple check if the website is up and running. Does anybody has an idea how to do it? (the site is password protected, so you have to add a user and pwd before logging in). ... (2 Replies)
Discussion started by: jurgen
2 Replies

10. Shell Programming and Scripting

Parsing the http post request

Hi, I am trying to write a shell script to parse the post request data that it received to a xml file. Below is the post request data that script is receiving. -----------------------------7dd2339190c8e Content-Disposition: form-data; name="param1" 1... (2 Replies)
Discussion started by: jdp
2 Replies
nca(1)																	    nca(1)

NAME
nca, snca - the Solaris Network Cache and Accelerator (NCA) The Solaris Network Cache and Accelerator ("NCA") is a kernel module designed to provide improved web server performance. The kernel mod- ule, ncakmod, services HTTP requests. To improve the performance of servicing HTTP requests, the NCA kernel module maintains an in-kernel cache of web pages. If the NCA kernel module cannot service the request itself, it passes the request to the http daemon (httpd). It uses either a sockets interface, with family type designated PF_NCA, or a private Solaris doors interface that is based on the Solaris doors RPC mechanism, to pass the request. To use the sockets interface, the web server must open a socket of family type PF_NCA. The PF_NCA family supports only SOCK_STREAM and pro- tocol 0, otherwise an error occurs. The following features are not presently supported: o You cannot initiate a connection from a PF_NCA type socket. The connect(3SOCKET) interface on PF_NCA fails. o System calls that are associated with type SO_DGRAM, such as send(), sendto(), sendmsg(), recv(), recvfrom(), and recvmsg(), fails. o You cannot set TCP or IP options on a PF_NCA type socket through setsockopt(3SOCKET). The NCA cache consistency is maintained by honoring HTTP headers that deal with a given content type and expiration date, much the same way as a proxy cache. For configuration information, see When native PF_NCA socket support does not exist in the web server, the ncad_addr(4) interface must be used to provide NCA support in that web server. NCA is intended to be run on a dedicated web server. Running other large processes while running NCA might cause undesirable behavior. NCA supports the logging of in-kernel cache hits. See ncalogd.conf(4). NCA stores logs in a binary format. Use the ncab2clf(1) utility to convert the log from a binary format to the Common Log File format. /etc/nca/ncakmod.conf Lists configuration parameters for NCA. /etc/nca/ncalogd.conf Lists configuration parameters for NCA logging. /etc/nca/nca.if Lists the physical interfaces on which NCA runs. /etc/nca/ncaport.conf ncaport configuration file /etc/hostname.{}{0-9} Lists all physical interfaces configured on the server. /etc/hosts Lists all host names associated with the server. Entries in this file must match with entries in /etc/host- name.{}{0-9} for NCA to function. See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWncar (32-bit) | +-----------------------------+-----------------------------+ | |SUNWncarx (64-bit) | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ ncab2clf(1), ncakmod(1), close(2), read(2), write(2), sendfilev(3EXT), accept(3SOCKET), bind(3SOCKET)connect(3SOCKET), door_bind(3DOOR), door_call(3DOOR), door_create(3DOOR), getsockopt(3SOCKET), listen(3SOCKET), setsockopt(3SOCKET), shutdown(3SOCKET), socket.h(3HEAD), socket(3SOCKET), ncad_addr(4), nca.if(4), ncakmod.conf(4), ncaport.conf(4), ncalogd.conf(4), attributes(5) 22 Feb 2005 nca(1)
All times are GMT -4. The time now is 10:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy