Thread: stucks on second command over network

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

    stucks on second command over network

    hi,
    the following code works properly on the local computer; but, it gets stuck after the first output when tried over a network.
    socket2.h
    Code:
    #pragma once
    #include<Winsock2.h>
    #include<iostream>
    #include<fstream>
    #include<string>
    
    using namespace std;
    
    int cnt;
    bool exit_status=0;
    int x=0,a=0,b=0,z=0;
    class Socket
    {
    
         protected:
                   WSADATA wsadata;            
                   SOCKET backup;
                   SOCKET Accept;
                   sockaddr_in address;
         public:
                SOCKET msocket;
                Socket();
                ~Socket();
                void closeconnection();
                bool SendData(char *);
                void RecvData(char *,int);
    };
    class ServerSocket: public Socket
    {
          public:
                 void Listen();
                 void Bind(int port);
                 void starthosting(int port);                 
    };
    class ClientSocket: public Socket
    {
          public:
                 void connecttoserver(const char *ipaddr, int port);
                 void RecvFile(char *,int);
                 void RecvFile1();
    };
    Socket::Socket()
    {
                    if((WSAStartup(MAKEWORD(2,2),&wsadata))!=NO_ERROR)
                    {
                                                                      cout<<"error with initialization";
                                                                      system("pause");
                                                                      WSACleanup();
                                                                      exit(0);
                    }
                    msocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
                    if(msocket== INVALID_SOCKET)
                    {
                                cout<<"error with initializtion 2"<<endl;
                                system("pause");
                                WSACleanup();
                                exit(0);
                    }
                    backup=msocket;
    }
    Socket::~Socket()
    {
                     WSACleanup();
    }
    void Socket::closeconnection()
    {
         WSACleanup();
         //char *exit= "exit";
         //send(msocket,"exit",5,0);
         //msocket=backup;
         exit(0);
         //exit_status=1;
    }
    void ServerSocket::starthosting(int port)
    {
         Bind(port);
         Listen();
    }
    void ServerSocket::Bind(int port)
    {
         address.sin_family=AF_INET;
         address.sin_port=htons(port);
         address.sin_addr.s_addr= inet_addr("0.0.0.0");
         if((bind(msocket,(SOCKADDR*) &address,sizeof(address)))!=NO_ERROR)
         {
                                     cout<<"error with binding"<<endl;
                                     system("pause");
                                     WSACleanup();
                                     exit(0);
         }
    }
    void ServerSocket::Listen()
    {
         if(listen(msocket,1)==SOCKET_ERROR)
         {
                                           cout<<"socket error"<<endl;
                                           system("pause");
                                           WSACleanup();
                                           exit(0);
         }
         Accept=accept(backup,NULL,NULL);
         while(Accept==SOCKET_ERROR)
         {
                                    Accept=accept(backup,NULL,NULL);
         }
         msocket=Accept;
    }
    void ClientSocket::connecttoserver(const char *ipaddr,int port)
    {
         address.sin_family=AF_INET;
         address.sin_addr.s_addr=inet_addr(ipaddr);
         address.sin_port=htons(port);
         if( ( cnt=connect(msocket,(SOCKADDR *) &address,sizeof( address ) ) )!=NO_ERROR)
         {
                                      cout<<"error with connection"<<endl;
                                      system("pause");
                                      WSACleanup();
                                      exit(0);
         }
         cout<<"successfully connected"<<endl;
    }
    bool Socket::SendData(char *buffer)
    {
         
         send(msocket,buffer,strlen(buffer),0);
         //memset(buffer,0,sizeof(buffer));
         return TRUE;
    }
    void Socket::RecvData(char *buff,int size)
    {
                               {
                                         cout<<"came here"<<endl;
                                         int i=recv(msocket,buff,size,0);
                                         buff[i]='\0';
                                         //int k=0;
                                         /*while((buff[k]!='\0')&&(buff[k]!='.'))
                                         {
                                                                                     cout<<"bht hua"<<endl;
                                                                                     k++;
                                         }
                                         if(buff[k]=='.')
                                         {
                                                            x=1;
                                                            FILE *open=fopen(buff,"r");
                                                            char ch;
                                                            while((ch=fgetc(open))!='\0')
                                                            {
                                                                                       send(msocket,&ch,1,0);
                                                            }
                                                            fclose(open);                                                        
                                         }
                                         else{   
                                         */
                                         printf("%s",buff);
                                         char line[200];
                                         FILE *fp=popen(buff,"r");
                                         if(fp==NULL) printf("file error");
                                         //memset(buff,0,sizeof(buff));                                           
                                         //{                getline(fp,line);
                                         while(fgets(line,sizeof(line),fp)!='\0')
                                         {
                                                                                 printf("in loop");
                                                                                 SendData(line);
                                                                              
                                         }
                                         printf("out of loop");
                                         //line="DONE";
                                         SendData("DONE");
                                         printf("out of loop");
                                         //delete(line);
                               }
                                         
    }
                               
    
                               
    
    
                      
    void ClientSocket::RecvFile(char buff[256],int size)
    {
         //if(x==z)
         {
                 //int i=recv(msocket,buff,size,0);
                 //buff[i]='\0';
                 while(strcmp(buff,"DONE"))
                 {
                                           int i;
                                           i=recv(msocket,buff,size,0);
                                           buff[i]='\0';
                                           printf("%s",buff);
                                           cout<<"still inn loop"<<endl;
                                           //printf("in loop recvfile");
                                           
                                           //system("pause");
                 }
                 cout<<"out of loop"<<endl;
                 system("pause");
         }
         //system("pause");
         //memset(buff,0,sizeof(buff));
         //printf("loop exit");
    }
    void ClientSocket::RecvFile1()                                 
    {
         char *rec;
         FILE *filerecv=fopen("received.txt","a");
         while(recv(msocket,rec,1,0))
                                      fputc(*rec,filerecv);
         fclose(filerecv);
         cout<<"file transfer complete"<<endl;
    }
    main.cpp

    Code:
    #include"socket2.h"
    
    using namespace std;
    
    char buffer[256];
    bool value;
    void wait ( int seconds )
    {
      clock_t endwait;
      endwait = clock () + seconds * CLOCKS_PER_SEC ;
      while (clock() < endwait) {}
    }
    int main()
    {
        int port=80;
        int choice;
        char c='y';
        string ipaddress;
        char recvmsg[256];
        char sendmsg[256];
        char recved[256];
        cout<<"1. client"<<endl ;
        cout<<"2. server"<<endl ;
        cout<<"3. exit"<<endl;
        cin>>choice;
        if(choice==3)
                     exit(0);
        else if(choice==1)
        {
             cout<<"enter the ip address to which u want to connect"<<endl;
             cin>>ipaddress;
             ClientSocket csocket;
             cout<<"---Attempting to connect---"<<endl;
             csocket.connecttoserver(ipaddress.c_str(),port);
             //do{
             while(1)
             {
                     cout<<"enter the string to be sent"<<endl;
                     cin.ignore();
                     cin.get(sendmsg,256);
                     //t k=0; 
                     csocket.SendData(sendmsg);
                     //(x==0)
                     csocket.RecvFile(recved,256);
                     /*se if(x==1)
                     {
                                 csocket.RecvFile1();
                                 /*FILE *filerecv=fopen("received.txt","w");
                                 char rec;
                                 recv(msocket,&rec,1,0);
                                 fputc(rec,filerecv);
                                 }*/
                     //se cout<<"error in x"<<endl;
                     system("pause");
                     //cout<<"do u want to exit?y/n?"<<endl;
                     //cin>>c;
             }
             //while(c=='y');
             csocket.closeconnection();
        }
        
        else
        {
            cout<<"---Waiting for connection---";
            ServerSocket ssocket;
            ssocket.starthosting(port);
            //while(a!=1)
            //while(strcmp(recvmsg,"exit"))
            //{
            while(1)
                    ssocket.RecvData(recvmsg,256);
                                         //wait(5);
            //                           }while(strcmp(recvmsg,"exit"));
            //cout<<recvmsg<<endl;
            system("pause");
        }  
    
    }

  2. #2
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    To brief with the program I was trying to make was to connect to a computer with a specified IP address & browse through its files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line how to handle optional command.
    By ovid in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2010, 11:41 PM
  2. Connect to a wireless network via Linux command line
    By lehe in forum Networking/Device Communication
    Replies: 4
    Last Post: 08-10-2009, 05:18 AM
  3. Network!
    By Joanna in forum Tech Board
    Replies: 3
    Last Post: 07-20-2004, 06:40 AM
  4. command-line network monitor
    By rotis23 in forum Linux Programming
    Replies: 1
    Last Post: 11-12-2002, 12:49 PM