Socket Programming : connecting to server
-
hello ppl i am a newbie to socket programming i am having problems in connecting a client to a server. This is what my SERVER code looks like : int MainFrame ::OnCreate(LPCREATESTRUCT lpcs) { if(!AfxSocketInit()) MessageBox("Socket init error"); //create the server first if(server.Create(1000,SOCK_STREAM)) MessageBox("Server Socket Created"); CWnd::SetTimer(1,1000,0); return CWnd::OnCreate(lpcs); } void MainFrame::OnTimer(UINT nIDEvent) { if(!server.Listen()) MessageBox("Server not responding"); CWnd::OnTimer(nIDEvent); } What this code tries to do is that it creates a server on PORT 1000 and listens for incoming connections after each second 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(2000,SOCK_STREAM)) MessageBox("Client Socket Created"); return CWnd::OnCreate(lpcs); } void MainFrame ::OnConnect() { while(client.Connect("172.16.64.89",1000) != TRUE ) client.Connect("172.16.64.89",1000); MessageBox("Connected to Server"); } Everything goes fine here except when i am trying to listen to the server using Connect it never suceeds please help me out whats wrong with my code what modifications are to be made will be very much thankful Regards Cyberizen
-
hello ppl i am a newbie to socket programming i am having problems in connecting a client to a server. This is what my SERVER code looks like : int MainFrame ::OnCreate(LPCREATESTRUCT lpcs) { if(!AfxSocketInit()) MessageBox("Socket init error"); //create the server first if(server.Create(1000,SOCK_STREAM)) MessageBox("Server Socket Created"); CWnd::SetTimer(1,1000,0); return CWnd::OnCreate(lpcs); } void MainFrame::OnTimer(UINT nIDEvent) { if(!server.Listen()) MessageBox("Server not responding"); CWnd::OnTimer(nIDEvent); } What this code tries to do is that it creates a server on PORT 1000 and listens for incoming connections after each second 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(2000,SOCK_STREAM)) MessageBox("Client Socket Created"); return CWnd::OnCreate(lpcs); } void MainFrame ::OnConnect() { while(client.Connect("172.16.64.89",1000) != TRUE ) client.Connect("172.16.64.89",1000); MessageBox("Connected to Server"); } Everything goes fine here except when i am trying to listen to the server using Connect it never suceeds please help me out whats wrong with my code what modifications are to be made will be very much thankful Regards Cyberizen
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