Hi
i am new with working with sockets
so i am trying to understand how it works ...
i am trying to compose the basic program working with socket
the simple as it can be for Cllent/server.
first i am tring to build the server. for client i will use telnet on port 23
i have written some code that create socket and handel the connection comming from telnet.
but i dont understand why i can't get data stream
the connection seem to be lost every time i try to send a stream only one byte arrives and the connection with the telnet is closed and i need to open it again
can some one please explain to me how can i fix this
so i will get the stream to my program?
this is my code:
// SocketListen.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "SocketListen.h"
#include "MyAsyncSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
//accepts connections from client.
void connectionHandler(CSocket * sock )
{
// construct a new, empty socket
CSocket sockRecv;
// accept connection
sock->Accept( sockRecv );
DWORD x =GetLastError ();
if (x==0)
{
char* chArr = new char[255];
for(int i=0;i<255;i++) chArr=NULL;
int rv = sockRecv.Receive(chArr,255);
cout<<chArr;
}
}
int SocketInitialize()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return err;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return err;
}
return err;
/* The WinSock DLL is acceptable. Proceed. */
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit
:GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed"
<< endl;
nRetCode = 1;
}
else
{
SocketInitialize();
//declare object of type socket
CSocket sock;
//create the socket
sock.Create( 23, SOCK_STREAM) ;
//start listening
sock.Listen(5);
//call function to deal with connetions.
connectionHandler(&sock);
connectionHandler(&sock);
WSACleanup( );
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
}
return nRetCode;
}
i am new with working with sockets
so i am trying to understand how it works ...
i am trying to compose the basic program working with socket
the simple as it can be for Cllent/server.
first i am tring to build the server. for client i will use telnet on port 23
i have written some code that create socket and handel the connection comming from telnet.
but i dont understand why i can't get data stream
the connection seem to be lost every time i try to send a stream only one byte arrives and the connection with the telnet is closed and i need to open it again
can some one please explain to me how can i fix this
so i will get the stream to my program?
this is my code:
// SocketListen.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "SocketListen.h"
#include "MyAsyncSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
//accepts connections from client.
void connectionHandler(CSocket * sock )
{
// construct a new, empty socket
CSocket sockRecv;
// accept connection
sock->Accept( sockRecv );
DWORD x =GetLastError ();
if (x==0)
{
char* chArr = new char[255];
for(int i=0;i<255;i++) chArr=NULL;
int rv = sockRecv.Receive(chArr,255);
cout<<chArr;
}
}
int SocketInitialize()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return err;
}
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we */
/* requested. */
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return err;
}
return err;
/* The WinSock DLL is acceptable. Proceed. */
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed"
nRetCode = 1;
}
else
{
SocketInitialize();
//declare object of type socket
CSocket sock;
//create the socket
sock.Create( 23, SOCK_STREAM) ;
//start listening
sock.Listen(5);
//call function to deal with connetions.
connectionHandler(&sock);
connectionHandler(&sock);
WSACleanup( );
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
}
return nRetCode;
}