file browser : second command doesnt work

This is a discussion on file browser : second command doesnt work within the C++ Programming forums, part of the General Programming Boards category; I tried to develop a file browser of a connected pc. Following is the code... main: Code: #include<iostream> #include<string> #include"socket2.h" ...

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

    Post file browser : second command doesnt work

    I tried to develop a file browser of a connected pc. Following is the code...
    main:
    Code:
    #include<iostream>
    #include<string>
    #include"socket2.h"
    #include<stdio.h>
    #include<time.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=666;
        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;
             ipaddress="127.0.0.1";
             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);
                     csocket.SendData(sendmsg);
                     csocket.RecvFile(recved,256);
                     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");
        }  
    
    }
    socket2.h:
    Code:
    #pragma once
    #include<Winsock2.h>
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    int cnt;
    bool exit_status=0;
    int x=1,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);
    };
    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";
         SendData(exit);
         msocket=backup;
         //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)
                 {
                               //if(x==z)
                               {
                                         printf("came here");
                                         int i=recv(msocket,buff,size,0);
                                         buff[i]='\0';
                                         ++x;
                                         printf("%s",buff);
                                         {     
                                               char *line;
                                               FILE *fp=popen(buff,"r");
                                               if(fp==NULL) printf("file error");
                                               //memset(buff,0,sizeof(buff));                                           
                                               while(fgets(line,sizeof(line),fp)!='\0')
                                               {
                                                                              printf("in loop");
                                                                              SendData(line);
                                                                              
                                               }
    
                                               printf("out of loop");
                                               line="DONE";
                                               SendData(line);
                                               delete(line);
                                         }
                               }
    
                               
                 }     
    void ClientSocket::RecvFile(char buff[50],int size)
    {
         //if(x==z)
         {
                 int i=recv(msocket,buff,size,0);
                 buff[i]='\0';
                 ++x;
                 while(strcmp(buff,"DONE"))
                 //while(1)
                 {
                                           i=recv(msocket,buff,size,0);
                                           buff[i]='\0';
                                           //printf("in loop recvfile");
                                           printf("%s",buff);
                                           //system("pause");
                 }
         }
         //system("pause");
         memset(buff,0,sizeof(buff));
         //printf("loop exit");
    }





    It runs through the first cycle ( send command & get an output back). But the server side crashes in second cycle with a following error :
    An Access Violation (Segmentation fault) error
    at the highlighted statement (while(fgets.....) in socket2.h

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    The problem is probably to do with the fact you declare a char pointer but don't make it point to anything and then try and read info off it anyway.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    i didnt get your point...fgets is getting the line from the opened file & that line is assigned the "line" pointer.
    Anyways, going by your point, it should not have worked in the first run...

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Code:
    fgets(line,sizeof(line),fp)
    This only works if line is a stack array, not a pointer.

    You need to allocate space for fgets to write to, such as by making line an array or by calling malloc(). If you use malloc, you will need to specify the length to fgets explicitly (instead of sizeof(line) and call free() later.

    The fact that your program sometimes works does not mean that there isn't anything wrong. You just got lucky. Or unlucky depending on how you look at it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    This is C++, so using malloc is usually a bad idea.
    You should start making this C into C++.
    Replace char with std::string. If you need a buffer, then use std::vector<char>.
    Replace fgets with std::getline.
    Replace printf with std::cout.
    Replace memset with std::fill.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Elysia's right.

    Also you should only be putting the class definitions is the header file. All those member functions and constructors should go in a separate source file.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-22-2010, 01:46 PM
  2. Why doesnt this work
    By Hugo716 in forum C++ Programming
    Replies: 13
    Last Post: 05-18-2006, 12:57 PM
  3. Why doesnt this work
    By digdug4life in forum C++ Programming
    Replies: 13
    Last Post: 06-19-2005, 04:22 PM
  4. why doesnt this work?
    By brad123 in forum C Programming
    Replies: 3
    Last Post: 04-23-2002, 06:26 PM
  5. Why doesnt this work?
    By dougaerb in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2001, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21