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.

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