Results 1 to 10 of 10
How do u read binary data from an executable file ?? ...........
- 04-21-2008 #1Just Joined!
- Join Date
- Dec 2007
- Location
- bangalore
- Posts
- 38
reading binary data?
How do u read binary data from an executable file ?? ........
- 04-21-2008 #2
This could mean so many things. Are you using a binary executable file to read binary data from some other file? Are you trying to read binary data that's in some executable file? What?
Please give us many, many details.--
Bill
Old age and treachery will overcome youth and skill.
- 04-21-2008 #3Just Joined!
- Join Date
- Dec 2007
- Location
- bangalore
- Posts
- 38
i thought an executable file was a file containing binary data........anway i want to read the contents from an executable file and send it to the server through sockets (Clanguage)..........so the contents in the file must be binary so i was wondering how will i read binary from the file as binary ,and sent it as binary and write it into a file as binary.....
- 04-21-2008 #4Just Joined!
- Join Date
- Dec 2007
- Location
- bangalore
- Posts
- 38
here is a part where i read from executable file and send it to the server but i m not able to get it as executable file..............but i do get all the contents
CLIENT.c
***********
if((fp=fopen("sample","r"))==NULL){
perror("ERROR IN fp");
exit(1);
}
i=j=0;
sent=0;
printf("\n here 1");
while(j<1830) {
c=fgetc(fp);
if((sent+255)< 1830) {
if(i<255){
RB_G_buf[i]=c;
printf("%c",c);
i++;
}
else{
RB_G_buf[i]='\0';
send_com(RB_G_buf);
i=0;
printf("\n%c",c);
RB_G_buf[i]=c;
i++;
sent=sent+255;
j=j+1;
printf("==================== i is %d sent is %d j is %d\n",i,sent,j);
continue;
}
}
else
{
printf("******************* i is %d sent is %d j is %d\n",i,sent,j);
if(i<(1830-sent)){
RB_G_buf[i]=c;
printf("%c",c);
i++;
}
else{
RB_G_buf[i]='\0';
send_com(RB_G_buf);
i=0;
printf("\n%c",c);
RB_G_buf[i]=c;
i++;
continue;
}
}
j++;
}
RB_G_buf[i]='\0';
send_com(RB_G_buf);
****************
Server.c
********
void reading_the_file(int i,int newfd)
{
printf("\n enter here");
FILE *fp;
int n,j=0,k=0;
char buffer[BUFFER_LENGTH], buffer2[BUFFER_LENGTH],*temp;
char filename[100];
unsigned long fileSize=1830, sizeAlreadyRead=0;
if((fp=fopen("new","w"))==NULL){
perror("error in fp new");
exit(1);
}
bzero(buffer, sizeof(buffer));
bzero(buffer2, sizeof(buffer2));
// Read data from the socket for the first time
n=read(newfd, buffer, sizeof(buffer)-1);
if(n==-1)
error("Socket read error. ");
// get the file size from the header
// printf("File Size read = %lu Bytes\n", fileSize);
printf("The message read from the socket is: %s \n\n", buffer);
// Remove header from the message to write into file
strcpy(buffer2, removeHeader((char*)buffer));
printf("Message without the header: %s \n", buffer2);
sizeAlreadyRead = strlen(buffer2);
printf("Size already read: %lu\n", sizeAlreadyRead);
fwrite(buffer2, sizeof(char), strlen(buffer2), fp);
while(sizeAlreadyRead < fileSize-BUFFER_LENGTH + 1)
{
bzero(buffer, sizeof(buffer));
n=read(newfd, buffer, sizeof(buffer)-1);
printf("The message read from the socket is: %s \n\n", buffer);
if(n==-1) error("Socket read error. ");
sizeAlreadyRead += sizeof(buffer)-1;
printf("Size already read: %lu\n", sizeAlreadyRead);
fwrite(buffer, sizeof(char), sizeof(buffer)-1, fp);
}
// Write the remaining bytes - which are less than BUFFER_LENGTH
printf("Now transfering last amount of data. %lu",fileSize-sizeAlreadyRead);
bzero(buffer, sizeof(buffer));
n=read(newfd, buffer, fileSize-sizeAlreadyRead+1);
if(n==-1) error("Socket read error. ");
printf("The message read from the socket is: %s size is %d \n", buffer,fileSize-sizeAlreadyRead);
k=fileSize-sizeAlreadyRead;
sizeAlreadyRead += sizeof(buffer);
buffer[k]='\0';
temp=(char *)malloc (k);
strcpy(temp,buffer);
fwrite(temp, sizeof(char), k, fp);
fclose(fp);
printf("File saved successfully!! ");
free(temp);
if(n<0) error("Cannot write to socket.");
printf("\n leave here");
//printf("The message read from the socket is: %s", buffer);
}
do c the code
- 04-21-2008 #5Just Joined!
- Join Date
- Dec 2007
- Location
- bangalore
- Posts
- 38
this is being done on linux
- 04-21-2008 #6
It looks to me as though the biggest piece that's missing from your source code is any code to initialize network communication. If this is because you're unfamiliar with network programming, then the yellow brick road you are about to walk is a long, arduous yellow brick road.
If that's the case, you have a lot to learn, but it will be interesting and fun! For best results, go to your bookseller and order the book UNIX Network Programming, volume 1, by W. Richard Stevens, published by Addison-Wesley.
While waiting for that book to arrive, you may find something useful by googling this:
Hope this helps.Code:network programming
--
Bill
Old age and treachery will overcome youth and skill.
- 04-21-2008 #7Just Joined!
- Join Date
- Dec 2007
- Location
- bangalore
- Posts
- 38
well......i do believe that the person who went thro the code is not dumb to think that i havent done the initializing part for network communication......it was just that i didnt mention it.......sorryy for that....
CLIENT.c
***********
intializing n/w communication like creating sockets and connecting
if((fp=fopen("sample","r"))==NULL){
perror("ERROR IN fp");
exit(1);
}
i=j=0;
sent=0;
printf("\n here 1");
while(j<1830) {
c=fgetc(fp);
if((sent+255)< 1830) {
if(i<255){
RB_G_buf[i]=c;
printf("%c",c);
i++;
}
else{
RB_G_buf[i]='\0';
send_com(RB_G_buf);
i=0;
printf("\n%c",c);
RB_G_buf[i]=c;
i++;
sent=sent+255;
j=j+1;
printf("==================== i is %d sent is %d j is %d\n",i,sent,j);
continue;
}
}
else
{
printf("******************* i is %d sent is %d j is %d\n",i,sent,j);
if(i<(1830-sent)){
RB_G_buf[i]=c;
printf("%c",c);
i++;
}
else{
RB_G_buf[i]='\0';
send_com(RB_G_buf);
i=0;
printf("\n%c",c);
RB_G_buf[i]=c;
i++;
continue;
}
}
j++;
}
RB_G_buf[i]='\0';
send_com(RB_G_buf);
****************
Server.c
********
void reading_the_file(int i,int newfd)
{
printf("\n enter here");
FILE *fp;
int n,j=0,k=0;
char buffer[BUFFER_LENGTH], buffer2[BUFFER_LENGTH],*temp;
char filename[100];
unsigned long fileSize=1830, sizeAlreadyRead=0;
if((fp=fopen("new","w"))==NULL){
perror("error in fp new");
exit(1);
}
bzero(buffer, sizeof(buffer));
bzero(buffer2, sizeof(buffer2));
// Read data from the socket for the first time
n=read(newfd, buffer, sizeof(buffer)-1);
if(n==-1)
error("Socket read error. ");
// get the file size from the header
// printf("File Size read = %lu Bytes\n", fileSize);
printf("The message read from the socket is: %s \n\n", buffer);
// Remove header from the message to write into file
strcpy(buffer2, removeHeader((char*)buffer));
printf("Message without the header: %s \n", buffer2);
sizeAlreadyRead = strlen(buffer2);
printf("Size already read: %lu\n", sizeAlreadyRead);
fwrite(buffer2, sizeof(char), strlen(buffer2), fp);
while(sizeAlreadyRead < fileSize-BUFFER_LENGTH + 1)
{
bzero(buffer, sizeof(buffer));
n=read(newfd, buffer, sizeof(buffer)-1);
printf("The message read from the socket is: %s \n\n", buffer);
if(n==-1) error("Socket read error. ");
sizeAlreadyRead += sizeof(buffer)-1;
printf("Size already read: %lu\n", sizeAlreadyRead);
fwrite(buffer, sizeof(char), sizeof(buffer)-1, fp);
}
// Write the remaining bytes - which are less than BUFFER_LENGTH
printf("Now transfering last amount of data. %lu",fileSize-sizeAlreadyRead);
bzero(buffer, sizeof(buffer));
n=read(newfd, buffer, fileSize-sizeAlreadyRead+1);
if(n==-1) error("Socket read error. ");
printf("The message read from the socket is: %s size is %d \n", buffer,fileSize-sizeAlreadyRead);
k=fileSize-sizeAlreadyRead;
sizeAlreadyRead += sizeof(buffer);
buffer[k]='\0';
temp=(char *)malloc (k);
strcpy(temp,buffer);
fwrite(temp, sizeof(char), k, fp);
fclose(fp);
printf("File saved successfully!! ");
free(temp);
if(n<0) error("Cannot write to socket.");
printf("\n leave here");
//printf("The message read from the socket is: %s", buffer);
}
- 04-21-2008 #8
So just what is your question, then?
--
Bill
Old age and treachery will overcome youth and skill.
- 04-22-2008 #9
I'm assuming you mean that the code all works and you get a file on the server but it doesn't execute? Check the size of the file after it is moved to the server. If they are different sizes, the way the programs are reading and writing the file data may be incorrect. If they are the same size, it probably worked but you just didn't set the permissions before trying to execute the file.
How are you trying to run the file? Did you change the permissions to make it executable on the server (chmod +x name_of_your_file)?
- 04-22-2008 #10
I'm assuming you mean that the code all works and you get a file on the server but it doesn't execute? Check the size of the file after it is moved to the server. If they are different sizes, the way the programs are reading and writing the file data may be incorrect. If they are the same size, it probably worked but you just didn't set the permissions before trying to execute the file.
How are you trying to run the file? Did you change the permissions to make it executable on the server (chmod +x name_of_your_file)?


Reply With Quote