• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

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!
0
khgoh
Asked:
khgoh
1 Solution
 
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
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now