CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2012
    Posts
    6

    Winsock GET Request

    I am trying to make basic server with Winsock.
    How can I get the values from the arguments in a GET request from the URL?

    The URL format: http://124.1.12.23?argument1=value&argument2=value


    Code:


    Code:
    #include<io.h>
    #include<stdio.h>
    #include<winsock2.h>
    
    #pragma comment(lib,"ws2_32.lib") //Winsock Library
    
    int main(int argc , char *argv[])
    {
    	WSADATA wsa;
    	SOCKET s , new_socket;
    	struct sockaddr_in server , client;
    	int c;
    	char *message;
    
    	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( 80 );
    
    	//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");
    
    	//Listen to incoming connections
    	listen(s , 3);
    
    	//Accept and incoming connection
    	puts("Waiting for incoming connections...");
    
    	c = sizeof(struct sockaddr_in);
    
    	while( (new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET )
    	{
    		puts("Connection accepted");
    
    		//Reply to the client
    		message = "GET / HTTP/1.1 200 OK\r\nDate: Sun, 19 May 2018 08:56:53 GMT\r\nServer: Apache/2.2.14 (Win32)\r\nLast-Modified: Sat, 20 Nov 2004 07:16:26 GMT\r\nETag: \"10000000565a5-2c-3e94b66c2e680\"\r\nAccept-Ranges: bytes\r\nContent-Length: 46\r\nConnection: close\r\nContent-Type: text/html\r\nX-Pad: avoid browser bug\r\n\r\n<html><body><h1>It works!</h1></body></html>\r\n";
    		send(new_socket , message , strlen(message) , 0);
    	}
    
    	if (new_socket == INVALID_SOCKET)
    	{
    		printf("accept failed with error code : %d" , WSAGetLastError());
    		return 1;
    	}
    
    	closesocket(s);
    	WSACleanup();
    
    	return 0;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Winsock GET Request

    Quote Originally Posted by dot_binary View Post
    I am trying to make basic server with Winsock.
    How can I get the values from the arguments in a GET request from the URL?
    If you only want to parse the request then
    https://www.google.com/search?newwin...71.v3DpT38SxGI
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2012
    Posts
    6

    Re: Winsock GET Request

    How can I get the values from the URL.
    The request is made from a browser window: http://124.1.12.23/index.html?argume...rgument2=value
    On the server I need to get the values after the "?" sign.

    I'm interested in the function that receives the complete URL string with winsock before I do the parsing
    Last edited by dot_binary; November 15th, 2019 at 05:59 AM.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Winsock GET Request

    Doesn't CString::Find work?
    Or std::find?
    Victor Nijegorodov

  5. #5
    Join Date
    Jul 2012
    Posts
    6

    Re: Winsock GET Request

    When getting a request, in the main loop, I need to get the complete URL string before I do the URL parsing and respond to the client.

    Code:
    	while( (new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET ){
    		puts("Connection accepted");
    
    		//Reply to the client
    		message = "HTTP/1.1 200 OK\r\nDate: Sun, 19 May 2018 08:56:53 GMT\r\nServer: Apache/2.2.14 (Win32)\r\nLast-Modified: Sat, 20 Nov 2004 07:16:26 GMT\r\nETag: \"10000000565a5-2c-3e94b66c2e680\"\r\nAccept-Ranges: bytes\r\nContent-Length: 46\r\nConnection: close\r\nContent-Type: text/html\r\nX-Pad: avoid browser bug\r\n\r\n<html><body><h1>It works!</h1></body></html>\r\n";
    		send(new_socket , message , strlen(message) , 0);
    	}

  6. #6
    Join Date
    Jul 2012
    Posts
    6

    Re: Winsock GET Request

    Solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured