Html 如何在windows中使用C提交表单
我正在尝试以html形式实现一个简单的web服务器。它必须使用C语言,并且适用于windows用户Html 如何在windows中使用C提交表单,html,c,windows,forms,Html,C,Windows,Forms,我正在尝试以html形式实现一个简单的web服务器。它必须使用C语言,并且适用于windows用户 /* FOR SERVER */ #define _CRT_SECURE_NO_WARNINGS #define _WINSOCK_DEPRECATED_NO_WARNINGS #include <stdlib.h> #include <string.h> #include <assert.h> #include <limits.h> #includ
/* FOR SERVER */
#define _CRT_SECURE_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <io.h>
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#include <windows.h>
short SocketCreate(void)
{
short hSocket;
printf("Create the socket\n");
hSocket = socket(AF_INET, SOCK_STREAM, 0);
return hSocket;
}
int BindCreatedSocket(int hSocket)
{
int iRetval = -1;
int ClientPort = 8888;
struct sockaddr_in remote = { 0 };
/* Internet address family */
remote.sin_family = AF_INET;
/* Any incoming interface */
remote.sin_addr.s_addr = htonl(INADDR_ANY);
remote.sin_port = htons(ClientPort); /* Local port */
iRetval = bind(hSocket, (struct sockaddr *)&remote, sizeof(remote));
return iRetval;
}
int main(int argc, char *argv[])
{
WSADATA wsa;
int socket_desc, sock, clientLen;
struct sockaddr_in client;
char clienttan_gelen_message[200] = { 0 };
char serverResponse[500] = { 0 };
char response404[50] = { 0 };
char serverResponseXmlData[] = "<body><h1>Simple Calculator</h1><br><name>Can</name><age>26</age></body>";
char serverResponseXmlData2[] = "<form action = localhost:8888>"
"<div><label>Multiplicand 1: <input name = \"m\" size = \"5\"></label></div>"
"<div><label>Multiplicand 2: <input name = \"n\" size = \"5\"></label></div>"
"<div><input type = \"submit\" value = \"Multiply!\">< / div>"
"< / form>";
char message[100] = { 0 };
const char *pMessage = "hello";
printf("Initialising winsock...\n");
// The WSAStartup function is used to start or initialise winsock library.
// It takes 2 parameters ; the first one is the version we want to load and
// second one is a WSADATA structure which will hold additional information after winsock has been loaded.
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) // wsa will hold additional info after winsock has been loaded.
{
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");
//Create socket
socket_desc = SocketCreate();
if (socket_desc == -1)
{
printf("Could not create socket");
return 1;
}
printf("Socket created\n");
//Bind
if (BindCreatedSocket(socket_desc) < 0)
{
//print the error message
perror("bind failed.");
return 1;
}
printf("bind done\n");
//Listen
listen(socket_desc, 3);
//Accept and incoming connection
while (1)
{
printf("Waiting for incoming connections...\n");
clientLen = sizeof(struct sockaddr_in);
//accept connection from an incoming client
sock = accept(socket_desc, (struct sockaddr *)&client, (int *)&clientLen);
if (sock < 0)
{
perror("accept failed");
return 1;
}
printf("Connection accepted\n");
memset(clienttan_gelen_message, '\0', sizeof clienttan_gelen_message);
memset(message, '\0', sizeof message);
//Receive a reply from the client
if (recv(sock, clienttan_gelen_message, 200, 0) < 0)
{
printf("recv failed");
break;
}
printf("Clienttan gelen mesaj: %s\n\n\n", clienttan_gelen_message);
// Send some data
//sprintf(serverResponse, "HTTP/1.0 200 OK\r\n\r\n%s\r\n", serverResponseXmlData);
sprintf(serverResponse, "HTTP/1.0 200 OK\r\n\r\n%s\r\n", serverResponseXmlData2);
if (send(sock, serverResponse, strlen(serverResponse), 0) < 0)
{
printf("Send failed");
sprintf(response404, "HTTP/1.0 404 Not Found\r\n");
send(sock, response404, strlen(response404), 0);
return 1;
}
//closesocket(sock);
Sleep(1000); // Note uppercase 'S'
}
closesocket(sock);
WSACleanup();
return 0;
}
服务器的/**/
#定义\u CRT\u安全\u无\u警告
#定义\u WINSOCK\u不推荐\u无\u警告
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#pragma注释(lib,“ws2_32.lib”)//Winsock库
#包括
短SocketCreate(void)
{
短hSocket;
printf(“创建套接字\n”);
hSocket=socket(AF_INET,SOCK_STREAM,0);
返回hSocket;
}
int BindCreatedSocket(int hsSocket)
{
int-iRetval=-1;
int ClientPort=8888;
远程中的结构sockaddr_={0};
/*因特网地址族*/
remote.sin_family=AF_INET;
/*任何传入接口*/
remote.sin_addr.s_addr=htonl(INADDR_ANY);
remote.sin_port=htons(ClientPort);/*本地端口*/
iRetval=bind(hSocket,(struct sockaddr*)&remote,sizeof(remote));
返回iRetval;
}
int main(int argc,char*argv[])
{
WSADATA wsa;
内部插座描述,插座,客户;
客户端中的结构sockaddr_;
char clienttan_gelen_消息[200]={0};
char serverResponse[500]={0};
字符响应404[50]={0};
char serverResponseXmlData[]=“简单计算器
Can26”;
char serverResponseXmlData2[]=“”
“被乘数1:”
“被乘数2:”
“