Created
April 26, 2012 13:13
-
-
Save gabonator/2499460 to your computer and use it in GitHub Desktop.
Socket connector - double server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// spipe.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
//#include <signal.h> | |
BOOL m_bRunning = FALSE; | |
class CNetwork { | |
public: | |
CNetwork() | |
{ | |
WSADATA wi; | |
WSAStartup(0x0101,&wi); | |
} | |
} Network; | |
class CConsole { | |
public: | |
HANDLE m_hConsole; | |
CConsole() | |
{ | |
m_hConsole = GetStdHandle( STD_OUTPUT_HANDLE ); | |
} | |
void operator ()(int nColor) | |
{ | |
SetConsoleTextAttribute( m_hConsole, nColor ); | |
} | |
} Console; | |
class CStream { | |
public: | |
BYTE *m_pBuffer; | |
int m_nSize; | |
CStream() : m_pBuffer(NULL), m_nSize(0) {} | |
void operator ()( BYTE* pBuffer, int nSize ) | |
{ | |
m_pBuffer = pBuffer; | |
m_nSize = nSize; | |
} | |
}; | |
class CLog | |
{ | |
FILE *f; | |
public: | |
CLog() | |
{ | |
f = fopen("spipe.log", "w"); | |
} | |
~CLog() | |
{ | |
fclose(f); | |
} | |
CLog& operator << ( CStream& stream ) | |
{ | |
if ( stream.m_nSize > 0 ) | |
{ | |
fwrite( stream.m_pBuffer, stream.m_nSize, 1, f ); | |
fflush( f ); | |
} | |
return *this; | |
} | |
}; | |
class CServer | |
{ | |
enum { | |
BufferLength = 4096 | |
}; | |
int m_nPort; | |
BOOL m_bValid; | |
SOCKET m_socket; | |
SOCKET m_hTerminalSocket; | |
BYTE m_pBuffer[BufferLength]; | |
public: | |
CServer( int nPort ) | |
{ | |
m_nPort = nPort; | |
m_bValid = FALSE; | |
m_hTerminalSocket = NULL; | |
m_socket = socket(AF_INET, SOCK_STREAM, 0); | |
/*char hostname[64] = {0}; // = "ppp_peer"; | |
gethostname(hostname,64); | |
hostent* hent = gethostbyname(hostname);*/ | |
sockaddr_in myaddr; | |
myaddr.sin_family = AF_INET; | |
myaddr.sin_port = htons(m_nPort); | |
myaddr.sin_addr.s_addr = INADDR_ANY; //*(DWORD*)hent->h_addr_list[0]; | |
if ( bind( m_socket, (sockaddr*)&myaddr, sizeof(sockaddr)) ) | |
{ | |
Console( 0x1C ); | |
_tprintf( _T("CServer::CServer() bind error %d\n"), WSAGetLastError() ); | |
} | |
} | |
~CServer() | |
{ | |
closesocket( m_socket ); | |
} | |
BOOL Connect() | |
{ | |
// fd_set readfds; | |
timeval tv = { 0 }; | |
tv.tv_sec = 5; | |
Console( 0x1A ); | |
_tprintf( _T("CServer::Connect() Connecting (localhost:%d)... "), m_nPort); | |
u_long nNonBlocking = 1; | |
ioctlsocket(m_socket, FIONBIO, &nNonBlocking); | |
if ( listen( m_socket, 1 ) == SOCKET_ERROR ) | |
{ | |
Console( 0x1C ); | |
_tprintf(_T("Error listening on socket.\n")); | |
WSAError(); | |
return FALSE; | |
} | |
SOCKET hNew = accept(m_socket, NULL, NULL); | |
if ( WSAGetLastError() == WSAEWOULDBLOCK ) | |
{ | |
Console( 0x1C ); | |
_tprintf( _T("Failed.\n")); | |
// Neni error, iba cakame na pripojenie | |
//WSAError(); | |
return FALSE; | |
} | |
m_hTerminalSocket = hNew; | |
// u_long nNonBlocking = 1; | |
//ioctlsocket(m_hTerminalSocket, FIONBIO, &nNonBlocking); | |
_tprintf( _T("Ok.\n")); | |
m_bValid = TRUE; | |
return TRUE; | |
} | |
CServer& operator >> ( CStream& stream ) | |
{ | |
if ( !m_hTerminalSocket ) | |
{ | |
stream( NULL, 0 ); | |
return *this; | |
} | |
int nRecv = recv( m_hTerminalSocket, (CHAR*)m_pBuffer, BufferLength, 0 ); | |
if ( nRecv == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK ) | |
{ | |
m_bValid = FALSE; | |
nRecv = 0; | |
} | |
stream( m_pBuffer, nRecv ); | |
return *this; | |
} | |
CServer& operator << ( CStream& stream ) | |
{ | |
if ( !m_hTerminalSocket ) | |
return *this; | |
if ( stream.m_nSize > 0 ) | |
{ | |
int nSent = send( m_hTerminalSocket, (CHAR*)stream.m_pBuffer, stream.m_nSize, 0 ); | |
if ( nSent == SOCKET_ERROR ) | |
m_bValid = FALSE; | |
} | |
return *this; | |
} | |
BOOL operator() () | |
{ | |
return m_bValid; | |
} | |
static void WSAError() | |
{ | |
_tprintf( _T("WSAGetLastError() = %d\n"), WSAGetLastError() ); | |
} | |
}; | |
BOOL CtrlHandler( DWORD fdwCtrlType ) | |
{ | |
switch( fdwCtrlType ) | |
{ | |
case CTRL_C_EVENT: | |
Console( 0x1A ); | |
_tprintf(_T("Disconnecting...\n")); | |
m_bRunning = FALSE; | |
return TRUE; | |
} | |
return FALSE; | |
} | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
CServer s1( 1125 ); | |
CServer s2( 1124 ); | |
CLog log; | |
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE ); | |
m_bRunning = TRUE; | |
while ( m_bRunning ) | |
{ | |
CStream str; | |
if ( !s1() ) | |
if ( !s1.Connect() ) | |
Sleep(2000); | |
if ( !s2() ) | |
if ( !s2.Connect() ) | |
Sleep(2000); | |
s1 >> str; | |
s2 << str; | |
log << str; | |
if (str.m_nSize > 0) | |
{ | |
Console( 0x07 ); | |
fwrite( str.m_pBuffer, str.m_nSize, 1, stdout ); | |
} | |
s2 >> str; | |
s1 << str; | |
log << str; | |
if (str.m_nSize > 0) | |
{ | |
Console( 0x08 ); | |
fwrite( str.m_pBuffer, str.m_nSize, 1, stdout ); | |
} | |
Sleep(10); | |
} | |
Console( 0x07 ); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment