Need assistance from a programmer!

CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
Spent nearly 45 minutes debugging a simple little application that took about 15 minutes to write in GDB and i traced it back to "fopen". I was getting a segfault when running the program. I had noticed that the address of the structure was changing in a completely different function when it should have been the same (it's been passed by pointer the whole time). Fixed it by moving fopen to the main function... If there is any proficient C programmer here, does fopen allocate on the heap, or stack? By the behavior I got it seems to be the stack. icon_rolleyes.gif I did google this and didn't come up with anything. Maybe I should start learning a new language...?

The program simply copies a specified file from the computer to whatever host on whatever port you specify.

sendclient.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>

#define BUFFER_SIZE 1024

typedef struct{
  char * fileName;
  char fileBuffer[BUFFER_SIZE];
}data;

typedef struct{
  int socketFD;
  struct sockaddr_in server;
}socketStruct;


/* Value-resultant function whose input is a character string */
/* Returned is the length of the filename                     */

int parseFileName( char * fileName ){
  char * temp = fileName + strlen( fileName ) + 1;
  char * bgPtr = temp;
  int charCounter = 0;

  while( *((bgPtr--) - 1) != '/' && bgPtr >= fileName )
    charCounter++;

  while( bgPtr++ < temp )
    *fileName++ = *bgPtr;

  *(fileName + charCounter ) = '\0';  

  return charCounter;
}

int prepareFile( FILE * filePtr , char * fileName, data * dataStruct ){

  long fileSize = 0;

[B][COLOR=#FF0000]  /* Had to move this code to main just for this to work
    if(!(filePtr = fopen( fileName, "r+"))){
      perror("fopen():");
      exit(-1);
    }
  */[/COLOR][/B]


  fseek( filePtr , 0 , SEEK_END );

  fileSize = ftell( filePtr );

  fseek( filePtr , 0 , SEEK_SET );

  parseFileName( fileName );

  dataStruct->fileName = fileName;

  return fileSize;
}

void prepareSocket( socketStruct * socketData ){
  if( (socketData->socketFD = socket( AF_INET, SOCK_STREAM , 0 ) ) < 0 ){
    perror( "socket():");
  }

  char ipAddress[INET_ADDRSTRLEN];

  char port[6];

  printf("Enter an IP Addres and port: ");

  scanf("%s %s" , ipAddress , port);

  socketData->server.sin_family = AF_INET;
  socketData->server.sin_port = htons(atoi( port ) );

  if( inet_pton( AF_INET , ipAddress , &socketData->server.sin_addr ) < 0 ){
    perror("inet_pton():");
    exit(-1);
  }

}

void sendFile( socketStruct * socketData , data * dataStruct , FILE * filePtr ){

  if( connect( socketData->socketFD , (struct sockaddr*)&socketData->server,
           sizeof(socketData->server)) < 0){
    perror("connect():");
    exit(-1);
  }

  int rBytes = 0 , wBytes = 0 , allBytes = 0;

  //This is to simply send the filename 

  // write( socketData->socketFD , dataStruct->fileName , strlen(dataStruct->fileName));

  while( rBytes = fread( dataStruct->fileBuffer , sizeof(char) , BUFFER_SIZE , filePtr)){
    wBytes = write( socketData->socketFD , dataStruct->fileBuffer ,
            rBytes);
    allBytes += wBytes;
  }

  printf("%d bytes written to the socket...\n" , allBytes);
}

int main( int argc , char ** argv ){

  FILE * filePtr;
  data dataStruct;
  socketStruct socketData;
  int fileSize = 0 , currFile = 0;

  while( currFile++ < argc ){
[B][COLOR=#0000FF]
//This had to be moved from prepareFile just for this to work

    if(!(filePtr = fopen( argv[currFile] , "r+"))){
      perror("fopen():");
      exit(-1);
    }[/COLOR][/B]

    fileSize = prepareFile( filePtr, argv[currFile] , &dataStruct ); 

    prepareSocket( &socketData );

    printf("Writing %d byte file...\n" , fileSize);

    sendFile( &socketData, &dataStruct , filePtr );

    fclose(filePtr);

  }
  fputs( "All files have been sent...\n" , stdout );

  return 0;
}



Currently reading: Network Warrior, Unix Network Programming by Richard Stevens

Comments

  • XcluzivXcluziv Member Posts: 513 ■■■■□□□□□□
    I think you need to come up to speed on a new language.....j/picon_lol.gif....didn't learn C, started out with C# which is an up-to-date version on the C framework....hmmm.....after doing a little research...from the look of it I may think it is the heap, this being that, when called, fopen allocates a file object on the heap.

    BTW, the details of how things like fopen() are implemented may also depend on the OS (UNIX has fopen() too)

    Not sure if this helped out any....
    icon_sad.gif
    LINKED | GTECH | NOTHINGBUTSHAREPOINT - BLOG AUTHOR

    "TRY NOT. DO. OR DO NOT. THERE IS NO TRY" - Yoda

  • CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
    hmm stepping through this again in GDB, it looks like it is on the heap. I say that because the memory address is really high there, where as in main, its really low. Did some more stepping through and it the change for some reason doesn't carry back to main even though this is a pointer. I'll have to look at this more tomorrow. Too sleepy now haha. I've been thinking about giving C# a try though!
    Currently reading: Network Warrior, Unix Network Programming by Richard Stevens
  • CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
    Figured out what I messed up!! I forgot that if I want to modify what a pointer points to, it's gotta be a double pointer. Makes since as to why the address wasn't keeping . Here is the correct way.
    int prepareFile([B][COLOR=#00ff00] FILE ** filePtr[/COLOR][/B] , char * fileName, data * dataStruct ){
    
      long fileSize = 0;
    
      
        if(!([COLOR=#00ff00][B]*filePtr[/B][/COLOR] = fopen(fileName , "r+"))){
          perror("fopen():");
          exit(-1);
        }
      
    
    
      fseek( [COLOR=#00ff00][B]*filePtr[/B][/COLOR] , 0 , SEEK_END );
    
      fileSize = ftell( *filePtr );
    
      fseek( [COLOR=#00ff00][B]*filePtr[/B][/COLOR] , 0 , SEEK_SET );
    
      //parseFileName( fileName );
    
      dataStruct->fileName = fileName;
    
      return fileSize;
    }
    
    Currently reading: Network Warrior, Unix Network Programming by Richard Stevens
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Xcluziv wrote: »
    I think you need to come up to speed on a new language.....j/picon_lol.gif....didn't learn C, started out with C# which is an up-to-date version on the C framework....hmmm.....after doing a little research...from the look of it I may think it is the heap, this being that, when called, fopen allocates a file object on the heap.

    BTW, the details of how things like fopen() are implemented may also depend on the OS (UNIX has fopen() too)

    Not sure if this helped out any....
    icon_sad.gif
    LOL! You would be proverbially crucified for such a statement in some circles!
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    CodeBlox wrote: »
    hmm stepping through this again in GDB, it looks like it is on the heap. I say that because the memory address is really high there, where as in main, its really low. Did some more stepping through and it the change for some reason doesn't carry back to main even though this is a pointer. I'll have to look at this more tomorrow. Too sleepy now haha. I've been thinking about giving C# a try though!

    I been enjoying c# but its is .net which is one minus point so you need a .net framework solution on the client machine, I would love to have the time to learn C++ so have more portability in applications, took a course once but it was a long time ago.

    I would also argue that c# is not a update of c or c++, it was written pretty much from scratch, like VB.net, they have a simmular (ish) interface to there namesakes, but they are completly different animals under the skin.
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
  • XcluzivXcluziv Member Posts: 513 ■■■■□□□□□□
    LOL! You would be proverbially crucified for such a statement in some circles!

    Yea I'm sure...hey, this coming from a guy who started in c#....lol. DevilWAH, yea....im guessing thats why I was under the impression of them being alike with the similar (ish) interfaces....welp, learned something new icon_thumright.gif
    LINKED | GTECH | NOTHINGBUTSHAREPOINT - BLOG AUTHOR

    "TRY NOT. DO. OR DO NOT. THERE IS NO TRY" - Yoda

  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    Xcluziv wrote: »
    Yea I'm sure...hey, this coming from a guy who started in c#....lol. DevilWAH, yea....im guessing thats why I was under the impression of them being alike with the similar (ish) interfaces....welp, learned something new icon_thumright.gif

    I know what you mean. I've never done any "non-managed" language. I started with Perl and then moved on to Java in the days of Java 1 and 2 but didn't really stick with it. So the only language I have learned really well is C#.
  • CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
    C is what I seem to know best. I don't have a problem using it. I don't like how java requires you to have the directory structure actually match your classes and packages (if thats the correct terminology). Thinking I'll probably learn python next though.
    Currently reading: Network Warrior, Unix Network Programming by Richard Stevens
  • paul78paul78 Member Posts: 3,016 ■■■■■■■■■■
    LOL! You would be proverbially crucified for such a statement in some circles!
    I was just thinking the same thing icon_lol.gif

    [quote=Xcluziv]started out with C# which is an up-to-date version on the C framework[/quote] The C# language isn't really like C. Languages like C will always be around for developing high-performance systems like operating systems, device drives, etc. The big difference as you noted - is memory management. C#, Java, Perl, etc. are all intepreted languanges with a garbage collector so you aren't as close to the metal as C.
    CodeBox wrote:
    C is what I seem to know best.
    I like C. Glad that you were able to figure out your problem. Personally, I have always felt that to reach a high proficiency in understanding how computing and networking systems work, it's helpful to know the low-level details. I always advocate for learning C and Assembly. It's a great way to gain experience to troubleshoot IT systems later on in life.
  • CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
    Whats more is that I was amazed that I actually fixed it using GDB! Never used GDB like i did on this one. A really good programmer ( I mean godly programmer ) suggested I learn to use GDB and it actually allowed me to fix my own problem.
    Currently reading: Network Warrior, Unix Network Programming by Richard Stevens
  • paul78paul78 Member Posts: 3,016 ■■■■■■■■■■
    CodeBlox wrote: »
    Whats more is that I was amazed that I actually fixed it using GDB! Never used GDB like i did on this one. A really good programmer ( I mean godly programmer ) suggested I learn to use GDB and it actually allowed me to fix my own problem.
    Despite his weirdness, I am a huge admirer or Richard Stallman - :D

    GDB is a wonderful debugger.If you use Emacs as your editor, try using GDB from gdb-mode if haven't tried it yet.

    Although, the newer integrated development environments from Microsoft are pretty slick too.

    But once you learn a debugger like GDB - you can really appreciate the hard-work that goes into environments like Eclipse and Visual Studio.
  • CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
    I'm currently using emacs. I've been compiling in GDB using GCC. I had forgot what the command was to use GDB from emacs and was too lazy to look it up, lol. Thanks! Will give it a try.

    Everything I've tried (projects )has been in C. I tried writing a 6502 emulator about a year ago and got overwhelmed and never finished. I feel like I could probably take on the project again and complete it this time though. There is a lot to learn about the language.
    Currently reading: Network Warrior, Unix Network Programming by Richard Stevens
  • DevilWAHDevilWAH Member Posts: 2,997 ■■■■■■■■□□
    I Really wish I had the time to delve in to the depths of Programming, trying to study Cisco security at the moment and just don't have the time to learn all the subilities of a language.

    Although I use c# and VBA a lot really these feel like scripting languages more than "real" programming, I hope one day I have the time and a project to really get my teeth in to it.

    Question, can you install installshield Light in to visual studio express?? I using visual studi 11 beta at the moment.
    • If you can't explain it simply, you don't understand it well enough. Albert Einstein
    • An arrow can only be shot by pulling it backward. So when life is dragging you back with difficulties. It means that its going to launch you into something great. So just focus and keep aiming.
Sign In or Register to comment.