Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > mingw socket programming

Reply
Thread Tools

mingw socket programming

 
 
Khookie
Guest
Posts: n/a
 
      12-06-2007
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, 3276;
#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;
}
 
Reply With Quote
 
 
 
 
dj3vande@csclub.uwaterloo.ca.invalid
Guest
Posts: n/a
 
      12-06-2007
In article <(E-Mail Removed)>,
Khookie <(E-Mail Removed)> wrote:
>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

 
Reply With Quote
 
 
 
 
Khookie
Guest
Posts: n/a
 
      12-06-2007
On Dec 6, 8:33 pm, (E-Mail Removed) wrote:
> In article <(E-Mail Removed)>,
>
> Khookie <(E-Mail Removed)> wrote:
> >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
 
Reply With Quote
 
diksa2005@fastermail.com
Guest
Posts: n/a
 
      12-07-2007
On Dec 6, 10:52 am, Khookie <(E-Mail Removed)> wrote:
> On Dec 6, 8:33 pm, (E-Mail Removed) wrote:
>
>
>
>
>
> > In article <(E-Mail Removed)>,

>
> > Khookie <(E-Mail Removed)> wrote:
> > >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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows GCC Support (Mingw & Mingw-w64) xeno fears Python 1 10-04-2009 06:40 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM
[g++ mingw eclipse] How do I setup Eclipse to use MinGw's g++ compiler? Sam Smith C++ 0 10-18-2004 11:36 AM



Advertisments