Sign in to follow this  
TFS_Waldo

MFC Sockets (Chat App.) :: Socket class loses data when passed to another class?

Recommended Posts

Hey, everyone. I am working on a simple chat application that allows two people to chat back and forth. Now, I have a problem with this. The program has a server and a client. Whichever user wants to host the chat will run the server. The other user will input the other's IP address in and click "Connect". Well, that's not a problem. Those are the two main windows. Once a server is started, a separate dialog pops up, which is the chat window itself. Same goes for when the client makes a connection with the server. (BTW, they are two separate projects in the same workspace. =P) Each one includes a socket class, which I derived from 'CSocket.' The socket classes are member variables of the 'CServerDlg' and 'CClientDlg' classes, which are the two main windows. When a connection is started, and I send a message directly from the server (ex. "You connected! =)") it works just fine. But when I try to send a message after that, I get the error code '10038,' which is an operation on a non-socket. I have a function in the 'CChatDlg' class declared as 'SetSocket(CCliSocket sock);' and it sets the 'm_cliSock' member of the 'CChatDlg' class to the 'CCliSocket' passed to the 'SetSocket' function. Well, I think it is losing the socket data when I call 'SetSocket.' Can anyone offer any suggestions or anything of the sort? Thanks in advance! =)

Share this post


Link to post
Share on other sites
Your SetSocket function should look like:

SomeDlg::SetSocket(CCliSocket& sock)
{
// Code
}


Passing it as reference will make sure the socket won't invalidate. Perhaps you should turn it into a pointer and turn the member variable of the dialog into a pointer aswell, so you only have 1 instance of the socket.

Toolmaker

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this