#ERROR reading response from socket
15 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define HOST "mywebsite.com"
#define PORT 443
void error(const char *msg) {
perror(msg);
exit(EXIT_FAILURE);
}
int main(void) {
struct hostent *server;
struct sockaddr_in serv_addr;
int sockfd, bytes, sent, received, total;
char response[4096];
char *message = "POST mywebsite.com HTTP/1.1\r\n"
"Host: " HOST "\r\n"
"Content-type: application/json; charset=utf-8\r\n"
"Cookie: somecookie\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0\r\n"
"Accept: application/json\r\n"
"Accept-Language: en-US,en;q=0.5\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"Referer: mywebsite.com/\r\n"
"Origin: mywebsite.com\r\n"
"Sec-Fetch-Dest: empty\r\n"
"Sec-Fetch-Mode: cors\r\n"
"Sec-Fetch-Site: same-origin\r\n"
"Te: trailers\r\n"
"Connection: close\r\n"
"\r\n"
"{\r\n"
"t0ken: \"sometoken\"\r\n"
"username: \"helloworld\"\r\n"
"}\r\n"
"\r\n";
printf("Request:\n%s\n", message);
// Create the socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket");
// Lookup the ip address
server = gethostbyname(HOST);
if (server == NULL) error("No Such Host");
// Fill in the structure
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
// Connect the socket
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) error("ERROR Connecting");
// Send The Request
total = strlen(message);
sent = 0;
do {
bytes = write(sockfd, message+sent, total-sent);
if (bytes < 0) error("ERROR writing message to socket");
if (bytes == 0) break;
sent += bytes;
} while (sent < total);
// Receive the response
memset(response, 0, sizeof(response));
total = sizeof(response)-1;
received = 0;
do {
bytes = read(sockfd, response+received, total-received);
if (bytes < 0) error("ERROR reading response from socket");
if (bytes == 0) break;
received += bytes;
} while (received < total);
if (received == total) error("ERROR storing complete response from socket");
// Close socket
close(sockfd);
// process response
printf("Response: \n%s\n", response);
return EXIT_SUCCESS;
}
ERROR reading response from socket: Connection reset by peer
This error occurs if you try to read from a connection which was closed by the server. I think the problem is that the server closes the connection after sending the response and you try to read from it, which results in an error since it's closed. So if you just break the loop instead of exitting you should be fine.
if I do this i get no response
I mean it does not work
ah, wait if you try to connect over https (port 443) this won't work. You first need to execute a TLS handshake etc.
Wow
maybe i do it the wrong way
maybe using socket is too hard
maybe i should do this ?
I'd say, yes