📄 telnetdlg.cpp
字号:
if (stRmtName.sin_addr.s_addr != INADDR_NONE)
{
/* Get destination port number */
lpServent = getservbyname("telnet", 0);
stRmtName.sin_port = lpServent->s_port;
/* Initiate non-blocking connect to server */
stRmtName.sin_family = PF_INET;
nRet = connect(hSock,(LPSOCKADDR)&stRmtName,SOCKADDR_LEN);
if (nRet == SOCKET_ERROR)
{
int WSAErr = WSAGetLastError();
if (WSAErr != WSAEINTR)
{
/* Display all errors except "operation interrupted"*/
closesocket(hSock);
hSock = INVALID_SOCKET;
}
if(WSAErr == WSAETIMEDOUT)
{
AfxMessageBox("Time out.");
}
}
}
else
{
/* Can't resolve destination address */
closesocket(hSock);
hSock = INVALID_SOCKET;
}
}
return (hSock);
}
/**********************************************************************************/
// End ConnectTCP()
/**********************************************************************************/
/**********************************************************************************/
// Function: CloseTCP()
// Description: Do a graceful close of a TCP connection using the robust algorithm of
// "half-closing" with shutdown(how=1), then loop on recv() to read remaining data
// until it fails, or returns a zero, then call closesocket().
/**********************************************************************************/
int CTelnetDlg::CloseTCP(SOCKET hSock, LPSTR lpInBuf, int len)
{
int nRet = SOCKET_ERROR, cbBytesDone=0;
/* Half-close the connection to close neatly (we ignore the error here
* since some WinSocks fail with WSAEINVAL if they've recieved a RESET
* on the socket before the call to shutdown(). */
nRet = shutdown (hSock, 1);
/* Read and discard remaining data (until EOF or any error) */
nRet = 1;
while (nRet && (nRet != SOCKET_ERROR))
{
nRet = recv (hSock, lpInBuf, len-cbBytesDone, 0);
if (nRet > 0)
cbBytesDone += nRet;
}
/* close the socket, and ignore any error (since we can't do much
* about them anyway */
nRet = closesocket (hSock);
return (nRet);
}
/**********************************************************************************/
// End CloseTCP()
/**********************************************************************************/
/**********************************************************************************/
// Function: RecvData()
// Description: Recieve data amount requested into buffer passed
/**********************************************************************************/
int CTelnetDlg::RecvData(SOCKET hSock, LPSTR lpInBuf, int cbTotalToRecv, int nTimeout)
{
int cbTotalRcvd = 0, cbRcvd;
int nRet = SOCKET_ERROR; /* assume error */
DWORD StartTick = 0;
/* Set a timer, if requested */
if (!nTimeout)
{
nTimeout = 2000;
}
StartTick = GetTickCount();
while (((cbTotalToRecv - cbTotalRcvd) > 0) && (hSock != INVALID_SOCKET))
{
if(GetTickCount() > StartTick + nTimeout)
break;
cbRcvd = DoRecv(hSock, lpInBuf+cbTotalRcvd, cbTotalToRecv - cbTotalRcvd, nTimeout);
if (cbRcvd != SOCKET_ERROR)
{
/* Tally and Quit if we've received amount requested */
cbTotalRcvd += cbRcvd;
if ((cbTotalToRecv <= cbTotalRcvd))
break;
}
else
{
cbTotalRcvd = SOCKET_ERROR; // If receive failed, return an error
}
}
return (cbTotalRcvd);
}
/**********************************************************************************/
// End RecvData()
/**********************************************************************************/
/**********************************************************************************/
// Function: DoRecv()
// Description: Receive data into buffer. We call this function
// in response to FD_READ asynchronous notification.
/**********************************************************************************/
int CTelnetDlg::DoRecv(SOCKET hSock, LPSTR lpInBuf, int cbTotalToRecv, int nTimeout)
{
int cbTotalRcvd = 0;
int cbLeftToRecv = cbTotalToRecv;
int nRet=0, WSAErr;
DWORD StartTick = 0;
/* Read as much as we can buffer from client */
StartTick = GetTickCount();
while (cbLeftToRecv > 0)
{
if(GetTickCount() > StartTick + nTimeout)
break;
nRet = recv(hSock,lpInBuf+cbTotalRcvd, cbLeftToRecv, MSG_PARTIAL);
if (nRet == SOCKET_ERROR)
{
WSAErr = WSAGetLastError();
/* Display all errors except "operation interrupted" */
if (WSAErr == WSAEINTR)
AfxMessageBox("Operation interrupted.");
break;
}
else
{
if (nRet == 0)
{ /* Other side closed socket */
/* quit if server closed connection */
break;
}
else
{
/* Update byte counter */
cbTotalRcvd += nRet;
}
cbLeftToRecv = cbTotalToRecv - cbTotalRcvd;
if(cbTotalToRecv > cbTotalRcvd)
break;
}
}
return (cbTotalRcvd);
} /* end DoRecv() */
/**********************************************************************************/
// Function: SendData()
// Description: Send data amount requested from buffer passed.
/**********************************************************************************/
int CTelnetDlg::SendData(SOCKET hSock, LPSTR lpOutBuf, int cbTotalToSend)
{
int cbTotalSent = 0, cbSent;
int nRet = SOCKET_ERROR; /* assume error */
while (((cbTotalToSend - cbTotalSent) > 0) && (hSock != INVALID_SOCKET))
{
cbSent = DoSend(hSock, lpOutBuf+cbTotalSent, cbTotalToSend - cbTotalSent);
if (cbSent != SOCKET_ERROR)
{
/* Tally and Quit the loop if we've sent amount requested */
cbTotalSent += cbSent;
if ((cbTotalToSend - cbTotalSent) <= 0)
break;
}
else
{
/* If send failed, return an error */
cbTotalSent = SOCKET_ERROR;
}
}
return cbTotalSent;
}
/**********************************************************************************/
// End SendData()
/**********************************************************************************/
/**********************************************************************************/
// Function: DoSend()
// Description: Send data. We call this function from SendData(),
// or in response to FD_WRITE asynchronous notification.
/**********************************************************************************/
int CTelnetDlg::DoSend(SOCKET hSock, LPSTR lpOutBuf, int cbTotalToSend)
{
int cbTotalSent = 0;
int cbLeftToSend = cbTotalToSend;
int nRet, WSAErr;
/* Send as much data as we can */
while (cbLeftToSend > 0)
{
/* Send data to client */
nRet = send (hSock, lpOutBuf+cbTotalSent,
cbLeftToSend < MTU_SIZE ? cbLeftToSend : MTU_SIZE, 0);
if (nRet == SOCKET_ERROR)
{
WSAErr = WSAGetLastError();
/* Display all errors except "operation interrupted" */
if (WSAErr != WSAEINTR)
{
AfxMessageBox("Error in DoSend().");
}
break;
}
else
{
/* Update byte counter, and display. */
cbTotalSent += nRet;
}
/* calculate what's left to send */
cbLeftToSend = cbTotalSent - cbTotalToSend;
}
return cbTotalSent;
}
/**********************************************************************************/
// End DoSend()
/**********************************************************************************/
/**********************************************************************************/
// Description: Given a string, it will return an IP address.
// First it tries to convert the string directly.
// If that fails, it tries to resolve it as a hostname
//
// IN : szHost - Pointer to the host name string
// RETURNS : lAddr -- IP Address of the host
// WARNING: gethostbyname() is a blocking function
/**********************************************************************************/
u_long CTelnetDlg::GetAddr(LPSTR szHost)
{
LPHOSTENT lpstHost;
u_long lAddr = INADDR_ANY;
/* check that we have a string */
if (*szHost)
{
/* check for a dotted-IP address string */
lAddr = inet_addr (szHost);
/* If not an address, then try to resolve it as a hostname */
if ((lAddr == INADDR_NONE) && (strcmp (szHost, "255.255.255.255")))
{
lpstHost = gethostbyname(szHost);
if (lpstHost)
{ /* success */
lAddr = *((u_long FAR *) (lpstHost->h_addr));
}
else
{ /* failure */
lAddr = INADDR_ANY;
}
}
}
return (lAddr);
}
/**********************************************************************************/
// End GetAddr()
/**********************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -