怎么做文件上传的服务器端(C++ Builder)?

Ywg78 2003-11-06 11:46:28
不借助NMFTP怎么写一个文件的服务器端?各位高手请指教
...全文
234 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ywg78 2003-11-07
  • 打赏
  • 举报
回复
兄弟!你看一下这代码是什么意思?我网络上下载的

#include <stdlib.h>
#include "..\winsock.h" //Winsock header file

#define PROG_NAME "Quick FTP Version 1"
#define HOST_NAME "www.ipvod.com" // FTP server host
#define PASSWORD "p12157mn" //Password for FTP server host
#define WINSOCK_VERSION 0x0101 // Program requires Winsock version 1.1
#define DEFAULT_PROTOCOL 0 // No protocol specified, use default
#define NO_FLAGS 0 // No special flags specified

char szCommandBuffer[100]; // Buffer for FTP commands
LPSTR lpszFunctionName; // Pointer for function names
UINT GetReplyCode(LPSTR lpszServerReply)
{
UINT nCode; // Reply code as a number
char c; // Temporary storage
c = *(lpszServerReply+3); // Save the character
*(lpszServerReply+3) = '\0'; // Terminate the code

nCode = atoi((const char *)lpszServerReply); // Convert code to number
*(lpszServerReply+3) = c; // Restore the character

return(nCode); // Return the reply code
}
UINT ReadFTPServerReply(SOCKET hControlChannel)
{
char sReceiveBuffer[1024]; // Data-storage buffer for FTP server reply
int iLength; // Length of data received from FTP server

lpszFunctionName = "ReadFTPServerReply ";

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 to indicate an error has occurred
return(999);
}

sReceiveBuffer[iLength] = '\0';
MessageBeep(MB_ICONASTERISK);
MessageBox(NULL, (LPSTR)sReceiveBuffer, lpszFunctionName,MB_OK |MB_ICONINFORMATION);
// Extract the reply code from the server reply and return as an integer
return(GetReplyCode(sReceiveBuffer));
}
UINT SendFTPCommand(SOCKET hControlChannel, LPSTR szCommandBuffer)
{
lpszFunctionName = "SendFTPCommand ";

// Send the FTP command
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 to indicate an error has occurred
return(999);
}

// Read the server's reply and return the reply code as an integer
return(ReadFTPServerReply(hControlChannel));
}
UINT AnonymousFTPLogIn(SOCKET hControlSocket)
{
int nReplyCode; // FTP server reply code
int iMsg = 0; // Index subscript for FTP commands

lpszFunctionName = "AnonymousFTPLogIn ";
char *LoginCommand[]={"USER anonymous\r\n ",PASSWORD,NULL};
do
{
nReplyCode = SendFTPCommand(hControlSocket,(LPSTR)LoginCommand[iMsg++]);
}
while(LoginCommand[iMsg] && nReplyCode < 400);
return(nReplyCode);
}
SOCKET ConnectFTPControlSocket(LPSTR lpszHost)
{
LPHOSTENT lpHostEnt; // Internet host information structure
SOCKADDR_IN sockAddr; // Socket address structure
LPSERVENT lpServEnt; // Service information structure
short nProtocolPort; // Protocol port
int nConnect; // Socket connection results
SOCKET hControlSocket; // Control socket handle

lpszFunctionName = "ConnectFTPControlSocket ";

if (!(lpHostEnt = gethostbyname(lpszHost)))
{
int iWinsockErr = WSAGetLastError();
wsprintf(szCommandBuffer,"Error #%d while resolving address for %s ",iWinsockErr, lpszHost);
MessageBeep(MB_ICONHAND);
MessageBox(NULL, szCommandBuffer, lpszFunctionName,MB_OK |MB_ICONSTOP);
return(INVALID_SOCKET);
}

if ((hControlSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))== INVALID_SOCKET)
{
int iWinsockErr = WSAGetLastError();
wsprintf(szCommandBuffer,"Error #%d occurred while creating socket!! ",iWinsockErr);
MessageBeep(MB_ICONHAND);
MessageBox(NULL, szCommandBuffer, lpszFunctionName,MB_OK |MB_ICONSTOP);
return(INVALID_SOCKET);
}

lpServEnt = getservbyname( "ftp ", DEFAULT_PROTOCOL);

if (lpServEnt == NULL)
nProtocolPort = htons(IPPORT_FTP);
else
nProtocolPort = lpServEnt->s_port;

// Define the socket address
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = nProtocolPort;
sockAddr.sin_addr = *((LPIN_ADDR)*lpHostEnt->h_addr_list);

// Connect the socket
if( nConnect = connect(hControlSocket, (LPSOCKADDR)&sockAddr,sizeof(sockAddr)))
{
int iWinsockErr = WSAGetLastError();
wsprintf(szCommandBuffer,"Error #%d occurred while connecting socket!! ",iWinsockErr);
MessageBeep(MB_ICONHAND);
MessageBox(NULL, szCommandBuffer, lpszFunctionName,MB_OK |MB_ICONSTOP);
return(INVALID_SOCKET);
}

if (ReadFTPServerReply(hControlSocket) >= 400)
return(INVALID_SOCKET);
else
return(hControlSocket);
}

int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
WSADATA wsaData; // Winsock implementation details
SOCKET hControlChannel; // Socket handle for the control channel
UINT nReplyCode; // FTP reply code

lpszFunctionName = "WinMain ";

if (WSAStartup(WINSOCK_VERSION, &wsaData))
{
MessageBeep(MB_ICONHAND);
MessageBox(NULL, "Could not load Windows Sockets DLL. ",lpszFunctionName, MB_OK |MB_ICONSTOP);
return(NULL);
}

hControlChannel = ConnectFTPControlSocket((LPSTR)HOST_NAME);
// Note that from a DLL, here we would return
// our control channel handle (if valid) for
// use by other program modules.

if (hControlChannel != INVALID_SOCKET)
{
// If we have a control channel, then login.
nReplyCode = AnonymousFTPLogIn(hControlChannel);

if (nReplyCode == 230) // User logged in; we can proceed.
{
SendFTPCommand(hControlChannel, "QUIT\r\n ");
closesocket(hControlChannel);
}
}

WSACleanup();
MessageBeep(MB_ICONEXCLAMATION);
MessageBox(NULL, "THE END!! ", PROG_NAME, MB_OK |MB_ICONEXCLAMATION);
return(NULL);
}
yesry 2003-11-07
  • 打赏
  • 举报
回复
才两个事件啊。我想你是没有看清楚用了什么控件,以及这些控件的事件。

TIdTrivialFTPServer *IdTrivialFTPServer1;//在IndyServer,事件ReadFile
TIdTrivialFTP *IdTrivialFTP1;//在IndyClient,连接(Host="127.0.0.1";Active=true;)后,用Get方法。

Ywg78 2003-11-06
  • 打赏
  • 举报
回复
兄弟!怎么没一点注解!小女子,愚昧,看不太懂?请写详细点行吧
yesry 2003-11-06
  • 打赏
  • 举报
回复
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMemo *Memo1;
TButton *Button1;
TIdTrivialFTPServer *IdTrivialFTPServer1;
TIdTrivialFTP *IdTrivialFTP1;
void __fastcall IdTrivialFTPServer1ReadFile(TObject *Sender,
AnsiString &FileName, const TPeerInfo &PeerInfo,
bool &GrantAccess, TStream *&AStream,
bool &FreeStreamOnComplete);
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};



void __fastcall TForm1::IdTrivialFTPServer1ReadFile(TObject *Sender,
AnsiString &FileName, const TPeerInfo &PeerInfo, bool &GrantAccess,
TStream *&AStream, bool &FreeStreamOnComplete)
{
AStream=new TMemoryStream;
AStream->Write("123456789",10);
AStream->Position=0;
FreeStreamOnComplete=true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
IdTrivialFTP1->Active=true;

::Sleep(10);
TMemoryStream *s=new TMemoryStream;
IdTrivialFTP1->Get("ggggggggggggggggggggggggggggggggggggggg1",s);

Memo1->Text=(char *)s->Memory;
}
//---------------------------------------------------------------------------


yesry 2003-11-06
  • 打赏
  • 举报
回复
Indy

1,317

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 网络及通讯开发
社区管理员
  • 网络及通讯开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧