Solved

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

Posted on 1998-06-03
22
310 Views
Last Modified: 2010-04-10
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");
}
0
Comment
Question by:CyberMage
  • 5
  • 5
  • 4
  • +4
22 Comments
 
LVL 1

Expert Comment

by:tchris
ID: 1165196
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
 
LVL 22

Expert Comment

by:nietod
ID: 1165197
alternately, if you do what something done, what?
0
 

Author Comment

by:CyberMage
ID: 1165198
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
 

Author Comment

by:CyberMage
ID: 1165199
I want it coded so, that you could also send an attached file along with it; does
anyone know how to do that?!
0
 
LVL 6

Expert Comment

by:WxW
ID: 1165200
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
 
LVL 1

Expert Comment

by:delirium
ID: 1165201
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
 
LVL 22

Expert Comment

by:nietod
ID: 1165202
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
 
LVL 11

Expert Comment

by:alexo
ID: 1165203
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
 
LVL 6

Expert Comment

by:WxW
ID: 1165204
I think that the speed will not be so important , as the non-portability that you will get by using VC++ sock libraries.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1165205
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
 
LVL 1

Expert Comment

by:tchris
ID: 1165206
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1165207
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
 

Author Comment

by:CyberMage
ID: 1165208
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
 
LVL 1

Expert Comment

by:tchris
ID: 1165209
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
 
LVL 22

Expert Comment

by:nietod
ID: 1165210
If this isn't the forum, what is?  
0
 
LVL 1

Expert Comment

by:tchris
ID: 1165211
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
 
LVL 22

Expert Comment

by:nietod
ID: 1165212
learn the langauge if you are going to create or maintain code in it?  I guess that would be a good idea.
0
 

Author Comment

by:CyberMage
ID: 1165213
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
 
LVL 1

Expert Comment

by:tchris
ID: 1165214
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
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1165215
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
 

Author Comment

by:CyberMage
ID: 1165216
RONSLOW, I do not know C++...so I cannot fix up the syntax; that is why this is being reopened!
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 50 total points
ID: 1165217
Tell me the problem and I'll fix it up.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now