mingw socket programming

K

Khookie

Hi everyone

I've been coding a web server recently, and wanted to figure out how
to do it with mingw (code is below).

It works fine when you first load a page (GET request), but when you
submit (POST), it usually comes back with a "connection has been
reset" error. Sometimes it does work though.

Furthermore - when I inspect the request string, it's always like:

--START--
POST / HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:
1.8.1.11) Gecko/20071127 Firefox/2.0.0.11
Accept: text/xml,application/xml,application/xhtml+xml,text/
html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-au
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost/
Content-Type: application/x-www-form-urlencoded
Content-Length: 19

--END--

I can't figure out how to get the POST string (i.e.
something=something&something2=something2, etc.)...

Any winsock insight would be appreciated.

Code below:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef MINGW32
#include <winsock2.h>
#else
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <signal.h>
#include <sys/socket.h>
#include <unistd.h>
#endif

int main() {
#ifdef MINGW32
WSADATA wsadata;
if (WSAStartup(MAKEWORD(1,1), &wsadata) == SOCKET_ERROR) {
printf("Error creating socket.");
return -1;
}
#endif

int listen_fd;
if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
puts("Error opening socket.");
return -1;
}

struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(80);

struct sockaddr_in client_addr;
unsigned int client_addr_length = sizeof(client_addr);

int on = 1;
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on,
sizeof(on));
if ((bind(listen_fd, (struct sockaddr*)&server_addr,
sizeof(server_addr))) < 0) {
puts("Error binding socket.");
return -1;
}

if ((listen(listen_fd, 64)) < 0) {
puts("Error listening on socket.");
return -1;
}

while (1) {
int socket_fd = accept(listen_fd, (struct sockaddr *)&client_addr,
&client_addr_length);

char request[32768];
#ifdef MINGW32
recv(socket_fd, request, 32768, 0);
#else
read(socket_fd, request, 32768);
#endif

puts(request);

char* content = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nA
web server doing POST<br/><form method=post><input type=text
name=something value=something><input type=submit value=Submit></
form>";
#ifdef MINGW32
send(socket_fd, content, strlen(content), 0);
closesocket(socket_fd);
#else
write(socket_fd, content, strlen(content));
close(socket_fd);
#endif

}

#ifdef MINGW32
closesocket(listen_fd);
WSACleanup();
#endif
return 0;
}
 
D

dj3vande

Hi everyone

I've been coding a web server recently, and wanted to figure out how
to do it with mingw (code is below).

None of this is defined by the C programming language, which puts it
beyond the scope of comp.lang.c.

Any winsock insight would be appreciated.

comp.os.ms-windows.programmer.win32 would be a better place to ask
about that.

(Though from what you've said it seems to me that it may be a HTTP
protocol problem and not a winsock problem.)


dave
 
K

Khookie

None of this is defined by the C programming language, which puts it
beyond the scope of comp.lang.c.


comp.os.ms-windows.programmer.win32 would be a better place to ask
about that.

(Though from what you've said it seems to me that it may be a HTTP
protocol problem and not a winsock problem.)

dave

hey everyone, apologies for posting to the wrong group - will go to
alt.winsock.programming - Chris
 
D

diksa2005

hey everyone, apologies for posting to the wrong group - will go to
alt.winsock.programming - Chris- Hide quoted text -

- Show quoted text -

Hi
I love the way you are trying in your programming work and i really
want you to use this opportunity to introduce yourself to an online
company where you can get instant assistance in all ways with this
extend you have gone. There you meet alots of programmers in all
fields of knowlegde applying for jobs by signing up free of charge,i
expected you to join them because i saw so many you are far better
than collecting contract project on daily basis. Here's the site,
http://www.getafreelancer.com/affil...s/PHP-ASP/selling-using-text-message-sms.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top