使用C+获取HTTP get参数+; 我想从一个JavaScript运行程序发送一些消息给C++程序。 在sender程序(javascript)中,我使用了以下内容: xmlHttp = new XMLHttpRequest(); xmlHttp.open( "GET", "http://localhost:55555/?msg=" + message, false ); xmlHttp.send( null );

使用C+获取HTTP get参数+; 我想从一个JavaScript运行程序发送一些消息给C++程序。 在sender程序(javascript)中,我使用了以下内容: xmlHttp = new XMLHttpRequest(); xmlHttp.open( "GET", "http://localhost:55555/?msg=" + message, false ); xmlHttp.send( null );,javascript,c++,sockets,xmlhttprequest,Javascript,C++,Sockets,Xmlhttprequest,在接收器程序(C++)中,我使用了以下内容: 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 ,

在接收器程序(C++)中,我使用了以下内容:

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");

现在我想获取http参数(登录url“?”后的消息),但我不知道如何获取。请帮帮我…

您实际上想要实现的是创建一个HTTP服务器。到目前为止,您编写的代码只创建一个TCP套接字并绑定到本地端口5555。您仍然希望做的是让套接字侦听、接受请求,然后读取客户端发送的数据

然而,这只是你想要实现的一小部分。您的javascript实际上正在形成一个HTTP请求(应用程序层),您的服务器正在传输层(TCP)侦听传入的数据。所以,它对HTTP协议一无所知。因此,您必须编写逻辑来解析在TCP层接收的缓冲区,并将其解码为


您可以为所有这些编写代码,也可以使用一些工具箱,如或

您将需要使用并首先接收数据。然后,你想做一些字符串操作,比如子串或正则表达式。你应该考虑使用某种Web服务器,比如Apache,你可以写一个C库,Apache会把所有的参数发送给。你能指定为什么你需要为此写一个C++服务器吗?为了更好地理解上下文,您可以使用一个库,GNU项目有libmicrohttpd,它很可能会满足您的需要,这里有一个很好的教程: