初学vc望 recv函数返回值的有关问题
初学vc望高手指点 recv函数返回值的问题
该程序编译连接运行时无错,可是总得不到预期的结果。在其中ReadFTPServerReply中的recv函数总是返回-1。下面程序已经注明!!
#include <stdlib.h>
#include <winsock.h>
#define PROG_NAME "Simple FTP "
#define HOST_NAME "FTPSERVER.COM.CN "
#define PASSWORD "PASS guest\r\n "
#define WINSOCK_VERSION 0x0101
#define DEFAULT_PROTOCOL 0
#define NO_FLAGS 0
char szCommandBuffer[100];
LPSTR lpszFunctionName;
UINT GetReplyCode(LPSTR lpszServerReply) // 4
{
UINT nCode;
char c;
c = *(lpszServerReply+3);
*(lpszServerReply+3) = '\0 ';
nCode = atoi((const char *)lpszServerReply);
*(lpszServerReply+3) = c;
return(nCode);
}
UINT ReadFTPServerReply(SOCKET hControlChannel) //二 1 1 1
{
char sReceiveBuffer[1024];
int iLength;
lpszFunctionName = "ReadFTPServerReply "; //二 1 1 1
//此时recv()总是返回-1
if((iLength = recv(hControlChannel,(LPSTR)sReceiveBuffer,
sizeof(sReceiveBuffer),NO_FLAGS))==SOCKET_ERROR) {
int iWinsockErr = WSAGetLastError();
wsprintf(szCommandBuffer, "Error %d from the recv() function!! ",iWinsockErr);
MessageBeep(MB_ICONHAND);
MessageBox(NULL,szCommandBuffer,lpszFunctionName,MB_OK|MB_ICONSTOP);
return(999);
}
sReceiveBuffer[iLength]= '\0 ';
MessageBeep(MB_ICONASTERISK);
MessageBox(NULL,(LPSTR)sReceiveBuffer,lpszFunctionName,MB_OK|MB_ICONINFORMATION);
return(GetReplyCode(sReceiveBuffer));
}
UINT SendFTPCommand(SOCKET hControlChannel,LPSTR szCommandBuffer) //二 1 1
{
lpszFunctionName = "SendFTPCommand ";
if((send(hControlChannel,(LPSTR)szCommandBuffer,lstrlen(szCommandBuffer),NO_FLAGS))==SOCKET_ERROR)
{
int iWinsockErr = WSAGetLastError();
wsprintf(szCommandBuffer, "Error %d from the send() function!! ",iWinsockErr);
MessageBeep(MB_ICONHAND);
MessageBox(NULL,szCommandBuffer,lpszFunctionName,MB_OK|MB_ICONSTOP);
return(999);
}
return(ReadFTPServerReply(hControlChannel)); //二 1 1 1
}
UINT AnonymousFTPLogIn(SOCKET hControlSocket) //二 1
{
int nReplyCode;
int iMsg = 0;
lpszFunctionName = "AnonymousFTPLogIn "; //二 1
char *LoginCommand[] = { "USER anonymous\r\n ",PASSWORD,NULL};
do
{
nReplyCode = SendFTPCommand(hControlSocket,(LPSTR)LoginCommand[iMsg++]); //二 1 1
}
while(LoginCommand[iMsg]&&nReplyCode <400);
return(nReplyCode);
}
SOCKET ConnectFTPControlSocket(LPSTR lpszHost) // 一 1
{
LPHOSTENT lpHostEnt;
SOCKADDR_IN sockAddr;
LPSERVENT lpServEnt;
short nProtocolPort;
该程序编译连接运行时无错,可是总得不到预期的结果。在其中ReadFTPServerReply中的recv函数总是返回-1。下面程序已经注明!!
#include <stdlib.h>
#include <winsock.h>
#define PROG_NAME "Simple FTP "
#define HOST_NAME "FTPSERVER.COM.CN "
#define PASSWORD "PASS guest\r\n "
#define WINSOCK_VERSION 0x0101
#define DEFAULT_PROTOCOL 0
#define NO_FLAGS 0
char szCommandBuffer[100];
LPSTR lpszFunctionName;
UINT GetReplyCode(LPSTR lpszServerReply) // 4
{
UINT nCode;
char c;
c = *(lpszServerReply+3);
*(lpszServerReply+3) = '\0 ';
nCode = atoi((const char *)lpszServerReply);
*(lpszServerReply+3) = c;
return(nCode);
}
UINT ReadFTPServerReply(SOCKET hControlChannel) //二 1 1 1
{
char sReceiveBuffer[1024];
int iLength;
lpszFunctionName = "ReadFTPServerReply "; //二 1 1 1
//此时recv()总是返回-1
if((iLength = recv(hControlChannel,(LPSTR)sReceiveBuffer,
sizeof(sReceiveBuffer),NO_FLAGS))==SOCKET_ERROR) {
int iWinsockErr = WSAGetLastError();
wsprintf(szCommandBuffer, "Error %d from the recv() function!! ",iWinsockErr);
MessageBeep(MB_ICONHAND);
MessageBox(NULL,szCommandBuffer,lpszFunctionName,MB_OK|MB_ICONSTOP);
return(999);
}
sReceiveBuffer[iLength]= '\0 ';
MessageBeep(MB_ICONASTERISK);
MessageBox(NULL,(LPSTR)sReceiveBuffer,lpszFunctionName,MB_OK|MB_ICONINFORMATION);
return(GetReplyCode(sReceiveBuffer));
}
UINT SendFTPCommand(SOCKET hControlChannel,LPSTR szCommandBuffer) //二 1 1
{
lpszFunctionName = "SendFTPCommand ";
if((send(hControlChannel,(LPSTR)szCommandBuffer,lstrlen(szCommandBuffer),NO_FLAGS))==SOCKET_ERROR)
{
int iWinsockErr = WSAGetLastError();
wsprintf(szCommandBuffer, "Error %d from the send() function!! ",iWinsockErr);
MessageBeep(MB_ICONHAND);
MessageBox(NULL,szCommandBuffer,lpszFunctionName,MB_OK|MB_ICONSTOP);
return(999);
}
return(ReadFTPServerReply(hControlChannel)); //二 1 1 1
}
UINT AnonymousFTPLogIn(SOCKET hControlSocket) //二 1
{
int nReplyCode;
int iMsg = 0;
lpszFunctionName = "AnonymousFTPLogIn "; //二 1
char *LoginCommand[] = { "USER anonymous\r\n ",PASSWORD,NULL};
do
{
nReplyCode = SendFTPCommand(hControlSocket,(LPSTR)LoginCommand[iMsg++]); //二 1 1
}
while(LoginCommand[iMsg]&&nReplyCode <400);
return(nReplyCode);
}
SOCKET ConnectFTPControlSocket(LPSTR lpszHost) // 一 1
{
LPHOSTENT lpHostEnt;
SOCKADDR_IN sockAddr;
LPSERVENT lpServEnt;
short nProtocolPort;