Solved

How to do reconnect socket when port closed or idled?

Posted on 2007-03-30
6
1,211 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
[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
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

734 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