Important alert: (current site time 7/15/2013 5:34:49 PM EDT)
 

VB icon

A client server app with 2 processes, multithread, sneding data over network to eachother

Email
Submitted on: 5/12/2004 10:48:01 AM
By: Tako Griffith 
Level: Beginner
User Rating: By 1 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++
Views: 31467
 
     Nice sample: multithreading Visual C++ application. 2 threads: sender and receiver. The multi-threaded app sends from one process to another over the network.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: A client server app with 2 processes, multithread, sneding data over network to eachother
// Description:Nice sample: multithreading Visual C++ application. 2 threads: sender and receiver. The multi-threaded app sends from one process to another over the network.
// By: Tako Griffith
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7948&lngWId=3//for details.//**************************************

// ********************************************************************
// TCP/IP client/server communication sample
//Client/Server communication: 1 thread sends data, 1 thread receives data
//Requires freeware ASocket.dll to run the sample.
//Download it from http://www.vahland.com/asocket/asocket.dll
//and register it (REGSVR32 asocket.dll) on your machine.
//
// IMPORTANT: 
//Online manual: http://www.vahland.com/asocket/manual.htm
// ********************************************************************
#define _MT
#define _WIN32_DCOM
#include <comdef.h>
#include <atlbase.h>
#include <windows.h>
#include <process.h>
#include <stdio.h>
#include "..\include\ASocketConstants.h"
#include "..\include\ASocket_i.c"
#include "..\include\ASocket.h"
////////////////////////////////////////////////
#define	SAMPLETCPPORT	1500 
CRITICAL_SECTION		g_cs;
BOOL					g_bFinished = FALSE;
////////////////////////////////////////////////
void Print( LPSTR lpszMessage )
{
 EnterCriticalSection( &g_cs );
 printf( "%s\n", lpszMessage );
 LeaveCriticalSection( &g_cs );
}
////////////////////////////////////////////////
void SetFinished( BOOL bFinished )
{
 EnterCriticalSection( &g_cs );
 g_bFinished = bFinished;
 LeaveCriticalSection( &g_cs );
}
////////////////////////////////////////////////
BOOL IsFinished()
{
 BOOL	bIsFinished;
 EnterCriticalSection( &g_cs );
 bIsFinished = g_bFinished;
 LeaveCriticalSection( &g_cs );
 return bIsFinished;
}
////////////////////////////////////////////////
void ClientThread( void* pParams )
{
 HRESULThr;
 ISocket*pSocket = NULL;
 _bstr_tbstrHost = "127.0.0.1"; 
 _bstr_tbstrMesg[ 8 ];
 CHAR szMessage[ 1023 + 1 ];
 LONG lResult;
 inti;
 CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );	// _WIN32_DCOM must be defined
 hr = CoCreateInstance(CLSID_Socket, NULL, CLSCTX_INPROC_SERVER, IID_ISocket, (void**) &pSocket);
 if( ! SUCCEEDED( hr ) )
 {
SetFinished( TRUE );
goto _EndClientThread;
 }
 pSocket->put_Protocol( asPROTOCOL_RAW );
 pSocket->Connect( bstrHost, SAMPLETCPPORT );
 pSocket->get_LastError( &lResult );
 sprintf( szMessage, "CLIENT - Connect: result = %d", lResult );
 Print( szMessage );
 if( lResult != asERR_SUCCESS ) 
 {
SetFinished( TRUE );
goto _EndClientThread;
 }
 bstrMesg[ 0 ] = "This is just a message";
 bstrMesg[ 1 ] = "And another message";
 bstrMesg[ 2 ] = "Quit";
 bstrMesg[ 3 ] = "";
 for( i = 0; i < 3; i++ )
 {
sprintf( szMessage, "CLIENT - Send : %s", ( char * ) bstrMesg[ i ] );
Print( szMessage );
pSocket->SendString( bstrMesg[ i ], 0 );
pSocket->Sleep( 3000 );
 }
_EndClientThread:
 if( pSocket != NULL )
 {
pSocket->Disconnect();	// Can be called at any time, even if socket is not open
pSocket->Release();
pSocket = NULL;
 }
}
////////////////////////////////////////////////
void ServerThread( void* pParams )
{
 HRESULThr;
 ISocket*pSocket = NULL;
 _bstr_tbstrHost = "127,0,0,1", bstrMesg = "", bstrReceived = "";
 BSTR bstrTemp;
 CHAR szMessage[ 1023 + 1 ];
 LONG lConnectionState, lResult;
 BOOL bHasData;
 CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );	// _WIN32_DCOM must be defined
 hr = CoCreateInstance(CLSID_Socket, NULL, CLSCTX_INPROC_SERVER, IID_ISocket, (void**) &pSocket);
 if( ! SUCCEEDED( hr ) )
 {
Print( "CoCreateInstance failed" );
goto _EndServerThread;
 }
 pSocket->put_Protocol( asPROTOCOL_RAW );
 // Try to listen for a connection on port on this machine
 pSocket->StartListening( SAMPLETCPPORT );
 pSocket->get_LastError( &lResult );
 sprintf( szMessage, "SERVER - StartListening: result = %d", lResult ); 
 Print( szMessage );
 if( lResult != asERR_SUCCESS )
goto _EndServerThread;
 // Wait for a connection on port 1500 on this machine now...
 Print( "SERVER - Waiting for a connection..." );
 while( TRUE )
 {
 pSocket->get_ConnectionState( &lConnectionState );		
 if( lConnectionState != asCONN_LISTENING )
break;
 pSocket->Sleep( 1000 );
 }
 if( lConnectionState != asCONN_CONNECTED )
 {
Print( "SERVER - No Connection" );
goto _EndServerThread;
 }
 // YES, connection established.
 Print( "SERVER - Connection established" );
 while( TRUE )
 {
pSocket->get_ConnectionState( &lConnectionState );		
if( lConnectionState != asCONN_CONNECTED )
 break;
if( stricmp( ( char * ) bstrReceived, "Quit" ) == 0 )
 break;
pSocket->HasData( &bHasData );
if( bHasData )
{
 pSocket->ReceiveString( &bstrTemp );
 bstrReceived = bstrTemp;
 sprintf( szMessage, "SERVER - Received : %s", ( char * ) bstrReceived );
 Print( szMessage );
 pSocket->Sleep( 100 );
}
 }
_EndServerThread:
 if( pSocket != NULL )
 {
pSocket->Disconnect();	// Can be called at any time, even if socket is not open
Print( "SERVER - Socket closed" );
pSocket->Release();
pSocket = NULL;
 }
 SetFinished( TRUE );
}
////////////////////////////////////////////////
int main( void )
{ 
 InitializeCriticalSection( &g_cs );
 _beginthread( ServerThread, 0, NULL );
 Sleep( 3000 );
 _beginthread( ClientThread, 0, NULL );
 while( ! IsFinished() )
 {;
 }
 Print( "MAIN - READY!" );
 Sleep( 5000 );
 return 0;
}


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

5/13/2004 12:47:46 PMR. Pierce

I'm quite new to C. where do I find
ASocketConstants.h?
I'm using VC++ 6.0 from microsoft
(If this comment was disrespectful, please report it.)

 
12/2/2004 1:13:48 PM

go to google and type:
"index of" ASocketConstants.h
(If this comment was disrespectful, please report it.)

 
8/13/2006 1:11:26 AMdubs

How about a zip or rar or tar.gz not a page full of nasty code?
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.