Tämä näyttäisi toimivan, ehkä siitä on seuraavalla kerralla jollekin apua. Linkkien tekstejä en löytänyt enää uudestaan.
// https://aticleworld.com/socket-programming-in-c-using-tcpip/
// https://www.tutorialspoint.com/http/http_requests.htm
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string>
#include <iostream>
class Postman
{
public:
int hSocket, read_size;
struct sockaddr_in server;
char server_reply[1024] = {0};
std::string rq, rq1, rq2, rq3, msg;
Postman()
{
rq1 = rq1 + "POST /koehakemisto/koesivu.php HTTP/1.1\r\n" +
"Host: xxxxx.fi\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: "; // tässä kohtaan content pituus eli rq2
rq3 = rq3 + "\r\n" +
"Connection: close\r\n\r\n";
// vielä content perään, se tulee combine_requestissa contentista
}
int combine_request(std::string content)
{
rq2 = std::to_string(content.size() );
rq = rq1 + rq2 + rq3 + content;
return 0;
}
short SocketCreate(void) //Create a Socket for server communication
{
short hSocket;
printf("Create the socket\n");
hSocket = socket(AF_INET, SOCK_STREAM, 0);
return hSocket;
}
int SocketConnect(int hSocket) //try to connect with server
{
int iRetval=-1;
int ServerPort = 80;
struct sockaddr_in remote= {0};
remote.sin_addr.s_addr = inet_addr("xxx.xxx.xxx.xxx"); // oli 127.0.0.1 //Local Host
remote.sin_family = AF_INET;
remote.sin_port = htons(ServerPort);
iRetval = connect(hSocket,(struct sockaddr *)&remote,sizeof(struct sockaddr_in));
return iRetval;
}
int SocketSend(int hSocket,char* Rqst,short lenRqst) // Send the data to the server
{ // and set the timeout of 20 seconds
int shortRetval = -1;
struct timeval tv;
tv.tv_sec = 20; /* 20 Secs Timeout */
tv.tv_usec = 0;
if(setsockopt(hSocket,SOL_SOCKET,SO_SNDTIMEO,(char *)&tv,sizeof(tv)) < 0)
{
printf("Time Out\n");
return -1;
}
shortRetval = send(hSocket, Rqst, lenRqst, 0);
return shortRetval;
}
int SocketReceive(int hSocket,char* Rsp,short RvcSize) //receive the data from the server
{
int shortRetval = -1;
struct timeval tv;
tv.tv_sec = 20; /* 20 Secs Timeout */
tv.tv_usec = 0;
if(setsockopt(hSocket, SOL_SOCKET, SO_RCVTIMEO,(char *)&tv,sizeof(tv)) < 0)
{
printf("Time Out\n");
return -1;
}
shortRetval = recv(hSocket, Rsp, RvcSize, 0);
printf("Response %s\n",Rsp);
return shortRetval;
}
int do_posting() //main driver program
{
hSocket = SocketCreate(); //Create socket
if(hSocket == -1)
{
printf("Could not create socket\n");
return 1;
}
printf("Socket is created\n");
if (SocketConnect(hSocket) < 0) //Connect to remote server
{
perror("connect failed.\n");
return 1;
}
printf("Sucessfully conected with server\n");
char *SendToServer = (char*) (rq.c_str());
std::cout << std:: endl << SendToServer << std::endl << std::endl;
SocketSend(hSocket, SendToServer, strlen(SendToServer)); //Send data to the server
read_size = SocketReceive(hSocket, server_reply, 200); //paluutiedot palvelimelta
printf("Server Response : %s\n\n",server_reply);
close(hSocket);
shutdown(hSocket,0);
shutdown(hSocket,1);
shutdown(hSocket,2);
return 0;
} // do_posting
}; // class Postman
int main()
{
Postman p;
int i = 0;
std::string s;
while(i < 11)
{
//p.combine_request("x=333333&y=444444");
//s = "x=xx" + std::to_string(i) + "&y=yy" + std::to_string(i + 1);
//s = "x=" + std::to_string(i) + "&y=" + std::to_string(i + 1);
s = "x=\"x x x" + std::to_string(i) + "\"" + "&y=" + std::to_string(i + 1);
p.combine_request(s.c_str());
p.do_posting( );
i += 2;
sleep(5);
std::cout << s;
}
return 0;
}