Solved

CSocket and CSocketFile

Posted on 2000-03-07
3
645 Views
Last Modified: 2013-11-20
I am trying to make  a simple client server application using CSocket and CSocketFile.
I have coded 2 thread functions, one listens for clients requests, while after moving beyond Accept()... first thread funtion "ServerStarter" is supposed to spin the other thread whoise function is "ClientHandler".
Code is as under
UINT ServerStarter(LPVOID pParam)
{CClientSocket*      clientsocket = new CClientSocket();
CListenSocket*      listensocket = new CListenSocket();
.
.
listensocket->Accept(*clientsocket);
CWinThread *def = AfxBeginThread(ClientHandler,clientsocket,THREAD_PRIORITY_BELOW_NORMAL,CREATE_SUSPENDED);
def->ResumeThread();
}
and "ClientHandler" is as under
UINT ClientHandler(LPVOID pParam)
{
((CClientSocket*)pParam)->init();

CString test = " hello server here";
*(((CClientSocket*)pParam)->in)<<test;
((CClientSocket*)pParam)->in->Flush();      
      return 0;
}

while init() is as under

void CClientSocket::init()
{

pSocketFile = new CSocketFile(this);
in = new CArchive(pSocketFile,CArchive::store);
out= new CArchive(pSocketFile,CArchive::load);

}

now programme give an "Assert_Valid" when it goes inside the init on the line
in = ........
to me, it seems that that socket is appearing an unvalid one to it. But if I dont start the thread "ClientHandler" and try to send to the data to client from first thread "ServerHandler" every things goes ok.
what could be the reason ?
0
Comment
Question by:daknight2000
  • 2
3 Comments
 

Author Comment

by:daknight2000
ID: 2593942
I have also tried making "clientsocket" as a global variable  but it didnt help.
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 100 total points
ID: 2594627
You should only use CSockets etc in a CWinThread (UI thread) type thread as the CWinThread contains all the initialization required to manage CSockets.

so in your OnAccept of your listener socket you would (pseudo code)

CListenSocket::OnAccept(...)
{
CSocket tempSocket;

Accept(&tempSocket);

CSocketThread*pThread=(CSocketThread*)AfxBeginThread(RUNTIME_CLASS(CSocketThread),...,CREATE_SUSPENDED);

pThread->tempHandle=tempSocket.Detach();

pThread->ResumeThread();
}

then you would attach your socket to your thread in the initinstance
class CSocketThread:public CWinThread
{
SOCKET tempHandle;
CClientSocket m_socket; // derived from CSocket

}

CSocketThread::InitInstance()
{
m_socket.Attach(tempHandle);
m_socket.Init();
}

0
 

Author Comment

by:daknight2000
ID: 2607522
thanx..
sorry i was busy ...so i got late
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 84
sumDigits challenge 9 150
Thin secure Windows 10 5 98
"Black Box" Testing of Control System Software 2 51
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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