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;
}
*********************************