Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem usink accept() [winsocket]

problem usink accept() [winsocket]

Scheduled Pinned Locked Moved C / C++ / MFC
helpsysadmin
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eli15021979
    wrote on last edited by
    #1

    Hi, I'm trying to connect between few PC's using TCP/IP(multycast connection). this is my listening thread: [bind() and listen() is in another function]

    UINT ListeningThread(LPVOID lpvoid)
    {
    CMultycast_serverDlg *dlg = (CMultycast_serverDlg *)lpvoid;
    FD_SET SocketSet; // set of socket descriptors for select()
    int port; // looping veriable for ports
    struct sockaddr_in echoClntAddr;// client address
    SOCKET clntSock; // socket discriptor for client
    unsigned int clntLen; // length of client address data structure
    int SelectResult = 0;

    dlg->SelectFlag = false;
    dlg->ExitFlag = false;
    while(!dlg->ExitFlag)
    {
        FD\_ZERO(&SocketSet);
        for(port = 0 ; port < dlg->NumberOfPorts ; port++)
    FD\_SET((unsigned int)dlg->SocketsArray\[port\] , &SocketSet);
    SelectResult = select(dlg->MaxDescriptor + 1 , &SocketSet , NULL , 
                              NULL , &(dlg->selTimeout));
    if(SelectResult == 0)
      continue;//AfxMessageBox("Error :  No echo requests for the time 
                                   you specified....server still listenning");
    else if(SelectResult == SOCKET\_ERROR)
    {
        AfxMessageBox("Error :  select() has failed ");
        return 0 ;
    }
    else
    {
        //dlg->SelectFlag = true;
        for(port = 0 ; port < dlg->NumberOfPorts ; port++)
        {
            if(FD\_ISSET(dlg->SocketsArray\[port\] , &SocketSet))
    	{
    	    clntLen = sizeof(echoClntAddr);//set the size of the in-  out parameter
    	    //if(!dlg->SelectFlag)
    	    clntSock=accept(dlg->SocketsArray\[port\],(struct sockaddr\*)&echoClntAddr,(int \*)&clntLen);
    	    //dlg->SelectFlag = true;
    	    if(clntSock != INVALID\_SOCKET) //wait for a client to connect
    	    {
    	 	dlg->HandleClient(clntSock);//clntSock is connected to a client
     		dlg->m\_RecievedDataListBox.AddString(dlg->RecievedString);
    		//WSACleanup();
                  }
                  }  
            } 
    }
    

    }
    for(port = 0 ; port < dlg->NumberOfPorts ; port++)
    closesocket(dlg->SocketsArray[port]);
    return 1;
    }

    let me explain what i want to do: i want my PC to listen to several ports,each port is for another PC,and i want to be able to transfer some data between all the PC's and my PC,when all the PC'c try to connect my PC only once(and that's the problem,because accept() is blocking my application after one connection. the important thing is that i can't use broadcast(the other PC must connect as peer to pe

    T A 2 Replies Last reply
    0
    • E eli15021979

      Hi, I'm trying to connect between few PC's using TCP/IP(multycast connection). this is my listening thread: [bind() and listen() is in another function]

      UINT ListeningThread(LPVOID lpvoid)
      {
      CMultycast_serverDlg *dlg = (CMultycast_serverDlg *)lpvoid;
      FD_SET SocketSet; // set of socket descriptors for select()
      int port; // looping veriable for ports
      struct sockaddr_in echoClntAddr;// client address
      SOCKET clntSock; // socket discriptor for client
      unsigned int clntLen; // length of client address data structure
      int SelectResult = 0;

      dlg->SelectFlag = false;
      dlg->ExitFlag = false;
      while(!dlg->ExitFlag)
      {
          FD\_ZERO(&SocketSet);
          for(port = 0 ; port < dlg->NumberOfPorts ; port++)
      FD\_SET((unsigned int)dlg->SocketsArray\[port\] , &SocketSet);
      SelectResult = select(dlg->MaxDescriptor + 1 , &SocketSet , NULL , 
                                NULL , &(dlg->selTimeout));
      if(SelectResult == 0)
        continue;//AfxMessageBox("Error :  No echo requests for the time 
                                     you specified....server still listenning");
      else if(SelectResult == SOCKET\_ERROR)
      {
          AfxMessageBox("Error :  select() has failed ");
          return 0 ;
      }
      else
      {
          //dlg->SelectFlag = true;
          for(port = 0 ; port < dlg->NumberOfPorts ; port++)
          {
              if(FD\_ISSET(dlg->SocketsArray\[port\] , &SocketSet))
      	{
      	    clntLen = sizeof(echoClntAddr);//set the size of the in-  out parameter
      	    //if(!dlg->SelectFlag)
      	    clntSock=accept(dlg->SocketsArray\[port\],(struct sockaddr\*)&echoClntAddr,(int \*)&clntLen);
      	    //dlg->SelectFlag = true;
      	    if(clntSock != INVALID\_SOCKET) //wait for a client to connect
      	    {
      	 	dlg->HandleClient(clntSock);//clntSock is connected to a client
       		dlg->m\_RecievedDataListBox.AddString(dlg->RecievedString);
      		//WSACleanup();
                    }
                    }  
              } 
      }
      

      }
      for(port = 0 ; port < dlg->NumberOfPorts ; port++)
      closesocket(dlg->SocketsArray[port]);
      return 1;
      }

      let me explain what i want to do: i want my PC to listen to several ports,each port is for another PC,and i want to be able to transfer some data between all the PC's and my PC,when all the PC'c try to connect my PC only once(and that's the problem,because accept() is blocking my application after one connection. the important thing is that i can't use broadcast(the other PC must connect as peer to pe

      T Offline
      T Offline
      ten90425
      wrote on last edited by
      #2

      You have two options : 1. separate accept() in different thread ( one thread per a listening socket ) which, when I think about it now, means all the stuff - socket, listen, bind accept etc. goes in the thread - the so called "ServerThread" 2. make your socket descriptors non blocking in windows I think this is done by using ioctlsocket( FIONBIO .... ) . I've never tryed it in windows, in unix select returns the listening nonblocking socket that accepted a connection, so I assume it is the same in windows.

      1 Reply Last reply
      0
      • E eli15021979

        Hi, I'm trying to connect between few PC's using TCP/IP(multycast connection). this is my listening thread: [bind() and listen() is in another function]

        UINT ListeningThread(LPVOID lpvoid)
        {
        CMultycast_serverDlg *dlg = (CMultycast_serverDlg *)lpvoid;
        FD_SET SocketSet; // set of socket descriptors for select()
        int port; // looping veriable for ports
        struct sockaddr_in echoClntAddr;// client address
        SOCKET clntSock; // socket discriptor for client
        unsigned int clntLen; // length of client address data structure
        int SelectResult = 0;

        dlg->SelectFlag = false;
        dlg->ExitFlag = false;
        while(!dlg->ExitFlag)
        {
            FD\_ZERO(&SocketSet);
            for(port = 0 ; port < dlg->NumberOfPorts ; port++)
        FD\_SET((unsigned int)dlg->SocketsArray\[port\] , &SocketSet);
        SelectResult = select(dlg->MaxDescriptor + 1 , &SocketSet , NULL , 
                                  NULL , &(dlg->selTimeout));
        if(SelectResult == 0)
          continue;//AfxMessageBox("Error :  No echo requests for the time 
                                       you specified....server still listenning");
        else if(SelectResult == SOCKET\_ERROR)
        {
            AfxMessageBox("Error :  select() has failed ");
            return 0 ;
        }
        else
        {
            //dlg->SelectFlag = true;
            for(port = 0 ; port < dlg->NumberOfPorts ; port++)
            {
                if(FD\_ISSET(dlg->SocketsArray\[port\] , &SocketSet))
        	{
        	    clntLen = sizeof(echoClntAddr);//set the size of the in-  out parameter
        	    //if(!dlg->SelectFlag)
        	    clntSock=accept(dlg->SocketsArray\[port\],(struct sockaddr\*)&echoClntAddr,(int \*)&clntLen);
        	    //dlg->SelectFlag = true;
        	    if(clntSock != INVALID\_SOCKET) //wait for a client to connect
        	    {
        	 	dlg->HandleClient(clntSock);//clntSock is connected to a client
         		dlg->m\_RecievedDataListBox.AddString(dlg->RecievedString);
        		//WSACleanup();
                      }
                      }  
                } 
        }
        

        }
        for(port = 0 ; port < dlg->NumberOfPorts ; port++)
        closesocket(dlg->SocketsArray[port]);
        return 1;
        }

        let me explain what i want to do: i want my PC to listen to several ports,each port is for another PC,and i want to be able to transfer some data between all the PC's and my PC,when all the PC'c try to connect my PC only once(and that's the problem,because accept() is blocking my application after one connection. the important thing is that i can't use broadcast(the other PC must connect as peer to pe

        A Offline
        A Offline
        Alexander M
        wrote on last edited by
        #3

        select() is normally used for non-blocking socket. try to set the listen socket non-blocking and your problem should be solved (btw. connect() with a non-blocking socket seems not to be possible!). Don't try it, just do it! ;-)

        E 1 Reply Last reply
        0
        • A Alexander M

          select() is normally used for non-blocking socket. try to set the listen socket non-blocking and your problem should be solved (btw. connect() with a non-blocking socket seems not to be possible!). Don't try it, just do it! ;-)

          E Offline
          E Offline
          eli15021979
          wrote on last edited by
          #4

          Hi, First,thanks for your help. Do you have some tutorial that can help me solve this problem? With best regards, Eli

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups