Solved

How to do reconnect socket when port closed or idled?

Posted on 2007-03-30
6
1,207 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
ID: 18822475
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
ID: 18822484
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 125 total points
ID: 18836625
>>>> 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
ID: 21466803
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

932 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

17 Experts available now in Live!

Get 1:1 Help Now