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");
}
CyberMageAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tchrisCommented:
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

0
nietodCommented:
alternately, if you do what something done, what?
0
CyberMageAuthor Commented:
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.
0
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

CyberMageAuthor Commented:
I want it coded so, that you could also send an attached file along with it; does
anyone know how to do that?!
0
WxWCommented:
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
0
deliriumCommented:
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.
0
nietodCommented:
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.
0
alexoCommented:
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.

0
WxWCommented:
I think that the speed will not be so important , as the non-portability that you will get by using VC++ sock libraries.
0
RONSLOWCommented:
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?

0
tchrisCommented:
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
0
RONSLOWCommented:
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);

0
CyberMageAuthor Commented:
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?
0
tchrisCommented:
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
0
nietodCommented:
If this isn't the forum, what is?  
0
tchrisCommented:
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
0
nietodCommented:
learn the langauge if you are going to create or maintain code in it?  I guess that would be a good idea.
0
CyberMageAuthor Commented:
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.
0
tchrisCommented:
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
0
RONSLOWCommented:
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;
}

0
CyberMageAuthor Commented:
RONSLOW, I do not know C++...so I cannot fix up the syntax; that is why this is being reopened!
0
RONSLOWCommented:
Tell me the problem and I'll fix it up.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.