Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

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

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
toannds
Asked:
toannds
  • 4
  • 4
1 Solution
 
rdelfinoCommented:

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
 
jhanceCommented:
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
 
toanndsAuthor Commented:
Thanks, I'll try...
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
jhanceCommented:
It's also too bad you accepted an incorrect answer...
0
 
toanndsAuthor Commented:
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
 
jhanceCommented:
Alas, it's too bad you accepted an incorrect answer...
0
 
toanndsAuthor Commented:
I don't understand what you said. My program used WSAAsyncSelect() and worked well. I wanna give you more points :-)
0
 
jhanceCommented:
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
 
toanndsAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now