how to create a client server in MFC

I have never used MFC before, just c++ console applications, and c#.
I just created a client server program in a c++ console application but now i need to make something similar in MFC.

I have a list box and a button (this is the server). When the button is clicked i want all the client's ip addresses that are connected to the server displayed in the list box.

Can someone please help with this?
I have a list box and a button (this is the server). When the button is clicked i want all the client's ip addresses that are connected to the server displayed in the list box.
A TCP server gets the client address when it connects. It the returned from accept().

If you want to list the addresses of connected clients, you need to trim this set of client addresses to the clients still connected and return that. You know when a client is disconnection becuase it's the ones you haven't called closesocket() on.
can a client run without a server? then when i click the button the server runs and then all waiting clients can connect? then the ip addresses of those clients get displayed in the list box

can you give me source code or link some tutorials to do this?
I have never used MFC before
Last edited on
The socket code it will be exactly the same if that already works for you. What exactly do you want ? How to add/remove strings from a list box in MFC ?
http://msdn.microsoft.com/en-us/library/y04ez4c9(v=vs.100).aspx
http://www.codeproject.com/Articles/796/Using-the-CListBox-control
i created a client-server program in c++ console. but this program is different so i don't think i can use the same code.

When i click the button i want the list box to populate with any clients that are connected to the server
can a client run without a server?
Not really. Your connection to the server will fail in connect(). What you do then is up to you.

when i click the button the server runs and then all waiting clients can connect?
Well, yes. The clients ought to be able to connect once you run the server.

Each time a client connects, accept() in the server will return a socket and the address of the client. You need to store these together and maintain a list of connected clients. You'll need the socket to talk to the client, but you'll also have the addresses for your dialog.

can you give me source code or link some tutorials to do this?
Not really, I don't have any to hand. BeeJ's guide ought to be sufficient.

I have never used MFC before
The complication is that a traditional Unix server sits around waiting for someone to connect in on a file descriptor.

A traditional GUI sits around waiting for some kind of message to arrive. So they're similar in that they're both sitting around waiting for something to happen.

There has been some effort to make a socket event look like a normal Windows event so they can be handled in the same way. That's the WinSock Async interface. Don't use it, it's a waste of everyones time.

I recommend you create a separate thread and run the server there. If the server needs to communicate with the GUI, it can construct a Windows message and send it using PostThreadMessage(). Sorry again, I don't have an example to hand.
so there is no way for a client to just sit around and wait for the server?
the server must always be running first?
Yes, there is, but you'll need to code it yourself.

connect() will timeout and you should implement an exponential back-off retry method. That is, wait, 1, 2, 4, 8, 16, 32 seconds. That way your clients don't hammer the box while the server's trying to start.
thanks for help but i really have no clue how do to this, since I like i said i have never used mfc. I have tried searching for simple client-server programs in mfc but i can't find any
Last edited on
Well, you have to start somewhere. What have you done so far?
I have done nothing but make the GUI for the server (list box and button).
I just don't know where to start
You could start by creating a dialog app using the wizard. Add the controls you need then we can think about wiring them up.

I'll just sort of ramble on where I can as I don't really use Windows these days. I have a Windows box to hand so it if it comes to that, but I really don't want to use it.
yes i have created a dialog
Have you added the controls and mapped them onto objects in the dialog class?

You'll also need to create to a thread that will run the socket server. Start it from OnInitDialog() and shut it down in OnClose().

You can set up a data structore to store the client connected, i.e. the socket and address. You'll need a mutex to protect it as the list box populator will read it, while the server maintains it.
Topic archived. No new replies allowed.