Socket Programming
-
Hi Jobin, I have created a simple example to simulate TCP using MFC CSocket. The purpose of the code is Server Listens at port number 6000. Once client gets connects to 6000, client sends a message to Server and the server should display the received message. The server code is running correctly in the Application generated by AppWizardExe (include windows sockets check box Yes)but not at console based program. The code is as follows s.Create (6000); s.Listen (); s.Accept (t); n = t.Receive ((void*)l,20); l[n] = 0; cout<
-
Hi Jobin, I have created a simple example to simulate TCP using MFC CSocket. The purpose of the code is Server Listens at port number 6000. Once client gets connects to 6000, client sends a message to Server and the server should display the received message. The server code is running correctly in the Application generated by AppWizardExe (include windows sockets check box Yes)but not at console based program. The code is as follows s.Create (6000); s.Listen (); s.Accept (t); n = t.Receive ((void*)l,20); l[n] = 0; cout<
The problem is using MFC in a console application. By default, the MFC socket classes need a window to process socket events. You could get it to work with the proper MFC initialization in a console app but I'm not sure it's worth the trouble unless you're using other MFC classes. You could try using direct calls to WinSock instead of using the MFC classes. If you must use MFC then can you post the code used to initialize MFC? Mark
-
Hi Jobin, I have created a simple example to simulate TCP using MFC CSocket. The purpose of the code is Server Listens at port number 6000. Once client gets connects to 6000, client sends a message to Server and the server should display the received message. The server code is running correctly in the Application generated by AppWizardExe (include windows sockets check box Yes)but not at console based program. The code is as follows s.Create (6000); s.Listen (); s.Accept (t); n = t.Receive ((void*)l,20); l[n] = 0; cout<
Hi I think your code have to work. Only a question, Have you create the console app with MFC options? David Leyva
-
Hi Jobin, I have created a simple example to simulate TCP using MFC CSocket. The purpose of the code is Server Listens at port number 6000. Once client gets connects to 6000, client sends a message to Server and the server should display the received message. The server code is running correctly in the Application generated by AppWizardExe (include windows sockets check box Yes)but not at console based program. The code is as follows s.Create (6000); s.Listen (); s.Accept (t); n = t.Receive ((void*)l,20); l[n] = 0; cout<
Clarification for David's question - Have you create the console app with MFC options? MFC options mean In the preferences I set the option "Use MFC as a shared DLL". Is that you mean or some thing else? Alse the variable s is of type CSocket. Other than the above mentioned code I did not write any thing extra (Except declarations). l is a character array of size 20 and n is an int type. s,t are variables of type CSocket. For Mark: --------- I did not write any code related to MFC initialization. Please tell me what is that code to initialize MFC in a console based application. Also, Please mail me how to attach my code to a window in a console based application without that window to be displayed. I did not understand the statement "MFC socket classes need a window to process socket events". Did not understand means How to simulate that is thing I did not understand. Srikanth K
-
Clarification for David's question - Have you create the console app with MFC options? MFC options mean In the preferences I set the option "Use MFC as a shared DLL". Is that you mean or some thing else? Alse the variable s is of type CSocket. Other than the above mentioned code I did not write any thing extra (Except declarations). l is a character array of size 20 and n is an int type. s,t are variables of type CSocket. For Mark: --------- I did not write any code related to MFC initialization. Please tell me what is that code to initialize MFC in a console based application. Also, Please mail me how to attach my code to a window in a console based application without that window to be displayed. I did not understand the statement "MFC socket classes need a window to process socket events". Did not understand means How to simulate that is thing I did not understand. Srikanth K
Does your app have a main() function or WinMain()? If so, get rid of that and set the programs entry point (in linker options) to wWinMainCRTStartup. If you are using MFC then MFC handles the entry point for you and initializes itself. As with any normal MFC app you'll need ONE global static object of a class dervived from CWinApp. Override InitInstance() in this class and perform initialization there, including initializing Windows Sockets with WSAStartup(). You don't need to create a main window for the app since it's a console app.
-
Clarification for David's question - Have you create the console app with MFC options? MFC options mean In the preferences I set the option "Use MFC as a shared DLL". Is that you mean or some thing else? Alse the variable s is of type CSocket. Other than the above mentioned code I did not write any thing extra (Except declarations). l is a character array of size 20 and n is an int type. s,t are variables of type CSocket. For Mark: --------- I did not write any code related to MFC initialization. Please tell me what is that code to initialize MFC in a console based application. Also, Please mail me how to attach my code to a window in a console based application without that window to be displayed. I did not understand the statement "MFC socket classes need a window to process socket events". Did not understand means How to simulate that is thing I did not understand. Srikanth K
Hi Srikanth, Also make sure you hit reply on my messages if you want me to see your replies. It was just by chance I looked this far into old messages and saw that you had replied :) Mark