GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 13-Feb-2006, 08:54
Radi0ShacK Radi0ShacK is offline
New Member
 
Join Date: Feb 2006
Posts: 13
Radi0ShacK is on a distinguished road

Socket Programming questions


I am developing a socket application using C++ "winsock API" obviously on Microsoft Windows platform, this application should listen on two different ports e.g. 666 and 999, and it will behave differently according to which connection it receives from which port.

So how can I do this?, should I divide my application into two different applications or is there a more cleaner/cooler/neat solution other than this?

Thanks alot in advance
  #2  
Old 13-Feb-2006, 09:41
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: socket programming questions?


there are two approaches to this: you can use threads, or you can use non-blocking sockets.
for threads, you would create two threads that would each listen on one of those ports, with their own sockets. the problem would be how to terminate the other thread when you got a connection.
For non-blocking sockets, you create a socket for each port, but you call a special function, ioctlsocket (with the value FIONBIO), to make it non-blocking. Now, instead of calling accept() and having your program hang, you call bind() and listen() on both sockets as normal, but then you call select() with the two sockets in the readfds set. That way, select() will tell you when either socket is ready to be accept()ed.
  #3  
Old 13-Feb-2006, 12:10
davis
 
Posts: n/a

Re: socket programming questions?


Unless you're dying to use WINSOCK, take a look at nsoftware.com's IP*Works! product(s). They make all sorts of socket programming and related protocols a breeze.


:davis:
  #4  
Old 14-Feb-2006, 01:39
Radi0ShacK Radi0ShacK is offline
New Member
 
Join Date: Feb 2006
Posts: 13
Radi0ShacK is on a distinguished road

Re: socket programming questions?


thanks Davis but its commercial and thanks alot ubergeek.
  #5  
Old 14-Feb-2006, 11:02
davis
 
Posts: n/a

Re: socket programming questions?


Quote:
Originally Posted by Radi0ShacK
thanks Davis but its commercial

...all the more reason to use well-proven/tested commercial third party royalty-free libraries. The length of time you spent debugging your WINSOCK code will pay for the library. In addition, writing your "daemon" class for SMTP is a total breeze.

See the documentation on IPDaemon and IPPort. They are incredibly easy to use.

CPP / C++ / C Code:
#include <iostream>
using namespace std;

#include <ipworks.h>

class Daemon : public IPDaemon
{
public:

    INT FireConnected(USHORT &usConnectionId, USHORT &usStatusCode, LPSTR &lpszDescription)
    {
        SetEOL( usConnectionId, "\n", 1 );
        cout << "Connected to " << GetRemoteHost( usConnectionId ) << endl;
        char *p_szResponse = "Connected to Daemon\r\n";
        SetDataToSend( usConnectionId, p_szResponse, strlen( p_szResponse ) );
        return 0;
    }

    INT FireConnectionRequest(BOOL &bAccept)
    {
        return 0;
    }

    INT FireDataIn(USHORT &usConnectionId, LPSTR &lpText, USHORT FAR &lenText, BOOL &bEOL) 
    {
        cout << "Data From " << GetRemoteHost( usConnectionId ) << " :" << lpText << endl;
        return 0;
    }

    INT FireDisconnected(USHORT &usConnectionId, USHORT &usStatusCode, LPSTR &lpszDescription)
    {
        cout << "Disconnected from " << GetRemoteHost( usConnectionId ) << endl;
        return 0;
    }

    INT FireError(USHORT &usErrorCode, LPSTR &lpszDescription) 
    {
        return 0;
    }

    INT FireReadyToSend(USHORT &usConnectionId)
    {
        return 0;
    }
    virtual ~Daemon()
    {
    };
};

int main( int argc, char * argv[] )
{
    Daemon d;
    d.SetLocalHost( "192.168.1.192" );
    d.SetLocalPort( 4747 );
    d.SetListening( true );
    while( 1 )
    {
        d.DoEvents();
    }
}


...this is a simplified example of how easy it is to implement a daemon/server using IP*Works!. Note that you could very easily SetLocalPort to 25 and handle your incoming FireDataIn events within a FSM. Note that I SetLocalHost simply because my dev machine has several Ethernet cards, and the "handling" of multiple NICs is a bit "different" so it is just easier to specify the host address.


:davis:
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Student programming questions Shawn Miscellaneous Programming Forum 1 02-Apr-2007 21:31
[PROGRAM] Winsock Programming Max Payne MS Visual C++ / MFC Forum 1 08-Mar-2007 23:38
A few questions for someone in the programming industry. WAEvans Miscellaneous Programming Forum 0 08-Oct-2006 09:19
Couple of Socket Questions nugol C++ Forum 2 26-Aug-2006 12:38
Where to go for Registry editing and socket programming ? realnapster C++ Forum 2 08-Feb-2006 08:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.