Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Would Like To Know How To Architect As Well Program Client/Server using CSocketFile For Transferring a "Case" Object...

Would Like To Know How To Architect As Well Program Client/Server using CSocketFile For Transferring a "Case" Object...

Scheduled Pinned Locked Moved C / C++ / MFC
c++sysadminarchitecturehelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    G V Bhaskar
    wrote on last edited by
    #1

    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; }

    A 1 Reply Last reply
    0
    • G G V Bhaskar

      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; }

      A Offline
      A Offline
      Alwin75
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups