Thread: send zip file via socket

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    send zip file via socket

    Hi,
    the platform I am writing the code on is unix based, command line stuff.
    I am more accustom to writing in c++ but my current task requires I alter a c program.

    I am dropping the data from file to a socket. I read in line by line the entire contents of a file.

    Problem is, the send is taking too long, since the file is so large. I need to zip the file and send the contents in a copressed manner. However, I don't know of any means to read from a compressed file, in a line by line manner.

    So I decided to just attempt a full file send of the zip file to the destination and then uncompress it. I know how to do a remove file using stdio.h remove() but, I was wondering if there was anyway to send a file in c to a destination address using socke to socket addressing?

    In c++ I can send files to different drives but this is unique to me.

    I'm sorry I know this is vague, I'm just trying to find a good starting point. Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    A zip file can't realy be read in line by line because it is binary data. It probably contains ocurances of the new line charactor but these are not intended to indicate newlines. You can use the sendfile function.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    do tcp socket work on a text based parsing?? or simply let the programmer decide what's the end or newline or not? i'd bet the second....

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by xErath
    do tcp socket work on a text based parsing?? or simply let the programmer decide what's the end or newline or not? i'd bet the second....
    tcp sockets are a byte stream, they will send however many bytes you specify without doing any checking of what those bytes are.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    164
    Thanks everyone for your replies and sorry for my late response, I have not been online in a few days. I am researching the sendfile function listed by quantum, the compressed file comment is what I always knew about compressed files, so I did not think that would be possible to read in line by line, thanks for confirming.

    Is the sendfile function tcp/ip based? I trying to find the header file where it is defined?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is this what you were looking for?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    164
    Thanks for the link, I am trying to read it now, I am having issues with my companies web filtering, Too many spyware attacks recently

    Is there another way to view this info? Sorry to ask such a question, desprate measures.... not trying to be a pain.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    On a unix based system try:
    man sendfile

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    164
    awesome, I am reading the man now. Thanks! I hope I will not have to bother anyone else on the matter. Thank you for the pointer.

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    164
    well, I think I am almost there.... I think....

    I have to compilation errors based of the structs being created, however, since I using the examples from the senfile man page to base my function off of, I am bit confused as to why the compiler is unhappy.

    here is the function:
    Code:
    int Sndfl ()
    {
    	 int ffd, tfd;
         off_t off;
         struct sockaddr_in sin;
         in_addr_t  addr;
         int len;
         struct stat stat_buf;
    
             ffd = open(BARCODE_TABLE, O_RDONLY);
             if (ffd == -1) {
                 perror("open");
                 exit(1);
             }
    
             tfd = socket(AF_INET, SOCK_STREAM, 0);
             if (tfd == -1) {
                 perror("socket");
                 exit(1);
             }
    
             sin.sin_family = AF_INET;
             sin.sin_addr = INADDR_ANY;    /* Fill in the  appropriate address. */
             sin.sin_port = htons(1130);
             if (connect(tfd, (struct sockaddr *) &sin, sizeof(sin)) <0) {
                 perror("connect");
                 exit(1);
             }
    
             if (fstat(ffd, &stat_buf) == -1) {
                 perror("fstat");
                 exit(1);
             }
    
             len = sendfile(tfd, ffd, &off, stat_buf.st_size);
    
             if  (len  ==  -1)  {
                 perror("sendfile");
                 exit(1);
             }
    }
    Here are my errors
    Code:
    utilserv.c: In function `Sndfl':
    utilserv.c:115: error: storage size of `stat_buf' isn't known
    utilserv.c:130: error: incompatible types in assignment
    I am guessing that I have not used this example properly and did not tell the compiler how much buffer space to give stat_buf??

    Again, thanks for any help available.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    struct stat doesn't exist according to error #2.

    The other error on the line:
    Code:
    sin.sin_addr = INADDR_ANY
    is because INADDR_ANY is not of the same type as sin.sin_addr, I think anyway.

    dwk

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    164
    hmmm... ok, that makes sense

    I am used to using such and IP address setup for windows, using winsock, not quite accustom to unix c coding using socket.h,
    Thanks for the pointers, I will define my struct and look at using something different for my ip address.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM