📄 mtsclient.cpp
字号:
/*
Author :- Nish [BusterBoy]
EMail :- nishinapp@yahoo.com
*/
#include "stdafx.h"
#include "MTSClient.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CWinApp theApp;
int EndFile(char *buff, int len) ;
int NoFile(char *buff, int len) ;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
CString servername=_T("127.0.0.1");
WSADATA wsaData;
struct sockaddr_in server;
int wsaret=WSAStartup(0x101,&wsaData);
if(wsaret)
return 0;
SOCKET conn;
conn=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(conn==INVALID_SOCKET)
return 0;
server.sin_addr.s_addr=inet_addr(servername.GetBuffer(0));
server.sin_family=AF_INET;
server.sin_port=htons(20248);
if(connect(conn,(struct sockaddr*)&server,sizeof(server)))
{
closesocket(conn);
return 0;
}
char buff[512];
int z;
z=recv(conn,buff,512,0);//连接成功,接收到server ready
buff[z]=0;
cout<<"from server below:"<<cout.width(strlen(buff))<<buff<<endl;
strcpy(buff,"auth passwd\r\n");
send(conn,buff,strlen(buff),0);//发送授权密码
z=recv(conn,buff,512,0);//接收登陆成功
buff[z]=0;
cout<<"from server below:"<<cout.width(strlen(buff))<<buff<<endl;
cout<<"Do you want to get a file?('y' to contiue,'n' to end):\n";
while (_getch()==121) //y的ASCII码值为121
{ cout<<"please input the file which you want to get(the full path):\n";
char filename[100];
cin>>filename;
buff[0]='F'; buff[1]='I'; buff[2]='L'; buff[3]='E'; buff[4]=' ';
cout<<"strlen(filename):"<<strlen(filename)<<endl;
for(int i=0;i<strlen(filename);i++)
{
buff[5+i]=filename[i];
}
buff[5+i]='\0';
cout<<"i:"<<i<<" buff:"<<buff<<endl;
// sprintf(buff,"file %s\r\n",filename);
send(conn,buff,strlen(buff),0);//发送文件名字
CFile f;
// CString newfile="e:\\spring.txt";
cout<<"Please input the file which you want to write to:\n";
char savefile[100];
cin>>savefile;
f.Open(savefile,CFile::modeCreate | CFile::modeWrite);
bool first=true;
bool second=false;
while(true)
{
z=recv(conn,buff,128,0);
if(z==SOCKET_ERROR)
{
cout << "\r\n\r\nsocket error socket error socket error\r\n";
break;
}
if(second)
{
if(NoFile(buff,128)<128)
{
cout << "File " << filename << " not found on server\r\n";
break;
}
second=false;
}
if(first)
{
if(NoFile(buff,128)<128)
{
cout << "File " << filename << " not found on server\r\n";
break;
}
first=false;
second=true;
}
int b;
if((b=EndFile(buff,z))<z)
{
f.Write(buff,b);
cout << filename << " has been saved.\r\n";
break;
}
f.Write(buff,z);
}
f.Close();
cout<<"Do you want to get a file?('y' to contiue,'n' to end):\n";
}
closesocket(conn);
WSACleanup();
return 0;
}
int EndFile(char *buff, int len)
{
int pos=len;
for(int u=0;u<(len-4);u++)
{
if(buff[u]=='#')
if(buff[u+1]=='F')
if(buff[u+2]=='i')
if(buff[u+3]=='l')
if(buff[u+4]=='e')
{
pos=u;
break;
}
}
return pos;
}
int NoFile(char *buff, int len)
{
int pos=len;
for(int u=0;u<(len-4);u++)
{
if(buff[u]=='!')
if(buff[u+1]=='F')
if(buff[u+2]=='i')
if(buff[u+3]=='l')
if(buff[u+4]=='e')
{
pos=u;
break;
}
}
return pos;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -