Link to home
Start Free TrialLog in
Avatar of CyberMage
CyberMage

asked on

C Needs To Be Changed To C++....

Can someone change this program from C to C++, this is a cool program...but it's slightly outdated as it is only C (yes I know C is still used but....)  So, here is the program:

#include <stdio.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdarg.h>

/* This program was coded by Michael Lauzon, (C) 1994 */

void smtp_connect(char *server);

int thesock; /* the socket */

void smtp_connect(char *server) {
  struct sockaddr_in sin;
  struct hostent *hp;
  hp = gethostbyname(server);
  if (hp==NULL) {
    printf("Unknown host: %s\n",server);
    exit(0);
  }
  bzero((char*) &sin, sizeof(sin));
  bcopy(hp->h_addr, (char *) &sin.sin_addr, hp->h_length);
  sin.sin_family = hp->h_addrtype;
  sin.sin_port = htons(25);
  thesock = socket(AF_INET, SOCK_STREAM, 0);
  connect(thesock,(struct sockaddr *) &sin, sizeof(sin));
}

void main(int argc, char **argv) {
  char buf[1024];
  if (argc != 4) {
    printf("usage: mflash smtp_server from to\n");
    exit(0);
  }
  printf("Connecting to SMTP Server %s\n",argv[1]);
  smtp_connect(argv[1]);
  printf("Sending Mail Flash To %s\n",argv[3]);
  sprintf(buf, "helo a\nmail from: %s\nrcpt to: %s\ndata\nSUBJECT:
\033c\033(0\033#8\033[1;3r\033[J\033[5m\033[?5h\n.\nquit\n",argv[2],argv[3]);
  send(thesock, buf, strlen(buf), 0);
 
  /* I am not sure how to check when this buffer is done being sent.  If you are having any problems increase the sleep time below! */
 
  printf("Sleeping To Make Sure Data Is Sent ...\n");
  sleep(3);
  printf("Done!\n");
}
Avatar of tchris
tchris
Flag of United States of America image

I guess my first question would be: why?  C++ is a superset of C, so all C is automatically C++ too.  Your program is already C++.  Is there some reason why you want it to be recoded in a more object-oriented fashion (which is what I assume you want)?  If it works, why recode?

Terry

Avatar of nietod
nietod

alternately, if you do what something done, what?
Avatar of CyberMage

ASKER

Because, I'd rather get it recoded...so it looks better; and C++ may be able to get it to run better; from C++ I'll get it changed into Visual C(++) and Turbo C(++).  Because what C doesn't have C++ will have.
I want it coded so, that you could also send an attached file along with it; does
anyone know how to do that?!
And if we were supposed to do it in C++ , how could we ? This program is using socket functions , and as far as I know , you will have more lines of code at first , and then you will have a real problem porting that from VC++ to Borland . If you leave it , it will be portable because of the winsock header
It's much better as it is now.  First of all, I have yet to meet the C++ program that runs faster than its C counterpart.  Sure, C++ is easier to code, maintain, etc., but I haven't heard anybody claim it's faster.  Classes and such add a layer of abstraction that simply doesn't exist at the processor level, which requires more code to do.  More code == slower execution.

And as the program is right now, the winsock headers make it portable to just about every c/c++ compiler in existance, so it's a lot better than using proprietary stuff.
There is no reason for C++ code to be any slower than C code.  A member function of a class will execute with the same speed as a non-member function that operates on a C structure.   virtual member function operates at the same speed as a C function called by a pointer.  If C++ code operates more slowly, it is because it does more.  That's hardly a fair test.  You can't compare creating an uninitialized stucture to creating and initialized object.
A C++ program is usually a bit (say, 5-10%) slower than its C counterpart.  The reason is that C++ is a more complex language so, with the presence of constructors, destructors, possibly exceptions and RTTI, etc. the compiler has less opportunities for optimization.

I think that the speed will not be so important , as the non-portability that you will get by using VC++ sock libraries.
It is already in C++

Which particular elements of C++ do you want to see included?  It doesn't really _need_ any changes, though.  (except perhaps that 'main' should return an int).

Or are you wanting to convert it to use MFC, perhaps?

Or, CyberMage, you could tweak this a little bit and then develop a class to wrap around it as a member function.  That way it would already be a working, debugged function.

Terry
How about a class like this...

class SMTP {
  int thesock; /* the socket */
public:
  SMTP(const char *server) { // I assume it should be const char*
    .. body of smtp_connect goes here
  }
  ~SMTP {
    .. do whatever to disconnect? perhaps
  }
  operator int() const { return thesock; }
  void send(const char* buf) {
    ::send(thesock, buf, strlen(buf), 0);
  }
};

then change main like this...

  SMTP mysmtp(argv[1]);
  printf("Sending Mail Flash To %s\n",argv[3]);
  ...
  mysmtp.send(buf);

RONSLOW, would you be kind enough to put the whole C code together, with your answer?
Also, this is for anybody.  I also want this program to send
along files with it; so I need a way to attach files...how can
this be done?
I personally don't feel this is the proper forum to have somebody TEACH you C++ and / or code your software for you.  C++ is very much worth the time to learn.  Besides, if somebody codes this for you, how are you going to maintain it?  Just my $0.02 worth.

Terry
If this isn't the forum, what is?  
I merely meant that it appears CyberMage doesn't know C++ but recognizes some advantages to it.  Why not invest the time and effort to learn the language rather than just ask somebody to do it for you?  If you are going to create and/or maintain code, you really need to learn the language. IMHO

Terry
learn the langauge if you are going to create or maintain code in it?  I guess that would be a good idea.
My C program is already coded, I was just asking him/her if he/she
could put his/her fix into it.  Also, I don't have the money to take
any C++ course...this program I wrote was my first and last
attempt at C.
I see.  I think that my comments sounded like criticism, but that wasn't my intention.  I just wanted to encourage you to learn C++.  There are a number of good books so you can learn it on your own.  I hope you will decide to do that, as it opens up many opportunities.

Terry
I thought my suggestions on how to do it were clear enough .. anyway .. this is pretty much it (I'll leave it for you to fix up any minor syntax errors and debug etc).

#include <stdio.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdarg.h>

/* This program was coded by Michael Lauzon, (C) 1994 */
// and modified by Roger Onslow
class SMTP {
  int thesock; // the socket
  // don't know if this is ok as an invalid socked value
  enum { BAD_SOCKET = -1 };
public:
  SMTP(const char *server) : thesock(BAD_SOCKET) {
    struct hostent *hp;
    hp = gethostbyname(server);
    if (! hp) return;
    struct sockaddr_in sin;
    bzero((char*) &sin, sizeof(sin));
    bcopy(hp->h_addr, (char *) &sin.sin_addr, hp->h_length);
    sin.sin_family = hp->h_addrtype;
    sin.sin_port = htons(25);
    thesock = socket(AF_INET, SOCK_STREAM, 0);
    connect(thesock,(struct sockaddr *) &sin, sizeof(sin));
  }
  ~SMTP() {
    // do we need to disconnect?
  }
  bool isok() const {
    return thesock != BAD_SOCKET;
  }
  void send(const char* buf) {
    ::send(thesock, buf, strlen(buf), 0);
  }
};

int main(int argc, char **argv) {
  char buf[1024];
  if (argc != 4) {
    printf("usage: mflash smtp_server from to\n");
    return 0;
  }
  printf("Connecting to SMTP Server %s\n",argv[1]);
  SMTP smtp(argv[1]);
  if (! smtp.isok()) {
    printf("Unknown host: %s\n",server);
    return -1;
  }
  printf("Sending Mail Flash To %s\n",argv[3]);
  sprintf(buf, "helo a\nmail from: %s\nrcpt to: %s\ndata\nSUBJECT:
\033c\033(0\033#8\033[1;3r\033[J\033[5m\033[?5h\n.\nquit\n",argv[2],argv[3]);
  smtp.send(buf);
   
  /* I am not sure how to check when this buffer is done being sent.  If you are having any problems increase the sleep time below! */
   
  printf("Sleeping To Make Sure Data Is Sent ...\n");
  sleep(3);
  printf("Done!\n");
  return 0;
}

RONSLOW, I do not know C++...so I cannot fix up the syntax; that is why this is being reopened!
ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial