Would Like To Know How To Architect As Well Program Client/Server using CSocketFile For Transferring a "Case" Object...
-
Dear Friends, I would like To Send my "Case" Object From One Client To Any Other Clients (whom I would Target To) Through My Server.The Server is to be Multithreaded.I would like to Use CSocketFile Class to make the Transfer from Client To Server And Then ,from Server To Other Client(s). I am Presenting My "Case" Class , to enable you to help me better.I would like to know which Classes I need To Build and where.I am very new to Socket Programming,I need the Architecture. Class CCase { protected: char CaseID[10]; char CaseName[20]; int ConsultationCount; public: char* GetCaseID(); void SetCaseID(char NewCaseID[10]); char* GetCaseName(); void SetCaseName(char NewCaseName[20]); int GetConsultationCount(); void SetConsultationCount(int NewConsultationCount); }; Also, Apart From the Above declarations,my Case Class Also Contains some objects (of Other Classes,I used) as members. Just Have A Look At My Code : ************************************************************* //Case.cpp IMPLEMENT_SERIAL (CCase,CObject,1) void CCase::Serialize(CArchive& ar) { CObject::Serialize (ar); if(ar.IsStoring ()) { ar.Read (this->CaseID ,20); ar.Read (this->CaseName,20); ar.Read ((void*)this->ConsultationCount,100) ; ar.Flush (); } else if(ar.IsLoading()) { ar.Write (this->CaseID,20 ); ar.Write(this->CaseName ,20); ar.Write ((void*)this->ConsultationCount,100 ); ar.Flush (); } } char* CCase::GetCaseID() { return this->CaseID ; } void CCase::SetCaseID(char NewCaseID[10]) { strcpy(this->CaseID ,NewCaseID); } char* CCase::GetCaseName() { return this->CaseName ; } void CCase::SetCaseName(char NewCaseName[20]) { strcpy(this->CaseName ,NewCaseName); } int CCase::GetConsultationCount() { return this->ConsultationCount ; } void CCase::SetConsultationCount(int NewConsultationCount) { this->ConsultationCount =NewConsultationCount; } CCase::~CCase() { } ************************************************************* //MyServer.cpp::InitInstance() : : : if (!ProcessShellCommand(cmdInfo)) return FALSE; CSocket ServerSocket; BOOL Created; BOOL ListenStatus; int nConnectionBacklog=5; BOOL AcceptClientRequestStatus; Created=ServerSocket.Create (20248,SOCK_STREAM,"200.200.200.44"); if(Created==FALSE) { AfxMessageBox("Could NOT Create ServerSocket!!!"); return FALSE; }
-
Dear Friends, I would like To Send my "Case" Object From One Client To Any Other Clients (whom I would Target To) Through My Server.The Server is to be Multithreaded.I would like to Use CSocketFile Class to make the Transfer from Client To Server And Then ,from Server To Other Client(s). I am Presenting My "Case" Class , to enable you to help me better.I would like to know which Classes I need To Build and where.I am very new to Socket Programming,I need the Architecture. Class CCase { protected: char CaseID[10]; char CaseName[20]; int ConsultationCount; public: char* GetCaseID(); void SetCaseID(char NewCaseID[10]); char* GetCaseName(); void SetCaseName(char NewCaseName[20]); int GetConsultationCount(); void SetConsultationCount(int NewConsultationCount); }; Also, Apart From the Above declarations,my Case Class Also Contains some objects (of Other Classes,I used) as members. Just Have A Look At My Code : ************************************************************* //Case.cpp IMPLEMENT_SERIAL (CCase,CObject,1) void CCase::Serialize(CArchive& ar) { CObject::Serialize (ar); if(ar.IsStoring ()) { ar.Read (this->CaseID ,20); ar.Read (this->CaseName,20); ar.Read ((void*)this->ConsultationCount,100) ; ar.Flush (); } else if(ar.IsLoading()) { ar.Write (this->CaseID,20 ); ar.Write(this->CaseName ,20); ar.Write ((void*)this->ConsultationCount,100 ); ar.Flush (); } } char* CCase::GetCaseID() { return this->CaseID ; } void CCase::SetCaseID(char NewCaseID[10]) { strcpy(this->CaseID ,NewCaseID); } char* CCase::GetCaseName() { return this->CaseName ; } void CCase::SetCaseName(char NewCaseName[20]) { strcpy(this->CaseName ,NewCaseName); } int CCase::GetConsultationCount() { return this->ConsultationCount ; } void CCase::SetConsultationCount(int NewConsultationCount) { this->ConsultationCount =NewConsultationCount; } CCase::~CCase() { } ************************************************************* //MyServer.cpp::InitInstance() : : : if (!ProcessShellCommand(cmdInfo)) return FALSE; CSocket ServerSocket; BOOL Created; BOOL ListenStatus; int nConnectionBacklog=5; BOOL AcceptClientRequestStatus; Created=ServerSocket.Create (20248,SOCK_STREAM,"200.200.200.44"); if(Created==FALSE) { AfxMessageBox("Could NOT Create ServerSocket!!!"); return FALSE; }
Hi there, To allow the server app to forward received objects to other clients, you first need to expand your server to allow multiple clients to connect. Just call ServerSocket.Accept() in a loop and store the returned CSocket (or derived) objects, one for each connected client, somewhere (you could use a CList for this). I guess the easiest way to handle client requests is to derive a class from CSocket and pass instances of these objects to Accept(). By overriding the OnReceive() method (see CSocket's base class CAsyncSocket in the online docs), you can cause code to be executed then data is received from a client. Just receive the object like you did in your example and forward it to all other connected clients using the socket objects you stored. You should also override the OnClose() method to remove socket objects from the socket list if the connection is being closed. As an aside, you could change your CaseId and CaseName member variables of CCase to CStrings, which allows you to (de)serialise them using the << and >> operators, ie. ar << CaseID << CaseName << ConsultationCount; Hope this helps (and works ;) ) Alwin