Click here to Skip to main content
15,887,596 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to build 64bit SDI with MFC? Pin
hztj20056-Jul-10 8:29
hztj20056-Jul-10 8:29 
QuestionRe: How to build 64bit SDI with MFC? Pin
David Crow6-Jul-10 9:43
David Crow6-Jul-10 9:43 
AnswerRe: How to build 64bit SDI with MFC? Pin
hztj20057-Jul-10 3:33
hztj20057-Jul-10 3:33 
QuestionWinsock problem Pin
masnu6-Jul-10 7:01
masnu6-Jul-10 7:01 
AnswerRe: Winsock problem Pin
Moak6-Jul-10 11:31
Moak6-Jul-10 11:31 
GeneralRe: Winsock problem Pin
masnu7-Jul-10 3:26
masnu7-Jul-10 3:26 
GeneralRe: Winsock problem [modified] Pin
Moak7-Jul-10 4:32
Moak7-Jul-10 4:32 
GeneralRe: Winsock problem Pin
masnu7-Jul-10 4:56
masnu7-Jul-10 4:56 
Sorry Moak, I was cutting and pasting and forgot the most important part. Here's the actual code to open and connect:

int CHPCtrl::Connect()
{


	struct sockaddr_in	rmtAddr;
	
//	Create data socket
	m_Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	if ( m_Socket == INVALID_SOCKET )
		return 1;

//	Set client properties
	rmtAddr.sin_family	= AF_INET;
	rmtAddr.sin_addr.s_addr = inet_addr( remoteIP );
	rmtAddr.sin_port	= htons( m_nDevicePort );

//	Connect to remote
	if ( connect( m_Socket, (struct sockaddr *)&rmtAddr, sizeof(rmtAddr) ) == SOCKET_ERROR )
		return 1;


//	Open rx monitor thread
	m_hRxMonitor = (HANDLE)_beginthreadex( 0, 0, RxMonitor, this, CREATE_SUSPENDED, 0 );

	if ( m_hRxMonitor )
	{
		
		m_bConnected = true;
		ResumeThread( m_hRxMonitor );

	} //__if ( m_pReadThread )__

	return 0;

}


And in a separate thread I wait for incoming data:

UINT CHPCtrl::RxMonitor(void *pThis)
{


	CHPCtrl		*pCtrl = (CHPCtrl*)pThis;
	int		bytes_recevied = 0;
	
	fd_set		sckt;
	timeval		timeout;

//	Set two second timeout
	timeout.tv_sec	= 2;
	timeout.tv_usec = 0;

//	Monitor all rx traffic
	while ( pCtrl->m_bConnected )
	{

		FD_ZERO( &sckt );
		FD_SET( pCtrl->m_Socket, &sckt );

		int ret = select( 0, &sckt, 0, 0, &timeout );

		switch ( ret )
		{

			case  SOCKET_ERROR:
				//Handle error 
				break;

			case 0:	//Timeout
				//Handle time out
				break;

			default:
				pCtrl->ReadFromSocket( pCtrl->m_Socket );

		} //__switch ( ret )__

	} //__while ( m_bRunRx )__

//	Shutdown rx comm
	shutdown( pCtrl->m_Socket, SD_RECEIVE );

	return 0;

}


m_Socket is a class variable. I did this so I can send and receive on the same socket without blocking. Please let me know if you see anything wrong with this.
GeneralRe: Winsock problem Pin
Moak7-Jul-10 5:05
Moak7-Jul-10 5:05 
AnswerRe: Winsock problem Pin
Parthiban6-Jul-10 18:16
Parthiban6-Jul-10 18:16 
GeneralRe: Winsock problem Pin
masnu7-Jul-10 3:30
masnu7-Jul-10 3:30 
GeneralRe: Winsock problem Pin
Emilio Garavaglia7-Jul-10 4:10
Emilio Garavaglia7-Jul-10 4:10 
GeneralRe: Winsock problem Pin
masnu7-Jul-10 4:24
masnu7-Jul-10 4:24 
GeneralRe: Winsock problem Pin
Iain Clarke, Warrior Programmer7-Jul-10 5:33
Iain Clarke, Warrior Programmer7-Jul-10 5:33 
AnswerRe: Winsock problem solved Pin
masnu7-Jul-10 8:20
masnu7-Jul-10 8:20 
GeneralRe: Winsock problem solved Pin
Moak7-Jul-10 8:22
Moak7-Jul-10 8:22 
GeneralRe: Winsock problem solved Pin
masnu7-Jul-10 8:25
masnu7-Jul-10 8:25 
GeneralRe: Winsock problem solved Pin
jeron17-Jul-10 10:18
jeron17-Jul-10 10:18 
GeneralRe: Winsock problem solved Pin
masnu7-Jul-10 10:22
masnu7-Jul-10 10:22 
GeneralRe: Winsock problem solved Pin
jeron17-Jul-10 10:41
jeron17-Jul-10 10:41 
GeneralRe: Winsock problem solved Pin
masnu7-Jul-10 10:46
masnu7-Jul-10 10:46 
GeneralRe: Winsock problem solved Pin
Moak7-Jul-10 11:15
Moak7-Jul-10 11:15 
GeneralRe: Winsock problem solved Pin
Moak7-Jul-10 11:56
Moak7-Jul-10 11:56 
GeneralRe: Winsock problem solved Pin
masnu8-Jul-10 2:59
masnu8-Jul-10 2:59 
QuestionDetect Windows Search feature Pin
Ivo Beltchev6-Jul-10 6:44
Ivo Beltchev6-Jul-10 6:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.