Question on CSocket

I am learning how to use CSocket and rougly wrote a server and client program.

server program:
#include <iostream.h>
#include <afxsock.h>
#include <afx.h>


void main()
{
      bool bInitFlag = AfxSocketInit();
      if (!bInitFlag)
            cout <<"Socket Initialization failed........."<<endl;
      
      CSocket socketServer;
  int flag = -1;
      flag = socketServer.Create(900);//, SOCK_STREAM, "137.57.140.111");
      
      if (!flag)
            cout <<socketServer.GetLastError()<<endl;
      else
      {
            CString socketName;
            UINT socketPort;
            socketServer.GetSockName(socketName, socketPort);
            cout <<"Socket Name is "<<socketName<<endl;
            cout <<"Socket Port is "<<socketPort<<endl;
      }

      cout <<"listening.............."<<endl;
      
      while (true)
      {
            if (socketServer.Listen())
            {
                  CSocket socketReceive;
                  
                  // accept connection
                  
                  if (!socketServer.Accept( socketReceive ))
                  {
                        cout <<"connection failed.............."<<endl;
                        break;
                  }
                  
                  cout <<"connection established........."<<endl;
                  
                  CString socketName;
                  UINT socketPort;
                  socketReceive.GetPeerName(socketName, socketPort);
                  cout <<"Peer Socket Name is "<<socketName<<endl;
                  cout <<"Peer Socket Port is "<<socketPort<<endl;
            
                  CSocketFile file(&socketReceive);
                  
                  CArchive arIn(&file, CArchive::load);
                  CArchive arOut(&file, CArchive::store);
                  CString sReceive;                              
                  bool flag = false;
                  while ( true )
                  {
                        flag = false;
                        flag = ( arIn.ReadString(sReceive)==0) ? false: true;
                        if (flag)
                              cout <<sReceive<<endl;
                  }
            }
      }
}
----------------------------------------------------      
client program listed below:
void main()
{      
      bool bInitFlag = AfxSocketInit();
      if (!bInitFlag)
            cout <<"Socket Initialization failed........."<<endl;
      
      CSocket socketClient;
      int flag = socketClient.Create();
      
      if (!flag)
            cout <<socketClient.GetLastError()<<endl;
      else
      {
            CString socketName;
            UINT socketPort;
            socketClient.GetSockName(socketName, socketPort);
            cout <<"Socket Name is "<<socketName<<endl;
            cout <<"Socket Port is "<<socketPort<<endl;
            socketClient.Connect("137.57.140.111", 900);    //wherever is Server is running; can be your own machine
      }

      CSocketFile file(&socketClient);
      CArchive arIn(&file, CArchive::load);
      CArchive arOut(&file, CArchive::store);
      
      CString sData;
      while (true)
      {
            char data[256];
            cout <<"Before writing................ "<<endl;
            cin.getline(data, 256);
            sData = data;
            if (sData == "/quit")
                  break;
            cout <<sData<<endl;
            arOut.WriteString (sData);
            cout <<"After writing................ "<<endl;
      }
      
      cout <<"quiting........"<<endl;
}




what i am trying to do here is to establish a communication channel between one server and one client.  The client can keep sending CString to the server (server will continue readString and display in MSDOS prompt).

I am not able to accomplish that til I type /quit in client program so the client quit.  Then, the server is able to read and display the CString I sent earlier.

Hope anyone can help me out and I can progress on learning CSocket.  Thanks!
khgohAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ToronadoCommented:
Hi khgoh,

I don't have much experience in using the CSocketFile class but I think you have to call the Flush () function of arOut.
See for more info "Windows Sockets: Example of Sockets Using Archives" in the Visual C++ programmers guide.

Success,
 Toronado
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
khgohAuthor Commented:
Flush is a problem.....

But mainly adding Flush wont solve the problem.......

I solve the problem by using another write function

Write(char, 256);
instead of WriteString(CString);
then add Flush ();

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.