I'm not 100% familiar with CAsyncSocket (this is what you are using?), but this tips might help/be applicable, but do take it with a pinch of salt!! 1. I don't think you have to "Listen" repeatedly, just call Listen() once 2. You need to Accept() connections in you server, implement the OnAccept() notification 3. You shouldn't bind you client socket to a specific port, rather use Create() and let the socket pick the best port for the client 4. Don't use ports in the 0-1024 range, these are resevered, rather us a number > 7000, this is what I usually do. So maybe: class CMySocket : public CAsyncSocket { CSocket m_Client; protected: virtual void OnAccept(int) { Accept(m_Client); } } ... //in MainFrame CMySocket server; ... int MainFrame ::OnCreate(LPCREATESTRUCT lpcs) { if ( !AfxSocketInit() ) { MessageBox("Socket init error"); } //create the server first if ( server.Create(7000,SOCK_STREAM) ) { server.Listen(); MessageBox("Server Socket Created"); } return CWnd::OnCreate(lpcs); } >>now the second part that is the CLIENT has the following code int MainFrame ::OnCreate(LPCREATESTRUCT lpcs) { if ( !AfxSocketInit() ) { MessageBox("Socket init error"); } if ( client.Create() ) { MessageBox("Client Socket Created"); } return CWnd::OnCreate(lpcs); } // OnConnect is a method delacared in your mainframe // and called by you in some way, maybe in response to // a menu item, etc DONT FORGET TO CALL THIS METHOD!!! void MainFrame ::OnConnect() { // this is nasty!! there should be a delay of some kind // or perhaps some other mechansim, if the connection fails // this will just flood the poor server!! // what if it never succeeds? You app will just lock up and the // MMI will cease to respond! while ( client.Connect("172.16.64.89",7000) != TRUE ) { client.Connect("172.16.64.89",7000); } MessageBox("Connected to Server"); }
Windows, Linux and Internet Development Consultant Email: corporate@scriptsmiths.com Web: http://www.scriptsmiths.com