Hi for all,
I'm newer with Delphi can anybody help me to convert the following c++ code to
delphi7 one's.

many thanks in advance

c++ code :
*********************************
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#define BUFSIZE 216 /* size of message buffer */

void usage(char *progname)
{
printf("usage: %s [-h host] [-p port]\n", progname);
exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
char *host = "localhost";
int port = 6661;
int fd;
struct hostent *hostentry = NULL;
struct sockaddr_in addr;
int n;
int c, errflg = 0;
char buffer[BUFSIZE];
char header[7];
char ibuffer[256];
FILE *f = freopen("/home/err_log","w",stderr);

memset(ibuffer, 0, 256);
memset ( header, 0, sizeof ( header ));
header[0] = ((strlen(argv[5])+4)/256);
header[1] = ((strlen(argv[5])+4)%256);
memcpy ( header + 2 , "1234", 4);
memcpy ( ibuffer ,header , 6);
memcpy ( ibuffer + 6 ,argv[5] ,strlen(argv[5]));
while ((c=getopt(argc, argv, "h:p:")) != -1) {
switch(c) {
case 'h': /* server host name */
host = optarg;
break;
case 'p': /* server port */
port = atoi(optarg);
break;
default: /* the rest are errors */
errflg++;
break;
}
}

if (host == NULL || port == 0 || errflg) {
usage(argv[0]);
}

signal(SIGPIPE, SIG_IGN);

/* create a TCP socket */
if ((fd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
printf("sock00:socket error %d\n", errno);
exit(EXIT_FAILURE);
}


/* connect to the socket server */
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);

if (strcmp(host, "any") == 0) {
addr.sin_addr.s_addr = htonl(INADDR_ANY);
} else if (strcmp(host, "loopback") == 0) {
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
} else {
if ((hostentry = gethostbyname(host)) == NULL) {
printf("sock00:invalid hostname \"%s\"\n", host);
exit(EXIT_FAILURE);
}
memcpy(&addr.sin_addr.s_addr, hostentry->h_addr, hostentry->h_length);
}

if (connect(fd, (struct sockaddr *)&addr,
sizeof(struct sockaddr)) < 0) {
printf("sock00:connect error %d\n", errno);
exit(EXIT_FAILURE);
}

if (send(fd, ibuffer, sizeof(ibuffer), 0) <= 0) {
printf("sock00:send error %d\n", errno);
exit(EXIT_FAILURE);
}

memset(buffer, 0, sizeof(buffer));
/* fprintf(stderr,"\nCMD :%s\n",ibuffer+6); */
if ((n = recv(fd, buffer, sizeof(buffer), MSG_WAITALL)) < 0) {
printf("recv error %d\n", errno);
exit(EXIT_FAILURE);
}
/* printf("%s\n",buffer+6 ) ;*/
fprintf(stderr,"%s\n",buffer+10);
close(fd);

return EXIT_SUCCESS;
}

*********************************

Recommended Answers

All 6 Replies

or delphi4 one's ...
Any help would be very appreciated.

hi there...
I got coverter which convert the C/C++ code into Pascal language. if you're interesting, please give me a PM. Thanks.

Hi Pramu,

Thanks for your reply, If you try it with the above code and see the result together.
Cheers.

Why not keep it in C++? Delphi will never be a match for C++ (Cross-platform, faster execution, more features etc)

Hi,
Sure C++ is the most powerful programming language but just for my own need I would like to convert it to Delphi (use it on a PC instead of mainframe).

Thanks

Hi Pramu,

Thanks for your reply, If you try it with the above code and see the result together.
Cheers.

here're the result:

unit _1_cpp;

{*************************************************************************

->  Converted with C to Pascal Converter 1.0
->  Release: 1.5.11.2008
->  Email: [email]al_gun@ncable.net.au[/email]
->  Updates: [url]http://cc.codegear.com/Author/302259[/url]
->  Copyright © 2005-2008 Ural Gunaydin, All rights reserved.

*************************************************************************}

interface

uses
    Windows, Messages, SysUtils, Classes;

//include <stdio.h>
//include <stdlib.h>
//include <signal.h>
//include <ctype.h>
//include <cString.h>
//include <errno.h>
//include <sys/param.h>
//include <sys/types.h>
//include <netinet/in.h>
//include <sys/socket.h>
//include <netdb.h>
//include <unistd.h>
const BUFSIZE = 216; (* size of message buffer *)

procedure usage(progname: PChar);
begin 
printf('usage:  mod s [-h host] [-p port]', progname);
exit(EXIT_FAILURE);
 end;

function main(argc: integer; argv: PChar): integer;
begin 
Char *host := 'localhost';
integer port := 6661;
integer fd;
struct hostent *hostentry := 0;
struct sockaddr_in addr;
integer n;
integer c, errflg := 0;
 buffer: array[0..BUFSIZE-1] of Char;
 header: array[0..7-1] of Char;
 ibuffer: array[0..256-1] of Char;
cFile *f := freopen('/home/err_log','w',stderr);

FillChar(ibuffer, 0, 256);
FillChar ( header, 0, SizeOf ( header ));
header[0] := ((strlen(argv[5])+4)/256);
header[1] := ((strlen(argv[5])+4) mod 256);
memcpy ( header + 2 , '1234', 4);
memcpy ( ibuffer ,header , 6);
memcpy ( ibuffer + 6 ,argv[5] ,strlen(argv[5]));
while ((c=getopt(argc, argv, 'h:p:')) <> -1) begin 
case(c) begin of 
 'h': (* server host name *)
host := optarg;
break;
 'p': (* server port *)
port := StrToInt(optarg);
break;
default: (* the rest are errors *)
errflg:= mod + 1;
break;
 end;
 end;

if (host = 0 or port = 0 or errflg) then  begin 
usage(argv[0]);
 end;

signal(SIGPIPE, SIG_IGN);

(* create a TCP socket *)
if ((fd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) then  begin 
printf('sock00:socket error  mod d', errno);
exit(EXIT_FAILURE);
 end;


(* connect to the socket server *)
FillChar( and addr, 0, SizeOf(struct sockaddr_in));
addr.sin_family := AF_INET;
addr.sin_port := htons(port);

if (StrComp(host, 'any') = 0) then  begin 
addr.sin_addr.s_addr := htonl(INADDR_ANY);
 end; else if (StrComp(host, 'loopback') := 0) then  begin 
addr.sin_addr.s_addr := htonl(INADDR_LOOPBACK);
 end; else begin 
if ((hostentry = gethostbyname(host)) = 0) then  begin 
printf('sock00:invalid hostname ' mod s'', host);
exit(EXIT_FAILURE);
 end;
memcpy( and addr.sin_addr.s_addr, hostentry^.h_addr, hostentry^.h_length);
 end;



if (connect(fd, (struct sockaddr ) then  and addr,
SizeOf(struct sockaddr)) < 0) begin 
printf('sock00:connect error  mod d', errno);
exit(EXIT_FAILURE);
 end;

if (send(fd, ibuffer, SizeOf(ibuffer), 0) <= 0) then  begin 
printf('sock00:send error  mod d', errno);
exit(EXIT_FAILURE);
 end;

FillChar(buffer, 0, SizeOf(buffer));
(* fprintf(stderr,"\nCMD :%s\n",ibuffer+6); *)
if ((n = recv(fd, buffer, SizeOf(buffer), MSG_WAITALL)) < 0) then  begin 
printf('recv error  mod d', errno);
exit(EXIT_FAILURE);
 end;
(* printf("%s\n",buffer+6 ) ;*)
fprintf(stderr,' mod s',buffer+10);
close(fd);

result:= EXIT_SUCCESS;
 end;

implementation
end.

hopefully it can solve your problem. ;) but surely it still need lot of correction from ourself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.