parseh
parseh

Reputation: 444

getting HTTP get argument using C++

I want to send some message from a javascript running program to a C++ program. in sender program (javascript) I used this:

xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", "http://localhost:55555/?msg=" + message, false );
xmlHttp.send( null );

in receiver program (C++) I used This:

printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
    printf("Failed. Error Code : %d",WSAGetLastError());
    return 1;
}

printf("Initialised.\n");

//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
    printf("Could not create socket : %d" , WSAGetLastError());
}

printf("Socket created.\n");

//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 55555 );

//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
    printf("Bind failed with error code : %d" , WSAGetLastError());
    exit(EXIT_FAILURE);
}

puts("Bind done");

Now I want to get http argument (message after '?' sign in url) but I don't know how. Help me please...

Upvotes: 1

Views: 381

Answers (1)

sanjayk79
sanjayk79

Reputation: 562

What you are essentially trying to achieve is create an HTTP server. The code you've written so far only creates a TCP socket and binds to local port 5555. What you would still want to do is let that socket listen, accept requests, and then read the data sent by the client.

However, that's just the small part of what you want to achieve. Your javascript is actually forming an HTTP request (Application Layer), and your server is listening for incoming data at Transport Layer (TCP). So, it doesn't know anything about HTTP protocol. So, you'll have to write the logic to parse the buffer you receive at TCP layer to decode it as HTTP.

You could either write code for all this or make use of some toolkit like wt or apache module 2

Upvotes: 1

Related Questions