Fd_set

This is a discussion on Fd_set within the C# Programming forums, part of the General Programming Boards category; I need FD_SET from C++ in C# can somebody help me?...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    14

    Fd_set

    I need FD_SET from C++ in C# can somebody help me?

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    There's all sorts of Winsock API calls in this article: [Advanced] Winsock in C# including the fd_set struct.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    14
    no like this FD_SET(socket, &readfds); and FD_CLR(socket, &errorfds);

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by deathkosui View Post
    no like this FD_SET(socket, &readfds); and FD_CLR(socket, &errorfds);
    You are thinking the wrong way. C# abstracts sockets at a higher level. You do not use FD_SET's
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    14
    this is the full code im trying to covnert from C++

    this is cpp file
    Code:
    /*
    Copyright (C) 2009 Xanadu Development Team
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; version 2
    of the License.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    */
    
    #include "Selector.h"
    #include "PacketHandler.h"
    
    void _selectorThread (Selector* selectorObject) {
    	selectorObject->selectThread();
    }
    
    Selector::Selector () {
    	terminate = false;
    	timeout.tv_sec = 1;
    	timeout.tv_usec = 0;
    	FD_ZERO(&readfds);
    	FD_ZERO(&writefds);
    	FD_ZERO(&errorfds);
    
    	HANDLE thread = CreateThread(NULL, 20000,
    		(LPTHREAD_START_ROUTINE)_selectorThread,
    		(LPVOID)this,
    		NULL,
    		NULL);
    }
    
    Selector::~Selector() {
    	terminate = true;
    }
    
    void Selector::registerSocket (int socket, 
    		bool selectRead, bool selectWrite, bool selectError, 
    		SelectHandler* handler) {
    	if (selectRead) {
    		FD_SET(socket, &readfds);
    	}
    	if (selectWrite) {
    		FD_SET(socket, &writefds);
    	}
    	if (selectError) {
    		FD_SET(socket, &errorfds);
    	}
    	handlers[socket] = handler;
    }
    
    void Selector::unregisterSocket (int socket) {
    	FD_CLR(socket, &readfds);
    	FD_CLR(socket, &writefds);
    	FD_CLR(socket, &errorfds);
    	if(handlers.find(socket) != handlers.end())
    		handlers.erase(socket);
    }
    
    void Selector::selectThread () {
    	fd_set t_readfds;
        fd_set t_writefds;
    	fd_set t_errorfds;
    	while (!terminate) {
    		try {
    			t_readfds = readfds;
    			t_writefds = writefds;
    			t_errorfds = errorfds;
    			int result = select(0, &t_readfds, &t_writefds, &t_errorfds, &timeout);
    			if (result == 0) continue;
    			unsigned int count = handlers.size();
    			for (hash_map<int,SelectHandler*>::iterator iter = handlers.begin();
    				 iter != handlers.end(); iter++){
    				int socket = iter->first;
    				SelectHandler* handler = iter->second;
    				if (FD_ISSET(socket, &t_errorfds)) {
    					handler->handle(this, socket);
    				}
    				if (FD_ISSET(socket, &t_readfds)) {
    					handler->handle(this, socket);
    				}
    				if (FD_ISSET(socket, &t_writefds)) {
    					handler->handle(this, socket);
    				}
    				if(handler->isEnd()){
    					unregisterSocket(socket);
    					closesocket(socket);
    					delete (PacketHandler*)handler;
    					break;
    				}
    			}
    		}
    		catch (...) {
    			// TODO error
    		}
    	}
    }
    .h file

    Code:
    /*
    Copyright (C) 2009 Xanadu Development Team
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; version 2
    of the License.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    */
    
    #ifndef SELECTOR_H
    #define SELECTOR_H
    
    #include <Winsock2.h>
    #include <hash_map>
    using namespace stdext;
    
    class Selector {
    public:
    	class SelectHandler {
    	protected:
    		bool bend;
    	public:
    		SelectHandler(){
    			bend = false;
    		}
    		virtual int handle (Selector* selector, int socket) = 0;
    		bool isEnd(){
    			return bend;
    		}
    		void end(){
    			bend = true;
    		}
    	};
    
    	Selector ();
    	~Selector();
    
    	void registerSocket 
    		(int socket, 
    		bool selectRead, bool selectWrite, bool selectError, 
    		SelectHandler* handler);
    
    	void unregisterSocket (int socket);
    	void selectThread();
    
    private:
    	bool terminate;
    	fd_set readfds;
        fd_set writefds;
    	fd_set errorfds;
    	struct timeval timeout;
    	hash_map<int, SelectHandler*> handlers;
    };
    
    #endif
    if anybody can help me covnert the FD_SET() and FD_CLR things in the code then thank you very mcuh

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Asynchronous dispatching of multiple sockets is handled completely different in C#. You should not be attempting to translate this code from C++ to C# -- you need to do it the C# way.

    Links to C# socket resources have already been posted.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    14
    um what? Asynchronous dispatching of multiple sockets? but is there equivilents to FD_CLR, FD_ISSET, FD_SET and others?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21