Click here to Skip to main content
15,913,773 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Embedding an application in an application Pin
TPN22-Jan-07 19:15
TPN22-Jan-07 19:15 
QuestionFinding the Connection speed ........ Pin
junkMind22-Jan-07 17:11
junkMind22-Jan-07 17:11 
AnswerRe: Finding the Connection speed ........ Pin
ThatsAlok22-Jan-07 18:43
ThatsAlok22-Jan-07 18:43 
AnswerRe: Finding the Connection speed ........ Pin
ThatsAlok22-Jan-07 18:44
ThatsAlok22-Jan-07 18:44 
AnswerRe: Finding the Connection speed ........ Pin
Anilkumar K V23-Jan-07 1:03
Anilkumar K V23-Jan-07 1:03 
AnswerRe: Finding the Connection speed ........ Pin
rp_suman24-Jan-07 17:47
rp_suman24-Jan-07 17:47 
QuestionIs it possible to custom_draw the chevron button on the CRebar? Pin
benben22-Jan-07 16:38
benben22-Jan-07 16:38 
QuestionNonBlocking Client Socket - Connection Status Pin
ScotDolan22-Jan-07 10:57
ScotDolan22-Jan-07 10:57 
I am trying to develop a C++ socket object. This object will be initialized as client or server. After the client is initialized with server ip address and port number I want to create a thread and have the thread maintain the socket connection and report the sockets client state.

I am having difficult try to find out if the client socket status. Whether the client socket is connected after i call connect() and whether the client socket has been disconnected after a connection has been made. What is the best way to check the status of the client socket.



*********************************************************************************************
Function Name:
Purpose:
**********************************************************************************************/
void SocketLib5::RunClient(void)
{
int eResults;
int state = INIT_CLIENT;

//int iOptVal;
int iOptLen = sizeof(int);

bool bOptVal;
int bOptLen = sizeof(bool);

int iMode;

hostent* localHost;
char* localIP;


/*do something*/
while( isRunning )
{

// Checks to see in system in initialized
switch( state )
{

case INIT_CLIENT:
// Starts up the socket interfaces, library, and dlls
eResults = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( eResults != NO_ERROR ){
WSACleanup();
break;
}

server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr( server_ip );
server_address.sin_port = server_port;

localHost = gethostbyname("");// Get the local host information
localIP = inet_ntoa (*(struct in_addr *)*localHost->h_addr_list);
client_address.sin_family = AF_INET;
client_ip = localIP;
state = CREATE_CLIENT;
break;

case CREATE_CLIENT: // Creates a socket
client_socket = socket(client_address.sin_family, SOCK_STREAM, PPROTO_TCP );
if ( client_socket == INVALID_SOCKET ){
closesocket(client_socket);
state = INIT_CLIENT;
break;
}

// Disable Blocking Mode function calls
//iMode = 0;
//ioctlsocket( client_socket, FIONBIO, (u_long FAR*) &iMode);

state = INIT_EVENT_CLIENT;
break;

case INIT_EVENT_CLIENT: // Configures Status Events
bOptVal = true;
//bOptLen = sizeof(bool);
// eResults = setsockopt(client_socket, SOL_SOCKET, SO_KEEPALIVE, (char*)&bOptVal, bOptLen);

// Create new event
g_hClientEvent = WSACreateEvent();
if (WSA_INVALID_EVENT == g_hClientEvent)
{
closesocket( client_socket );
state = INIT_CLIENT;
break;
}

eResults = WSAEventSelect(client_socket, g_hClientEvent, FD_READ | FD_WRITE | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
if ( eResults == SOCKET_ERROR)
{
WSACloseEvent(g_hClientEvent);
closesocket(client_socket);
state = INIT_CLIENT;
break;
}
state = CONNECT_CLIENT;
break;

case CONNECT_CLIENT: // Accepts Server Socket
eResults = connect( client_socket, (SOCKADDR*) &server_address, sizeof(server_address) );
if( eResults == SOCKET_ERROR ){
state = INIT_CLIENT;

eResults = WSAGetLastError();
switch( eResults )
{
case WSAEWOULDBLOCK:
state = LOOP_CLIENT;
break;

case WSAEISCONN:
state = LOOP_CLIENT;
break;

default:
state = INIT_CLIENT;
cout << " connect error " << eResults <<endl;
closesocket(client_socket);
="" break;
="" }

="" }else{
="" state="LOOP_CLIENT;
" }
="" break;



="" case="" loop_client:=""

=""
="" eresults="WSAGetLastError();
" switch(="" )
="" {
="" wsaewouldblock:
="" wsaeisconn:
="" break;

="" default:
="" cout="" <<="" "="" connect="" error="" <<endl;
="" wsaenumnetworkevents(client_socket,="" g_hclientevent,="" &networkclientevents);
="" switch="" (="" networkclientevents.lnetworkevents="" fd_write:
="" printf("write="" message:="" \n\r");
="" fd_read:="" printf("client="" read="" fd_accept:
="" printf("accept="" fd_connect:
="" connected="" fd_close:="" connection="" lost:="" }="" switch
="" isrunning="" while="" loop
="" wsacloseevent(g_hclientevent);
="" closesocket(client_socket);="" wsacleanup();
="" return;
}
=""

<div="" class="ForumSig">Scott Dolan
Jernie Corporation
Engineering & Manufacturing
Software, Hardware, & Enclosures
AnswerRe: NonBlocking Client Socket - Connection Status Pin
Mark Salsbery22-Jan-07 11:06
Mark Salsbery22-Jan-07 11:06 
GeneralRe: NonBlocking Client Socket - Connection Status Pin
ScotDolan22-Jan-07 11:21
ScotDolan22-Jan-07 11:21 
GeneralRe: NonBlocking Client Socket - Connection Status Pin
Mark Salsbery22-Jan-07 11:41
Mark Salsbery22-Jan-07 11:41 
GeneralRe: NonBlocking Client Socket - Connection Status Pin
ScotDolan22-Jan-07 12:04
ScotDolan22-Jan-07 12:04 
GeneralRe: NonBlocking Client Socket - Connection Status [modified] Pin
Mark Salsbery22-Jan-07 12:11
Mark Salsbery22-Jan-07 12:11 
GeneralRe: NonBlocking Client Socket - Connection Status Pin
Mark Salsbery22-Jan-07 12:15
Mark Salsbery22-Jan-07 12:15 
GeneralRe: NonBlocking Client Socket - Connection Status Pin
ScotDolan23-Jan-07 4:21
ScotDolan23-Jan-07 4:21 
GeneralRe: NonBlocking Client Socket - Connection Status Pin
Mark Salsbery23-Jan-07 8:16
Mark Salsbery23-Jan-07 8:16 
GeneralRe: NonBlocking Client Socket - Connection Status Pin
Mark Salsbery22-Jan-07 13:25
Mark Salsbery22-Jan-07 13:25 
GeneralRe: NonBlocking Client Socket - Connection Status Pin
Mike O'Neill22-Jan-07 13:20
Mike O'Neill22-Jan-07 13:20 
GeneralRe: NonBlocking Client Socket - Connection Status Pin
Mark Salsbery22-Jan-07 13:23
Mark Salsbery22-Jan-07 13:23 
GeneralRe: NonBlocking Client Socket - Connection Status Pin
Mike O'Neill22-Jan-07 15:12
Mike O'Neill22-Jan-07 15:12 
GeneralRe: NonBlocking Client Socket - Connection Status Pin
Mark Salsbery23-Jan-07 8:18
Mark Salsbery23-Jan-07 8:18 
QuestionUsing Adobe Acrobat to open a .PDF Pin
TheDelChop22-Jan-07 10:32
TheDelChop22-Jan-07 10:32 
AnswerRe: Using Adobe Acrobat to open a .PDF Pin
CPallini22-Jan-07 10:38
mveCPallini22-Jan-07 10:38 
AnswerRe: Using Adobe Acrobat to open a .PDF Pin
ThatsAlok22-Jan-07 18:49
ThatsAlok22-Jan-07 18:49 
QuestionCreating a Bitmap output without screen capture. Pin
u6ik22-Jan-07 9:24
u6ik22-Jan-07 9:24 

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.