Solved

Communication problem with CSocket -  CSocketFile - CArchive

Posted on 2002-05-29
7
580 Views
Last Modified: 2013-11-20
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
Comment
Question by:DennisWood
  • 4
  • 2
7 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
Comment Utility
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
 
LVL 22

Expert Comment

by:ambience
Comment Utility
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
 

Author Comment

by:DennisWood
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 22

Accepted Solution

by:
ambience earned 200 total points
Comment Utility
CSocketFile SockFile( & m_Socket );

change that to

CSocketFile SockFile( & ServerSocket);
   
0
 

Author Comment

by:DennisWood
Comment Utility
OK I'll try it, would maybe take until monday, cause I don't have windowsaccess
0
 

Author Comment

by:DennisWood
Comment Utility
Sorry, but it still won't work. I'm really out of any idea solving this problem.
0
 

Author Comment

by:DennisWood
Comment Utility
Now I've just have a "workaround", byletting the CArchive away.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now