Ciao ragazzi,
ho scritto un server ed un receiver entrambi su protocollo UDP. L'obietto è di fare inviare un file dal sender al receiver.
La compilazione va a buon fine ma quando invio dei pacchetti al receiver mi viene dato un errore di segmentation fault in corrispondenza di una fwrite che potete vedere nel codice.
Per favore qualcuno può darmi una mano (vi riporto anche il codice del sender) .
GRAzie
Codice PHP:
sender..c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define PORT 5500
#define BUFFSIZE 100
#define MIN(_x_,_y_)(((_x_)<(_y_) )? (_x_):(_y_))
int main (int argc, char* argv){
FILE* fin;
int sd; /*socket descriptor*/
struct sockaddr_in dest;
char buf[BUFFSIZE]; /*buffer for socket*/
unsigned long length, file_size, encoded_file_size, chunk_size;
memset((char *)&dest, 0, sizeof(dest));
dest.sin_family = PF_INET;
dest.sin_port = htons(PORT);
dest.sin_addr.s_addr = inet_addr("10.0.0.201"); /*destination's address*/
if ((sd = socket(PF_INET,SOCK_DGRAM,0))<0) {
perror("socket creation");
}
fin = fopen("/home/dileo/prova.txt","rb");
if (fin != NULL){
fseek(fin,0, SEEK_END);
file_size = ftell(fin);
fseek(fin,0, SEEK_SET);
encoded_file_size = htonl(file_size); /*send dim file*/
if(sendto(sd, &encoded_file_size, sizeof(encoded_file_size), 0, (struct sockaddr*)&dest, sizeof(dest))<0){
perror("send to error");
}
for (length=0; length < file_size; length += chunk_size ){
chunk_size = MIN(BUFFSIZE, file_size-length);
fread(buf, chunk_size,1, fin); /*Check*/
if (chunk_size != sendto(sd, buf, chunk_size,0,(struct sockaddr*)&dest, sizeof(dest)))
perror("Unable to send data");
}
fclose(fin);
}else{
printf("Unable to open file");
}
close(sd);
}
Codice PHP:
RECEIVER.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define BUFSIZE 5000
#define DEBUG 1
#define PORT 5500 /*receiver's port*/
#define MIN(_x_,_y_)(((_x_)<(_y_) )? (_x_):(_y_))
int main (int argc, char** argv){
int sd, sfamily, stype, sprotocol;
struct sockaddr_in receiver, their_addr;
socklen_t their_addr_len;
unsigned long file_size;
memset((char *)&receiver, 0, sizeof(receiver)); /*clean */
receiver.sin_family = PF_INET;
receiver.sin_port = htons(PORT);
receiver.sin_addr.s_addr = INADDR_ANY;
if ((sd = socket(PF_INET, SOCK_DGRAM, 0))<0)
perror("socket creation error");
if (bind(sd, (struct sockaddr*)& receiver, sizeof(receiver))){
perror("bind error");
exit(1);
}
if (recvfrom(sd,&file_size, sizeof(unsigned long),0, (struct sockaddr*)&their_addr, their_addr_len)<0){
perror("receive error");
}
printf("received packet from %s\n ", inet_ntoa(their_addr.sin_addr));
printf("packet contains \"%i\"\n",ntohl(file_size));
FILE *fout = fopen("/home/pippo.txt","wb");
char buffer[BUFSIZE];
unsigned long length, chunk_size;
for (length = 0; length < file_size; length += chunk_size){
#ifdef DEBUG
printf("length %i\n", length);
#endif
chunk_size = recvfrom (sd, &buffer, MIN(BUFSIZE, file_size-length),0,NULL, NULL);
#ifdef DEBUG
printf("chunk_size %i\n", chunk_size);
#endif
if (!chunk_size){
printf("connection unexpdeately terminated");
close(sd);
}
fwrite(buffer, chunk_size,1, fout); /*<--questa fwrite è reposnsabile segment fault */
}
fclose(fout);
close(sd);
return 0;
}