Solved

Winsock : How do I know when other host closes connection with me?

Posted on 1998-12-18
9
172 Views
Last Modified: 2010-04-15
Hello,

I'm beginner to Winsock programming. I wrote a "chat" program. It works but I want to know whenever the connection is closed by other host. For now, I check this by trying sending something to other host. If I can send without any error then it still alives. That's not a good and convenient methode.

Can anyone show me better methode to verify a connection without sending?
For instance something like getting the state of a connection.

Thank you.
0
Comment
Question by:toannds
  • 4
  • 4
9 Comments
 
LVL 2

Accepted Solution

by:
rdelfino earned 50 total points
ID: 1255285

There's another way. You try recv().

If the connection is no longer alive you will get 0 bytes received
or an error.

I hope it helps

Reginaldo

0
 
LVL 32

Expert Comment

by:jhance
ID: 1255286
rdelfino,

Wrong answer.  Just because recv() returns 0 bytes, doesn't mean the connection has closed.  There just may be 0 bytes available at that time.

Under Windows, the right way to do it is to use WSAAsyncSelct() and specify the FD_CLOSE event.  If WSAGETSELECTERROR returns 0 for the FD_CLOSE event, then the socket was closed gracefully.  If an error value of WSAECONNRESET is returned, the socket was aborted.
0
 

Author Comment

by:toannds
ID: 1255287
Thanks, I'll try...
0
 
LVL 32

Expert Comment

by:jhance
ID: 1255288
It's also too bad you accepted an incorrect answer...
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:toannds
ID: 1255289
Thanks jhance!

Now I have completed my program with new concept: event notifications. It works perfectly. When other host close connection, I receive immediately FD_CLOSE.
0
 
LVL 32

Expert Comment

by:jhance
ID: 1255290
Alas, it's too bad you accepted an incorrect answer...
0
 

Author Comment

by:toannds
ID: 1255291
I don't understand what you said. My program used WSAAsyncSelect() and worked well. I wanna give you more points :-)
0
 
LVL 32

Expert Comment

by:jhance
ID: 1255292
You didn't give ME any points.  You accepted rdelfino's incorrect answer rather than rejecting it.

So he gets the points and I don't.
0
 

Author Comment

by:toannds
ID: 2026483
I'm so sorry jhance.

For a long time I could not access to this site due to the stupid firewalls of my country. I was a beginner to this forum at that time so I didn't know clearly how to give you points. And by accident we meet again at my question

http://www.experts-exchange.com/jsp/qShow.jsp?ta=mfc&qid=10202631

Now I know my mistake so I wanna give you more points in that question.

Please forgive me.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
why debugging a macro i s difficult 10 26
Microsoft vcsetup.exe and vcssetup.exe 8 397
Adjust Mfcapp 29 155
C hashtable library 3 71
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now