【发布时间】:2015-04-19 11:53:34
【问题描述】:
我想从 javascript 运行程序向 C++ 程序发送一些消息。 在发件人程序(javascript)中我使用了这个:
xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", "http://localhost:55555/?msg=" + message, false );
xmlHttp.send( null );
在接收程序(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),但我不知道如何。请帮帮我...
【问题讨论】:
-
您可能应该考虑使用某种 Web 服务器,例如 apache,您可以编写一个 C 库,apache 会将所有参数发送到该库。
-
您能具体说明为什么需要为此编写一个 c++ 服务器吗?试图理解上下文
-
使用库会更好地为您服务,GNU 项目有 libmicrohttpd,它很可能会做您想做的事情和一个不错的教程:gnu.org/software/libmicrohttpd/tutorial.html
标签: javascript c++ sockets xmlhttprequest