Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Closing a socket gracefully

Posted on 1999-07-20
7
Medium Priority
?
757 Views
Last Modified: 2013-12-03
hello,

i have made a small server that immitates a http server, but only returns services that are supported in the server.
It could be done by individual CGI scripts, but for performance reason it is done like this.

Everything goes just fine, i get a request, proces it, and send a header back, followed by a lot of data (this being text/html)

After this i close the socket. At least, that's what it is supposed to do.. It doesnt do it gracefully, and the browser thinks the connection is reset by peer (while it should be a gracefull close, indicating end of document)

below is the code i use to close the socket:
(sock is a previously initialized socket)

LINGER l={1,200};
int ls=sizeof(l);

setsockopt(sock,SOL_SOCKET,SO_LINGER,(char *)&l,ls);
closesocket(sock);

well, what this does is close the socket alright.. cutting off all data that still needs to be send, and in general not being nice. Even though i think i called setsockopt ok..

Now, the one that see's the error and gives some code on how to correct it (probably only 4+ lines.. if it works i'm happy), and also explains what i did wrong, he/she will get the points :)

As you can see, i do NOT use MFC or OWL.. all plain API calls.. so please no fancy CSocket examples :-)

Thanks,

 Floris
0
Comment
Question by:TheMadManiac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 1

Author Comment

by:TheMadManiac
ID: 1403375
Oh i forgot to say that with a small return page (about 800 bytes) everything goes correctly. With a larger one (about 20k) it gives 'connection reset by peer'. This should indicate it does not linger very well :-)

Floris
0
 
LVL 2

Expert Comment

by:lucidity
ID: 1403376
I have some code on my site that is a generic server, it works fine for html files, digitalwizard.webjump.com, look for generic server.. Its all basic winsock API calls and should (might) give you an idea of what you are doing wrong.
0
 
LVL 2

Expert Comment

by:lucidity
ID: 1403377
once you get it look at the listen thread, thats where the program begins to receive a request and sends a reply.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 1

Author Comment

by:TheMadManiac
ID: 1403378
Well i looked at it, puzzled some with my program.. and found out there is nothing wrong!

I made a small client that requests a big page from the server (that the browsers IE 4 and NS 4 fart on), and this goes just fine.. argh! even recv() returns 0 indicating a gracefull close, and the page is indeed completely transferred. Hm, perhaps this is something for the CGI topic...
0
 
LVL 1

Author Comment

by:TheMadManiac
ID: 1403379
Doh all i missed was i didn't get 2 more bytes from the socket, busting the http protocol.

your GServer still gave me an 'error' on listen() though.

Could you answer, so i can grade? i have points enough and it did help just not on this question :-)

Floris
0
 
LVL 2

Accepted Solution

by:
lucidity earned 600 total points
ID: 1403380
cool.. I have not tried the G server with winsock 2 so that may be the problem there
0
 
LVL 1

Author Comment

by:TheMadManiac
ID: 1403381
i ment to say the GServer showed me an error in my code :-)

i called listen every time before an accept, which is a bit useless :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question