The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > Linux
Google UNIX.COM


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Permission denied big123456 UNIX for Advanced & Expert Users 1 10-10-2007 04:17 AM
rsh permission denied lweegp Shell Programming and Scripting 7 09-15-2006 01:36 AM
Permission Denied howeezy UNIX for Dummies Questions & Answers 0 09-14-2005 06:52 AM
./ Permission Denied. trouscaillon UNIX for Dummies Questions & Answers 8 01-26-2005 05:33 PM
Permission Denied using VI on my NFS jennifer UNIX for Dummies Questions & Answers 4 08-08-2001 03:34 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-12-2008
Registered User
 

Join Date: Apr 2008
Posts: 30
Stumble this Post!
Permission denied with sendto in Ubuntu

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)

Code:
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);   
}

Code:
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;

}
Reply With Quote
Google The UNIX and Linux Forums
Forum Sponsor
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0