Solved

How to do reconnect socket when port closed or idled?

Posted on 2007-03-30
6
1,206 Views
Last Modified: 2008-05-10
Hi Experts,
I am using winsock to work with Socket for MFC Window application.
I used port 8000 to connect to socket server,

Can you give some suggestion about how to do re-connect to server when server port 8000 is closed or idled for some reason?

Many thanks!

0
Comment
Question by:CongUan
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
DeepuAbrahamK earned 125 total points
Comment Utility
You could use closesocket and try reconnecting.Also make sure that you have set tcp linger option (using setoption()) off while creating the socket.
Best Regards,
DeepuAbrahamK
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 125 total points
Comment Utility
>>>> how to do re-connect to server when server port 8000 is closed or idled
I never heard of a "idled" port.

From my experience you must do nothing special. A  port or network device deactivated by an administrative action should be available again after it activated again. That may last a little time as TCP/IP is an asynchronously running subssystem.

The main problem maybe is that your prog needs to be able to handle such cases, i. e. instead of leaving a read or write operation in an undefined state it must identify the error returned a previous action as an error which might be temporary only and do periodically recalls, e. g. like that:

    int count = 0;
    while (true)
    {
           if (!readFromSocket())
           {
                 if (lastError == FATAL || ++count > MAX_RETRY)
                       exit(1);         // leave prog in case of a fatal error
                Sleep(1000);      // sleep a second
           }
    }

The lastError and readFromSocket used above are assumed to be members of an appropriate class.

Regards, Alex
0
 
LVL 6

Expert Comment

by:SeanDurkin
Comment Utility
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup Zone:
      Split: DeepuAbrahamK {http:#18822475} & itsmeandnobodyelse {http:#18836625}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Sean
EE Cleanup Volunteer
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

772 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

14 Experts available now in Live!

Get 1:1 Help Now