473,570 Members | 2,927 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,ima ge/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=somet hing&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(MAK EWORD(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_AN Y);
server_addr.sin _port = htons(80);

struct sockaddr_in client_addr;
unsigned int client_addr_len gth = sizeof(client_a ddr);

int on = 1;
setsockopt(list en_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on,
sizeof(on));
if ((bind(listen_f d, (struct sockaddr*)&serv er_addr,
sizeof(server_a ddr))) < 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_f d, (struct sockaddr *)&client_addr ,
&client_addr_le ngth);

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><in put type=text
name=something value=something ><input type=submit value=Submit></
form>";
#ifdef MINGW32
send(socket_fd, content, strlen(content) , 0);
closesocket(soc ket_fd);
#else
write(socket_fd , content, strlen(content) );
close(socket_fd );
#endif

}

#ifdef MINGW32
closesocket(lis ten_fd);
WSACleanup();
#endif
return 0;
}
Dec 6 '07 #1
3 25541
In article <9f************ *************** *******@s12g200 0prg.googlegrou ps.com>,
Khookie <ch********@gma il.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.program mer.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.i nvalid wrote:
In article <9f1d027e-c289-4de1-941f-21df063ff...@s1 2g2000prg.googl egroups.com>,

Khookie <chris.k...@gma il.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.program mer.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.pro gramming - Chris
Dec 6 '07 #3
On Dec 6, 10:52 am, Khookie <chris.k...@gma il.comwrote:
On Dec 6, 8:33 pm, dj3va...@csclub .uwaterloo.ca.i nvalid wrote:


In article <9f1d027e-c289-4de1-941f-21df063ff...@s1 2g2000prg.googl egroups.com>,
Khookie <chris.k...@gma il.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.program mer.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.pro gramming - 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1392
by: Rob | last post by:
Hello, I've got a huge problem with async sockets. I've created a networked virtual environment, using async socket programming. The problem I face occurs when I close the listener socket. It listens using socket.BeginAccept(...). When a client disconnects, the socket is disconnected like this: try {...
2
13722
by: Jean-Philippe Guyon | last post by:
Hello, I am trying to compile a class that uses socket using the Visual C++ ..NET compiler. I get the following error: ------ Build started: Project: infCommon, Configuration: Release Win32 ------ Compiling... cl : Command line warning D4029 : optimization is not available in the
4
4825
by: Ted | last post by:
Hi all, I am trying to learn C socket programming and I have a small program which is a UP client. The problem is, when I run the program, I get a runtime error - "Invalid Argument" - from a call to sendto. I was hoping that if someone has the time they could take a look at my code posted below and let me know what i'm doing wrong?
2
6965
by: chellappa | last post by:
Hi Every body!, i did a small Chat program in Linux C Socket Programm ... I am using stdout/stdin i did in single machinee i will work properly..but i want to run in differnet machine..i tried ,but its faliure... try to modify this pgm run in differnt machine Server Program ============== #include <stdio.h> #include <sys/socket.h>
3
881
by: zbcong | last post by:
hello i found there are three socket programming architectures from dotnet:synchronized socket,asynchronized socket and TcpListener. i know that the asynchronized socket is non-blocking socket.i can use it implementing non-blocking socket communication. but i don't know how to determine when i use the synchronized socket or TcpListener. ...
5
3674
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose but have been unsuccessful in finding the answers so far. Either because my understanding of sockets isn't where it needs to be or my questions are...
1
3380
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows XP. About the architecture: I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This...
4
3178
by: Marco Meoni | last post by:
Hi. I read the Gordon McMillan's "Socket Programming Howto". I tried to use the example in this howto but this doesn't work. The code is class mysocket: '''classe solamente dimostrativa - codificata per chiarezza, non per efficenza''' def __init__(self, sock=None): if sock is None: self.sock = socket.socket( socket.AF_INET,...
8
4667
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am new to .net technologies. ASP.NET supports socket programming like send/receive in c or c++? I am developing web-site application in asp.net and code behind is Visual C#. In page_load event, I am using atl com component. Here one for loop is there. In this for loop, number of iterations are 1000, I can receive some data...
0
7637
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7950
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6332
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5523
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5247
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3684
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3671
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2133
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.