468,497 Members | 2,016 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,497 developers. It's quick & easy.

mingw socket programming

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;
}
Dec 6 '07 #1
3 23209
In article <9f**********************************@s12g2000prg. googlegroups.com>,
Khookie <ch********@gmail.comwrote:
>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

Dec 6 '07 #2
On Dec 6, 8:33 pm, dj3va...@csclub.uwaterloo.ca.invalid wrote:
In article <9f1d027e-c289-4de1-941f-21df063ff...@s12g2000prg.googlegroups.com>,

Khookie <chris.k...@gmail.comwrote:
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
hey everyone, apologies for posting to the wrong group - will go to
alt.winsock.programming - Chris
Dec 6 '07 #3
On Dec 6, 10:52 am, Khookie <chris.k...@gmail.comwrote:
On Dec 6, 8:33 pm, dj3va...@csclub.uwaterloo.ca.invalid wrote:


In article <9f1d027e-c289-4de1-941f-21df063ff...@s12g2000prg.googlegroups.com>,
Khookie <chris.k...@gmail.comwrote:
>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

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/affili...ssage-sms.html

Dec 7 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Rob | last post: by
2 posts views Thread by Jean-Philippe Guyon | last post: by
4 posts views Thread by Ted | last post: by
2 posts views Thread by chellappa | last post: by
3 posts views Thread by zbcong | last post: by
5 posts views Thread by John Sheppard | last post: by
1 post views Thread by John Sheppard | last post: by
4 posts views Thread by Marco Meoni | last post: by
8 posts views Thread by =?Utf-8?B?Sm9obg==?= | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.