Sending large file through sockets
Hello, I am trying to send a file (1 MB) through sockets and for some reason I am getting segmentation fault message and receiving in the server an incomplete file. The current file is about 3 pages long and need to send it from the client to the server. I've tried malloc with free(), I am now trying the read stream and write stream, with the fdopen and the flush at the end. I'm new with Linux and didnt know where to place my question.
I guess my question(s) are: how can I send the complete file to the server? Is there another function in c that I can use?
The global variables are declared elsewhere.
Thank you
Here is the code:
#include "sockheaders.h"
void bail(const char *on_what) {
if ( errno != 0 ) {
fputs(strerror(errno),stderr);
fputs(": ",stderr);
}
fputs(on_what,stderr);
fputc('\n',stderr);
exit(1);
}
int create_server_socket(char *srvr_addr, char *srvr_port){
/* srvr_addr = NULL;
srvr_port = "9099";
srvr_addr = "127.0.0.1";*/
/*Create a TDP/IP socket to use : */
s = socket(PF_INET,SOCK_STREAM,0);
if ( s == -1 ){ bail("socket()");}
/* Create a server socket address: */
memset(&adr_srvr,0,sizeof adr_srvr);
adr_srvr.sin_family = AF_INET;
adr_srvr.sin_port = htons(atoi(srvr_port));
if ( strcmp(srvr_addr,"*") != 0 ) {
/* Normal Address */
adr_srvr.sin_addr.s_addr = inet_addr(srvr_addr);
if ( adr_srvr.sin_addr.s_addr == INADDR_NONE )
bail("bad address."); }
else { adr_srvr.sin_addr.s_addr = INADDR_ANY;} /* Wild Address */
/*Bind the server address: */
len_inet = sizeof adr_srvr;
z = bind(s,(struct sockaddr *)&adr_srvr,len_inet);
if ( z == -1 ) { bail("bind(2)");}
/* Make it a listening socket: */
z = listen(s,10);
if ( z == -1 ) { bail("listen(2)");}
//FILE *rx; /* read stream */
//FILE *tx; /* write stream */
//s = socket(PF_INET,SOCK_STREAM,0);
srx = fdopen(s,"r");
if ( srx == NULL ) {
fprintf(stderr, "%s: fdopen(s,'r')\n",strerror(errno));
exit(1);
}
stx = fdopen(dup(s),"w");
if ( stx == NULL ) {
fprintf(stderr,"%s: fdopen(s,'w')\n",strerror(errno));
exit(1);
}
setlinebuf(srx); /* Line Buffered Mode */
setlinebuf(stx); /* Line Buffered Mode */
return 1;
}
send_server_message(){
strcpy(dtbuf,"<html><bold> I am the server!</bold></html>");
/*##################################################################*/
/*-----------------connection section-------------------------------*/
/*##################################################################*/
/* Wait for a connect : */
len_inet = sizeof adr_clnt;
c = accept(s,(struct sockaddr *)&adr_clnt,&len_inet);
if ( c == -1 ){ printf("problem with accept\n");}
/*--------- recieve message----------*/
recieved[0]=0;
z = read(c,&recieved,sizeof recieved -1);
if ( z == -1 ){ printf("cannot read from socket 001\0");}
printf("int is: %s\n",recieved);
/*---------Write result back to the client---------*/
printf(" sending the following %s\n",dtbuf);
z = write(c,&dtbuf,sizeof dtbuf);//changed by coe
if ( z == -1 ){ printf("unable to write to socket");}
/*CLOSE CLIENT CONNECTION*/
close(c);
return 1;
}
int create_client( char *srvr_addr1, char *srvr_port1){
/*##########################################################*/
/*--------------connection1-setup---------------------------*/
/*##########################################################*/
/*srvr_addr1 = NULL;
srvr_port1 = "9099";
srvr_addr1 = "127.0.0.1"; */
s1 = socket(PF_INET,SOCK_STREAM,0);
if ( s1 == -1 ){/*bail("socket()");*/}
/*Create a server socket address: */
memset(&adr_srvr1,0,sizeof adr_srvr1);
adr_srvr1.sin_family = AF_INET;
adr_srvr1.sin_port = htons(atoi(srvr_port1));
adr_srvr1.sin_addr.s_addr = inet_addr(srvr_addr1);
if ( adr_srvr1.sin_addr.s_addr == INADDR_NONE ){/*bail("bad address.");*/}
/*Connect to the server: */
len_inet1 = sizeof adr_srvr1;
/*create the connection*/
z1 = connect(s1,&adr_srvr1,len_inet1);
if ( z1 == -1 ){ /*bail("connect(2)");*/}
//FILE *rx; /* read stream */
//FILE *tx; /* write stream */
//s = socket(PF_INET,SOCK_STREAM,0);
crx = fdopen(s1,"r");
if ( crx == NULL ) {
fprintf(stderr, "%s: fdopen(s,'r')\n",strerror(errno));
exit(1);
}
ctx = fdopen(dup(s1),"w");
if ( ctx == NULL ) {
fprintf(stderr,"%s: fdopen(s,'w')\n",strerror(errno));
exit(1);
}
setlinebuf(crx); /* Line Buffered Mode */
setlinebuf(ctx); /* Line Buffered Mode */
return 1;
}
int send_message(){
/*##########################################################*/
/*--------------connection1-message-------------------------*/
/*##########################################################*/
strcpy(dtbuf,"I am the client!");
printf("sending: %s\n",dtbuf);
z1= send_File_To_Server();
if ( z1 == -1 ){ /*bail("write(2)");*/}
z1= read(s1,&dtbuf,sizeof dtbuf -1);
printf("data recieved is %s",dtbuf);
close(s1);
putchar('\n');
}
int send_File_To_Server(){
FILE *ptr;
ptr = fopen("config.c", "r"); //open file
crx = fdopen(c, "r");
if (!crx){
close(c);
//continue;
}
ctx = fdopen(dup(c), "w");
if (!ctx){
close(c);
//continue;
}
srx = fdopen(s1, "r");
if (!srx){
close(s1);
//continue;
}
stx = fdopen(dup(s1), "w");
if (!stx){
close(s1);
//continue;
}
setlinebuf(srx); /* Line Buffered Mode */
setlinebuf(stx); /* Line Buffered Mode */
int ch;
int Pos,Length;
Pos = ftell(ptr);
fseek(ptr, 0L, SEEK_END);
Length = ftell(ptr);
fseek(ptr,Pos, SEEK_SET);
char temp[1048576];
int i=0;
while ((ch = getc(ptr)) != EOF/*!feof(ptr)*/){
temp[i]=ch;
i++;
} //prints everything from the file into the console
fputs(temp, stx);//to the server
fflush(stx);
}