MFC Socket listener

Hello everyone, got a quick question here that hopefully someone can help me
with.  Basically I am having trouble with my Client application which uses
MFC CSockets.  I currently can only listen to one port (say 4000), and I
need to be able to listen to multiple ports.  Originally I just set up two
listeners (one listening on 4000 another on 4200) but the problem with this
is that I need to know which Listener is flagged when I call the OnAccept()
method (actually this method is calling another function
OnProcessPendingAccept() at which point I need to know which listener got
tagged).  Basically what keeps happening is that the first listener in my
routine will try to Accept, and get stuck in an infinite loop if it doesn't
have the correct port (say 4000 connects first then 4200 will just crash).
Can I easily get a CSocket listener to scan multiple ports (and is so when I
go to call the Accept function how do I know that it will have the matching
port assignments?
Basically this line keeps crashing my program:
if (m_MySocket.Accept(*pSocket))

m_MySocket is of type MySocket which is of type CSocket.

pSocket of course is just an empty CClientSocket that again is of type
CSocket

Hope this isn't too confusing, basically I just need to know how to tell my
CSocket NOT to accept if it doesn't have the same port address (or else have
a listener which can accept over many different port address's).

Thanks for any help :)



0
Arlis
12/10/2003 4:03:52 PM
vc.mfc 33608 articles. 0 followers. Follow

4 Replies
1143 Views

Similar Articles

[PageSpeed] 0

I really don't understand the problem you are having, but I have done what
you are describing many times.
Say I have two different clients that want to connect to me. One on port
4000 and one on 4001. Then in that case I have in the past created two
different socket classes.

class CListen4000 : public CSocket        //this would be the listener for
one type of client
{
};

CListen4000::OnAccept()
{
    Tell the manager class that someone from port 4000 has connected
    Manager->Connected(4001,hSocket,....);
}

class CListen4001 : public CSocket    //this would be the listener for
another type of client
{
};

CListen4001::OnAccept()
{
    Tell the manager class that someone from port 4001 has connected
    Manager->Connected(4001,hSocket........);
}

and this approch has always worked for me.

Now I guess you can use one class

class CListenSocket : public CSocket
{
public:
    CListenSocket(int Port);
    void Listen();
private:
    int m_Port;
}

CListenSocket::OnAccpet()
{
    Manger->Connected(m_Port,hSocket....);
}

Ali R.
"Arlis Rose" <arlisATendevouraerospace.com> wrote in message
news:%23JE5NdzvDHA.3116@tk2msftngp13.phx.gbl...
> Hello everyone, got a quick question here that hopefully someone can help
me
> with.  Basically I am having trouble with my Client application which uses
> MFC CSockets.  I currently can only listen to one port (say 4000), and I
> need to be able to listen to multiple ports.  Originally I just set up two
> listeners (one listening on 4000 another on 4200) but the problem with
this
> is that I need to know which Listener is flagged when I call the
OnAccept()
> method (actually this method is calling another function
> OnProcessPendingAccept() at which point I need to know which listener got
> tagged).  Basically what keeps happening is that the first listener in my
> routine will try to Accept, and get stuck in an infinite loop if it
doesn't
> have the correct port (say 4000 connects first then 4200 will just crash).
> Can I easily get a CSocket listener to scan multiple ports (and is so when
I
> go to call the Accept function how do I know that it will have the
matching
> port assignments?
> Basically this line keeps crashing my program:
> if (m_MySocket.Accept(*pSocket))
>
> m_MySocket is of type MySocket which is of type CSocket.
>
> pSocket of course is just an empty CClientSocket that again is of type
> CSocket
>
> Hope this isn't too confusing, basically I just need to know how to tell
my
> CSocket NOT to accept if it doesn't have the same port address (or else
have
> a listener which can accept over many different port address's).
>
> Thanks for any help :)
>
>
>


0
nospam7515 (2085)
12/10/2003 4:21:18 PM
Hey Ali, aye basically that is an idea that I thought about, but I'm not
sure if that would be a good way to go.  I will probably have around 20 or
30 connections (to start with at least) so I'm not sure if I'd want 20-30
more classes kicking around for each Listener.  However (at least for now
for testing purposes) I'm going to try that route and see if I can at least
get it to work (I'm still a total MFC NewB so any programming practice is
good) :)
Thanks for the reply,


"Ali R." <nospam@nospam.com> wrote in message
news:2KHBb.311$AM.129429649@newssvr11.news.prodigy.com...
> I really don't understand the problem you are having, but I have done what
> you are describing many times.
> Say I have two different clients that want to connect to me. One on port
> 4000 and one on 4001. Then in that case I have in the past created two
> different socket classes.
>
> class CListen4000 : public CSocket        //this would be the listener for
> one type of client
> {
> };
>
> CListen4000::OnAccept()
> {
>     Tell the manager class that someone from port 4000 has connected
>     Manager->Connected(4001,hSocket,....);
> }
>
> class CListen4001 : public CSocket    //this would be the listener for
> another type of client
> {
> };
>
> CListen4001::OnAccept()
> {
>     Tell the manager class that someone from port 4001 has connected
>     Manager->Connected(4001,hSocket........);
> }
>
> and this approch has always worked for me.
>
> Now I guess you can use one class
>
> class CListenSocket : public CSocket
> {
> public:
>     CListenSocket(int Port);
>     void Listen();
> private:
>     int m_Port;
> }
>
> CListenSocket::OnAccpet()
> {
>     Manger->Connected(m_Port,hSocket....);
> }
>
> Ali R.
> "Arlis Rose" <arlisATendevouraerospace.com> wrote in message
> news:%23JE5NdzvDHA.3116@tk2msftngp13.phx.gbl...
> > Hello everyone, got a quick question here that hopefully someone can
help
> me
> > with.  Basically I am having trouble with my Client application which
uses
> > MFC CSockets.  I currently can only listen to one port (say 4000), and I
> > need to be able to listen to multiple ports.  Originally I just set up
two
> > listeners (one listening on 4000 another on 4200) but the problem with
> this
> > is that I need to know which Listener is flagged when I call the
> OnAccept()
> > method (actually this method is calling another function
> > OnProcessPendingAccept() at which point I need to know which listener
got
> > tagged).  Basically what keeps happening is that the first listener in
my
> > routine will try to Accept, and get stuck in an infinite loop if it
> doesn't
> > have the correct port (say 4000 connects first then 4200 will just
crash).
> > Can I easily get a CSocket listener to scan multiple ports (and is so
when
> I
> > go to call the Accept function how do I know that it will have the
> matching
> > port assignments?
> > Basically this line keeps crashing my program:
> > if (m_MySocket.Accept(*pSocket))
> >
> > m_MySocket is of type MySocket which is of type CSocket.
> >
> > pSocket of course is just an empty CClientSocket that again is of type
> > CSocket
> >
> > Hope this isn't too confusing, basically I just need to know how to tell
> my
> > CSocket NOT to accept if it doesn't have the same port address (or else
> have
> > a listener which can accept over many different port address's).
> >
> > Thanks for any help :)
> >
> >
> >
>
>


0
Arlis
12/10/2003 4:25:57 PM
I would try the second approch that I talked about first, the listener class
that takes a port as a paramter.

Ali R.

"Arlis Rose" <arlisATendevouraerospace.com> wrote in message
news:uM93jpzvDHA.2492@TK2MSFTNGP12.phx.gbl...
> Hey Ali, aye basically that is an idea that I thought about, but I'm not
> sure if that would be a good way to go.  I will probably have around 20 or
> 30 connections (to start with at least) so I'm not sure if I'd want 20-30
> more classes kicking around for each Listener.  However (at least for now
> for testing purposes) I'm going to try that route and see if I can at
least
> get it to work (I'm still a total MFC NewB so any programming practice is
> good) :)
> Thanks for the reply,
>
>
> "Ali R." <nospam@nospam.com> wrote in message
> news:2KHBb.311$AM.129429649@newssvr11.news.prodigy.com...
> > I really don't understand the problem you are having, but I have done
what
> > you are describing many times.
> > Say I have two different clients that want to connect to me. One on port
> > 4000 and one on 4001. Then in that case I have in the past created two
> > different socket classes.
> >
> > class CListen4000 : public CSocket        //this would be the listener
for
> > one type of client
> > {
> > };
> >
> > CListen4000::OnAccept()
> > {
> >     Tell the manager class that someone from port 4000 has connected
> >     Manager->Connected(4001,hSocket,....);
> > }
> >
> > class CListen4001 : public CSocket    //this would be the listener for
> > another type of client
> > {
> > };
> >
> > CListen4001::OnAccept()
> > {
> >     Tell the manager class that someone from port 4001 has connected
> >     Manager->Connected(4001,hSocket........);
> > }
> >
> > and this approch has always worked for me.
> >
> > Now I guess you can use one class
> >
> > class CListenSocket : public CSocket
> > {
> > public:
> >     CListenSocket(int Port);
> >     void Listen();
> > private:
> >     int m_Port;
> > }
> >
> > CListenSocket::OnAccpet()
> > {
> >     Manger->Connected(m_Port,hSocket....);
> > }
> >
> > Ali R.
> > "Arlis Rose" <arlisATendevouraerospace.com> wrote in message
> > news:%23JE5NdzvDHA.3116@tk2msftngp13.phx.gbl...
> > > Hello everyone, got a quick question here that hopefully someone can
> help
> > me
> > > with.  Basically I am having trouble with my Client application which
> uses
> > > MFC CSockets.  I currently can only listen to one port (say 4000), and
I
> > > need to be able to listen to multiple ports.  Originally I just set up
> two
> > > listeners (one listening on 4000 another on 4200) but the problem with
> > this
> > > is that I need to know which Listener is flagged when I call the
> > OnAccept()
> > > method (actually this method is calling another function
> > > OnProcessPendingAccept() at which point I need to know which listener
> got
> > > tagged).  Basically what keeps happening is that the first listener in
> my
> > > routine will try to Accept, and get stuck in an infinite loop if it
> > doesn't
> > > have the correct port (say 4000 connects first then 4200 will just
> crash).
> > > Can I easily get a CSocket listener to scan multiple ports (and is so
> when
> > I
> > > go to call the Accept function how do I know that it will have the
> > matching
> > > port assignments?
> > > Basically this line keeps crashing my program:
> > > if (m_MySocket.Accept(*pSocket))
> > >
> > > m_MySocket is of type MySocket which is of type CSocket.
> > >
> > > pSocket of course is just an empty CClientSocket that again is of type
> > > CSocket
> > >
> > > Hope this isn't too confusing, basically I just need to know how to
tell
> > my
> > > CSocket NOT to accept if it doesn't have the same port address (or
else
> > have
> > > a listener which can accept over many different port address's).
> > >
> > > Thanks for any help :)
> > >
> > >
> > >
> >
> >
>
>


0
nospam7515 (2085)
12/10/2003 4:32:59 PM
Arlis Rose wrote:

> Hey Ali, aye basically that is an idea that I thought about, but I'm not
> sure if that would be a good way to go.  I will probably have around 20 or
> 30 connections (to start with at least) so I'm not sure if I'd want 20-30
> more classes kicking around for each Listener.  However (at least for now
> for testing purposes) I'm going to try that route and see if I can at least
> get it to work (I'm still a total MFC NewB so any programming practice is
> good) :)
> Thanks for the reply,

You can use one listener socket class, with multiple objects of that 
class created - one for each port.  When it gets the OnAccept call you 
can use 'this' to look up in an array/list which listener it is, or you 
can save the port number in a listener member variable when you create 
each listener object.

-- 
Scott McPhillips [VC++ MVP]

0
scottmcp1 (239)
12/11/2003 2:19:40 AM
Reply:

Similar Artilces:

A strange problem with MFC
Hi, Rencently I met a strange problem. The code like below: CWinThread* pThread = AfxBeginThread(ThreadFunc, (LPVOID)this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED); if(pThread->m_hThread != NULL){ pThread->m_bAutodelete = FALSE; pThread->ResumeThread(); } After the statement "pThread->m_bAutodelete = FALSE" executed, I found the value of pThread->m_hThread is 0. While pThread->m_bAutodelete is still 1! The code section located in a win32 static library, with /Md option.The host application(exe) is a win32 console application, with /Md option. There...

Unicode in MFC
Hi, I am new to MFC programming and I have a problem: when I try to write the caption of a button or something else in my own language(Farsi), in the design mode it shows the correct string but when I compile it it shows a '?' instead of every character. Can anyone please help me with that. > I am new to MFC programming and I have a problem: > when I try to write the caption of a button or something else in my > own language(Farsi), in the design mode it shows the correct string > but when I compile it it shows a '?' instead of every character. Can > anyone ple...

return int value to OS in MFC App
Hi, Everybody. I have to make A MFC app to return int value to Operatiing System.. if i could use WinMain() function, this is not matter. but MFC does not reveal WinMain().... How can i solve this problem? Can Anybody help me with this problem? <flybluewing@bcline.com> wrote in message news:1189386249.923483.59250@y42g2000hsy.googlegroups.com... > Hi, Everybody. > > I have to make A MFC app to return int value to Operatiing System.. > > if i could use WinMain() function, this is not matter. > but MFC does not reveal WinMain().... > How can i solve this problem?...

mfc and IE activeX
I'm using VS 2003 and trying to use Internet Explorer in my MFC application but encountering problems If I built a very simple test dialog-application and drop in the IE it works exactly as expected. However, when I create a similar dialog box in my MFC application and call the ...DoModal() nothing appears at all - not even the dialog box itself. It's as if my application does not want to use ActiveX components - I also tried a couple of other Microsoft ActiveX controls and they have the same problem. I must be missing something obvious - any one with any ideas will be much ...

ActiveX Socket
I am developing an activex control with a TCP/IP socket. The activex control is written using vc++/mfc and the socket part is using CAsyncSocket class support of MFC. I am unable to connect to a server as I get WSAStartup not called 10093 error code. This is due to the activex test container supporting the control having a different process boundary. The proposed solution seems (and please correct me if I am wrong) to spawn a new thread from the activex control using afxbeginthread and make a connection to the server from this thread. Any comments on whether this will really work ? "High...

Need Help with MFC!!
Hi all, need help esp. with Graphic part. When you right click on any drive on your machine and click properties, you get a tabbed dialog. I want to know how can I embed graphic image (pie chart) into my MFC Dialog? I have written code which displays the Disk Usage right now but don't know how to display that info as a PIE CHART!!!!!! besides what type of MFC Dialog application should I look fwd to build? I want exactly as the DRIVE properties. TIA -- ======== Regards Vai ======== Have you looked at the CDC::Pie method? joe On Wed, 12 May 2004 23:07:11 -0400, "Vai2000"...

MFC to Console.
Hello Group. I need to convert the code of WiniNet Async Demo from microsoft samples from MFC to a console function.. Someone can help me with the changes.. The code is posted here Regards /* Microsoft Corporation Copyright 1999-2002 */ /******************************************************************** ProjectName : AsyncDemo Purpose : This sample demonstrates how to submit two WinInet requests, using InternentOpenUrl, asynchronously. Notes : This sample does not handle any authentication. To properly handle authentication, the functions that handle specific protocols (like Ht...

XML and MFC
Hello guys, What source of knowledge would you point me to if you were about to integrate XML with MFC? Of course if there's any Microsoft documentation anywhere, please post me a link to it. Thank you in advance! :-) Yea, I wanna know it too. Who would give a hand? > What source of knowledge would you point me to if you were about to > integrate XML with MFC? Of course if there's any Microsoft documentation > anywhere, please post me a link to it. After digging left and right, I remained with two main options: - MSXML - MS, proprietary, COM, with all the implications:...

Sending Window Messages from an MFC app to a Windows Firm tray application
Hello, I have an MFC MDI application and a Windows Forms (2.0) Tray Icon application. I want to run a command on the tray application via my MFC application. There is a "Register" context menu command on the tray icon application. I want to call the Register command from my MFC MDI application. I thought of a windows message. How do I do it? Is there any other better mechanism to do so? Thanks, Janiv Ratson. If you know the class of the window on the tray application you can use FindWindow() to get the window for the class. Then you can use either PostMessage() or SendMessag...

dynamically managing of layout in MFC + GridBagLayout in MFC or C++
Hi, can anybody tell me how can I make or find a function in MFC to manage layout of a dialog that dynamically has been create and also the controls in the dialog? I am new in MFC, what I am lokking for is something like GridBagLayout in java? is there something like this in C++ MFC ? any idea plz? Thank u There is nothing like GridBagLayout in MFC. However, you can achieve the same functionality thru various means. You have to move the controls manually on resize of the dialog or window. Please see the following examples: http://www.codeproject.com/dialog/easysize.asp http://www.codepr...

Console output from MFC dialog based app
Dear All, I have developed a MFC dialog based application. Now when I execute this app from command prompt, I need to write some text back to console. In console application we achieve this using functions like printf(), puts(), etc. Please let me know how do I achieve same in dialog based application since printf() and other functions are not working here. Thanks, Ajay You can't write to the same console window that you were ran from, but you can create a console window and write to that. Take a look at AllocConsole, ReadConsole, and WriteConsole. Here is an example: http://www....

Reporting back crashes in MFC-based products
Hello, We have a product written in MFC using Visual Studio 7.1. We are now planning to implement a feature that will detect a crash and report to us the cause of the crash and other meaningful information. The plan is to provide something similar to Window's error reporting tool. I found a 3rd party tool for Delphi that instruments the source code. When the program crashes, it provides information like the name of the function, module etc.. Is there something similar that I can use with MFC? The other option is to use MFC's Structured Exception Handling and try to get a memory dump...

Access and MFC
I would like to use some MFC capabilities from within Access. In PDC05/PRS313, Nick Kramer gave examples of using HWND's to create mixed Win32/Windows Presentation Foundation applications. I remember reading recently somewhere that Microsoft has released the MFC for general use. Can anyone provide information about (or even verify) the MFC release? Thanks, James A. Fortune CDMAPoster@FortuneJames.com MPAPoster@FortuneJames.com Microsoft Cabinet SDK: http://msdn2.microsoft.com/en-us/library/ms974336.aspx ...

bug of MFC ActiveX Control?
i create a default MFC activex control by wizard, i just check the windowlessActivate option, because i want to create a transparent control. but when i use the control in vb, the form_keydown event will be triger twice. i recompile the control after comment the follow line: dwFlags |= windowlessActivate; all things is ok. can someone confirm this "bug"? -- ף���ˣ� Maybe my english is so poor? Or something else? "sPhinX" <s5689412@163.com> д���ʼ� news:eQ$NrxkcFHA.4040@TK2MSFTNGP14.phx.gbl... > i create a default MFC activex control by wizard, > i just ch...

Static variables initialization problem with MFC DLL
Hi, I have the following code, working well in my MFC app, where a static member variable is initialized with the result of a static method, as this : int MyClass::anInt = MyClass::InitTheInt(); The problem is when this code is used in a DLL -using MFC in a shared DLL-, the initialization is simply ignored !!!! What's going on? Thx. Herbert wrote: >Hi, > >I have the following code, working well in my MFC app, where a >static member variable is initialized with the result of a static >method, as this : > > int MyClass::anInt = MyClass::InitTheInt(); &g...

Formatting Excell doc from within MFC app
Hello. I'm developing an application using MFC - VC++ 6.0 I'm wondering if there is an ActiveX control or other component that would make possible to make simple editing and formatting MS EXCELL doc from rithin application. The operations i would like to perform: - opening .xls file - inserting values into cells and simple formatting the cells (alliginig,, etc) - save the file - printing and displaying (not necessary) Thank You all for any help Sincerelly, magic ...

MFC Application has encountered a problem and needs to close. HELP
This is driving me nuts. Whenever I run my program in release configuraiton it always crashes with the above error message offering me an option to send a message to Microsoft. I have discovered it is an access violation (code 5). Whenever I run the program in debug configuration it runs OK. I have two main questions: 1 How can the program behave so differently in the two configurations? I can find no explanation of this in the help files. 2 How can I get useful information about the access violation? I cannot save the crash dump produced which the exception handler offers to send to ...

socket error 10060
Occasionally, when I'm on the road with my Windows Vista Laptop, I get this error signal (at hotels and other public wireless connections) and while I can receive email, I can't send any. So I googled 'socket error 10060' and got lots of advice, such as use 'Port 587' instead of 'Port 25' (which works fine at home) and I checked my ISP, which is Comcast, and went thru my "Tools", "Accounts", "Properties", in order to align things - without success. Can anyone put me on the path? Or do I need to go buy a Mac Notebook? ...

Is GDI+ Integrate with MFC?
Apparently, GDI+ is not intended to be used with MFC as GDI is. Is GDI+ a more stand-alone technology? Granted it can be used in MFC apps but the MFC classes do not seem to wrap GDI+. Is it resonable to use GDI+ without MFC at all? Thank You It's just another API. Why wouldn't it be reasonable to use without MFC? joe On Tue, 13 Feb 2007 21:54:49 -0800, Coder <Coder@discussions.microsoft.com> wrote: >Apparently, GDI+ is not intended to be used with MFC as GDI is. Is GDI+ a >more stand-alone technology? Granted it can be used in MFC apps but the MFC >classes...

Regular MFC dll
I created an MFC regular dll with MFC statically linked using MFC Appwizard(dll) and then I compiled the code generated by the AppWizard without adding any code. I got the these compile errors Compiling resources... Compiling... StdAfx.cpp c:\program files\microsoft sdk\include\shobjidl.h(9419) : error C2061: syntax error : identifier 'IXMLDOMDocument' c:\program files\microsoft sdk\include\shobjidl.h(9528) : error C2061: syntax error : identifier 'IXMLDOMDocument' Error executing cl.exe. How can I reomve these error. Thanks ...

Well not exactly MFC....though I am using it in MFC :)
Okay actually I am trying to write a DLL which my MFC dialog imports, but for some reason I cannot get something really simple done. Basically all I want to be able to do is copy a string that is given to a function, and then return it to the user unchanged (as within my program I modified the two strings which are given as parameters). What is weird is simply that I CANNOT delete a char * which I created using NEW??? Here is what I am trying to do.... I have char * tempString; //define in my .h file for the DLL. in the method void ChangeString(char * a, char * b) { tempString = ne...

How to insert a MFC Dialog in a DLL ?
I want to cerate a static DLL. In this DLL I want insert a MFC Dialog. I have made with the MSVisual C++ MFC AppWizard(dll) a DLL . I insert a test function in the DLL. With an other Progarmm I testet the test function the DLL . It works. Now I try insert a MCF- Dialog in the DLL but without success. Two days I was trying but no change. Knows everyone a sample or a link in the web. Thanks A beginner Daniel Daniel, If it is indeed the case that you have successfully created and called a DLL, you are close to your target. You want to check AFX_MANAGE_STATE. If you look t...

Monitor MFC Executable
I have an application that runs 24 hours a day. The problem I have is that it is crashing at random times, e.g 18 hours of usage, 44 hours of usage etc and when it crashes it leaves no message/error box/information. When I start my application, I kill explorer so that the desktop cannot be accessed. Could this be causing the crash in some way? The first time I have started my app by using the debug exe, which reported an error in the event viewer as shown below:- " The Instruction at "0x6dee 14ba" referenced memory at "0x025b8348". The memory could not be &quo...

VC++ MFC to VC++ 7.0 w/ MFC 7
This is a multi-part message in MIME format. ------=_NextPart_000_002E_01C3D9AD.F0EEB050 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I'm in the process of moving my code base to Visual Studio .Net 2003 = using VC++ w MFC 7.0. The code complies correctly but the linker is not = resolving what appears to be CString entry points, e.g.,=20 CookieField.obj : error LNK2019: unresolved external symbol "public: = void __thiscall CString::MakeLower(void)" (?MakeLower@CString@@QAEXXZ) = referenced in function "protected: vo...

Calling an Event in MFC ActiveX
I'm trying to call an event from another class. I get these errors when building: ---------------------------------------------------------------------------- ---- MyConnMngr.cpp(41) : error C2248: 'FireOnConnected' : cannot access protected member declared in class 'CXCtrl' CXCtl.h(65) : see declaration of 'FireOnConnected' MyConnMngr.cpp(41) : error C2352: 'CXCtrl::FireOnConnected' : illegal call of non-static member function CXCtl.h(65) : see declaration of 'FireOnConnected' -------------------------------------------------------...