I hope to post in the right place, otherwise I apologize for this
I wrote a UDP sender and a UDP receiver, my goal is to transmit a file from th sender to the receiver. I'm using Ubuntu 8.04
Compiling goes well but when I execute the sender, its "sendto" gives me the error "Permission denied" . What's wrong? I ran the same code on fedora 9 with no problem.
Here's the code, thank you in advance (I 've reported the receive too for the sake of completeness)
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_)<img src="images/smilies/frown.gif" border="0" alt="">_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){ <-here I've problem
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))) <--here Ive problem
perror("Unable to send data");
}
fclose(fin);
}else{
printf("Unable to open file");
}
close(sd);
}
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_)<img src="images/smilies/frown.gif" border="0" alt="">_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;
}