anukavi 0 Light Poster
Hi,

Before posting this question, i had gone thru related answers in many sites but cudnt get a solution to the problem.
I have followed the step needed for Socket timeout, but the timeout eventually pass for the first time but fails for second time.

Platform : VC++ 2012 MFC Server Socket prog

I have 3 buttons which reads as USER_LIST_DETAILS , START and CANCEL
When the user clicks the USER_LIST_DETAILS button, I Open the Socket

void CAboutDlg::OnBnClickedUserList()
{
  if (ssocket == INVALID_SOCKET)
        Open_Socket();

idc_textbox.put_Text("PRESS START");
}

void CAboutDlg::OpenSocket()
{
   ss = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);//SOCKET ss is global variable
   //binding and listen are done here.
}

Then the user has to choose START, for the functions to take place.
void CAboutDlg::OnBnClickedStart()
{
  struct timeval timer;
  struct fd_set master_set;
  int val;

  sin.sin_family = AF_INET;
  sin.sin_port = htons((u_short)myPort);
  sin.sin_addr.s_addr = INADDR_ANY;

  FD_ZERO(&master_set);
  FD_SET(ss,&master_set);

  timer.tv_sec  = 60;//60 second wait time
  timer.tv_usec = 0;

  idc_textbox_put_Text("Waiting...");UpdateWindow();

  val = select(ss+1,&master_set,NULL,NULL,&timer);

  if(val == -1)
   MessageBox("Error");

  if(val == 0)
   MessageBox("Timeout..Press CANCEL for Home Screen");//The user will choose CANCEL button

  if(val > 0)
  {
    //non block mode is set here    
    SOCKET cs = accept(ss,NULL,NULL);
    //read the user file and display them
  }

  closesocket(ss);
  closesocket(ss);
}

void CAboutDlg::OnBnClickedCancel()
{
  //here i show the 3 buttons
}

**Problem :**
When i choose USER_LIST Button, and on START, the system waits for 60 seconds and displays the message "Timeout.."
Then i click the CANCEL button and chooses USER_LIST Button again and then press ENTER
now the val = select(....) always gives me -1. WHY??

I had reset the fdset values. ie whenever the START button is called, FD_ZETO,FD_SET is initialised.
Where is the coding error?
Pls help me to sort it out.
Thanks