Socket C ansi header Post

Asked

Viewed 100 times

0

Hello I have a socket in C ansi without using adcional libraries working normally with http requests. When ordering on a new server do not get any response, using the Curl lib, results 100-continue and does the process normally.

Like, through the socket without Curl I can receive the data with a 100-continue Response?

Thank you

  • It is unclear what you have and exactly where you are at. Is the problem reading socket data? Interpreting http request? In sending a valid reply?

  • I apologize, when I asked the question, there was no attempt that the server only accepts http connections on port 443 with Ssl, connection that Curl does automatically, for this reason that on Curl always returns and socket no. I am implementing the connection via SSL in the socket in question. Thank you

1 answer

0

Assuming you want to access a local server, Apache is active and a connection is made through Socket, after connecting to the server, it is necessary to pass a Header(Header).

main:

const int PORT = 8080;
int _sock;
int res;
char buffer[] = "GET / HTTP/1.1"
"Host: localhost:8080"
"User-Agent: WebBrowser (<GUI>; <OS>; rv:<version>)"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9"
"Accept-Language: pt-BR"
"Connection: keep-alive";

struct sockaddr_in serv;
struct hostent *server;

_sock = socket(AF_INET, SOCK_STREAM, TCP);
server = gethostbyname("127.0.0.1");
serv.sin_family = AF_INET;
serv.sin_port = htons(PORT);
bcopy((char *) server->h_addr,
                        (char *) &serv.sin_addr.s_addr,
                        server->h_length);
int len = sizeof(serv);
res = connect(_sock, (struct sockaddr *) &serv, len);
write(_sock,buffer, strlen(buffer))

while(1)
    if(read(_sock, &c, 1) != 1)
        break;
    else
        write(stdout, &c, 1);

close(_sock);

The Webservers take the information of the user q accesses the server through this header. So there is no need to use the curl to apply to the server.

  • Thank you for your reply. When I asked the question, there was no attempt that the server only accepts http connections on port 443 with Ssl, connection that Curl does automatically, for this reason that on Curl always returns and the socket does not. I am implementing the connection via SSL in the socket in question. Thank you

  • @Alessandro, if the answer solved your problem, you can click on V next to the answer to mark your question as answered. That’s if the answer resolves. And welcome to Stackoverflow (a little late).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.