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

Communication problem with CSocket - CSocketFile - CArchive

Hi,
I try to implement a small socket app, a server and a client. The server just should print out, what the client send to him.
I think that the connections stand, but the communication doesn't work. If I try to read from the archive at the server side, it throws an ugly error, if i try to evaluate Archive::IsBufferEmpty, it's always empty. But the client permanently send the string "hallo" and flushes.
I have no any ideas left, here comes the code :

bool CSocket1Dlg::Server()
{
     CSocket ServerSocket;

     UpdateData();

     if( ! m_Socket.Create( m_iPort ) )
     {
          AfxMessageBox( _T("Couldn't create Serversocket"));
          return false;
     }
     if( ! m_Socket.Listen() )
     {
          AfxMessageBox( _T("Couldn't listen at Serversocket"));
          return false;
     }
     m_Socket.Accept( ServerSocket );

     CSocketFile SockFile( & m_Socket );
     CArchive     Archive( & SockFile, CArchive::load );

     while( 1 )
     {
          if( ! Archive.IsBufferEmpty() )
          {
               Archive >> m_sEditRead;

               m_EditRead.UpdateData( false );
               m_EditRead.RedrawWindow();
          }
     }

     return true;
}

bool CSocket1Dlg::Client()
{
     CString s("H A L L O");
     UpdateData();

     if( ! m_Socket.Create() )
     {
          AfxMessageBox( _T("Couldn't create Clientsocket"));
          return false;
     }
     m_Socket.Connect( getIPAdressString(), m_iPort );

     CSocketFile SockFile( & m_Socket );
     CArchive     Archive( & SockFile, CArchive::store );

     while( 1 )
     {
          Archive << s;
          Archive.Flush();
     }

     return true;
}

thanxs a lot
0
DennisWood
Asked:
DennisWood
  • 4
  • 2
1 Solution
 
Roshan DavisCommented:
How you run client and server in the same thread.

u are using the infinite loop .
So the server never get a chance to connect...

Roshmon
0
 
ambienceCommented:
are you using multiple threads ? , if not then you should.

try spawning the server thread first and maybe with a higher priority than the client thread.

Or better yet put a delay in client, so as to give the server a fair chance to catch up.
0
 
DennisWoodAuthor Commented:
Sorry, it looks a little bit confusing, in fact it's one application, but i start two instances, one as client, the other one as server. The infinite loops is just while i'm still testing, but all networkfunctionality is in the two methodes. Nothing gets initializied in another part.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ambienceCommented:
CSocketFile SockFile( & m_Socket );

change that to

CSocketFile SockFile( & ServerSocket);
   
0
 
DennisWoodAuthor Commented:
OK I'll try it, would maybe take until monday, cause I don't have windowsaccess
0
 
DennisWoodAuthor Commented:
Sorry, but it still won't work. I'm really out of any idea solving this problem.
0
 
DennisWoodAuthor Commented:
Now I've just have a "workaround", byletting the CArchive away.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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