Sponsored Content
Top Forums Programming Store file into a buffer to send it through a socket Post 302347468 by achenle on Tuesday 25th of August 2009 07:20:02 PM
Old 08-25-2009
You can also use "sendfile()". That's a lot more efficient because it cuts the number of memory copies by half.

When you read data from a file into the address space of a process, usually the data is copied into a kernel buffer as it comes in off disk, and it get copied again from that kernel buffer into the process buffer. To send that data out a socket, the data is first copied into a kernel buffer, and the data in that buffer is copied out to the socket.

The sendfile() library call will bypass the copying of data into and out of the address space of the process.

For example (I left off all error checking for clarity):

Code:
    struct stat sb;
    off_t offset = 0L;
    int fd;

    fd = open( filename, O_RDONLY );
    fstat( fd, &sb );
    sendfile( socket_fd, fd, &offset, sb.st_size );
    close( fd );

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

What is my UDP send/recieve buffer size

Hi, If some one was to suggest, "increase your kernal tunables related to UDP, in particular the UDP send/recieve buffer size".... then what would they mean? :confused: How can I find out what this current value is? Thousand many thanks. Neil (3 Replies)
Discussion started by: nhatch
3 Replies

2. Shell Programming and Scripting

send function in socket

Hi All, I encountered a stange problem while doing a perl script to use socket. i need to transfer a file from client to sever. but error came as argument missing in send function.........Plz tell me the wt r the arguments in send and recv functions....... (0 Replies)
Discussion started by: trupti_rinku
0 Replies

3. Programming

UDP socket - can both client and server recv and send

Hi, Am very new to socket programming. When we use UDP sockets to communicate between two processess, will both the client/server socket be able to send/recv ? meaning can sendto()/ recvfrom() be used on both server and client? It could be useful even if anybody provide some link on socket... (1 Reply)
Discussion started by: rvan
1 Replies

4. UNIX for Advanced & Expert Users

connect problem for sctp socket (ipv6 socket) - Runtime fail Invalid Arguments

Hi, I was porting ipv4 application to ipv6; i was done with TCP transports. Now i am facing problem with SCTp transport at runtime. To test SCTP transport I am using following server and client socket programs. Server program runs fine, but client program fails giving Invalid Arguments for... (0 Replies)
Discussion started by: chandrutiptur
0 Replies

5. Programming

Send/Receive buffer size??

Dear friends, How do I find the TCP send and receive buffer size? (1 Reply)
Discussion started by: nagalenoj
1 Replies

6. Programming

Socket Programming Send File

Hello my friends; Look at this 2 program: Client: #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> int main ( int agrc, char *argv ) { int Socket; struct sockaddr_in... (5 Replies)
Discussion started by: htabesh
5 Replies

7. Programming

Finding used socket receive-buffer size

I have set the receive buffer size of socket to max. setsockopt(sd,SOL_SOCKET, SO_RCVBUF,&max,optval); Am reading data from the socket in a loop(say max 100 bytes per recv) while(1) { int rlen=recv(sd,(void *)buf, 100 , 0); //err handle and processing } Assume my process is slow... (2 Replies)
Discussion started by: johnbach
2 Replies

8. Programming

how can I send and receive data in client server socket programing

char name; printf ("Welcome to the server \n"); printf ("Enter user name: \n"); scanf ("%c", &name); how can client send name to server:what should be the code? int send ( int sid , const char ∗buffer Ptr , int len , int f l a g ) how can client receive ack from... (1 Reply)
Discussion started by: saiful_911
1 Replies

9. AIX

Sample C program to Send/Recieve a file using Socket

Hi All, I urgently need a Sample C program to Send/Recieve a file using Socket. Thanks Sara (1 Reply)
Discussion started by: saraperu
1 Replies

10. Programming

How to avoid 'No buffer space available' on C socket?

Hello everybody, Years ago i left in stand-by a project of mine where the main program was supposed to send thousands ARP frames over the socket as fast as it could; but because of a programming issue i couldn't continue it. 2 days ago I decided to solve that issue. The thing is, when the... (4 Replies)
Discussion started by: Zykl0n-B
4 Replies
SENDFILE(2)						     Linux Programmer's Manual						       SENDFILE(2)

NAME
sendfile - transfer data between file descriptors SYNOPSIS
#include <sys/sendfile.h> ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count); DESCRIPTION
sendfile() copies data between one file descriptor and another. Because this copying is done within the kernel, sendfile() is more effi- cient than the combination of read(2) and write(2), which would require transferring data to and from user space. in_fd should be a file descriptor opened for reading and out_fd should be a descriptor opened for writing. If offset is not NULL, then it points to a variable holding the file offset from which sendfile() will start reading data from in_fd. When sendfile() returns, this variable will be set to the offset of the byte following the last byte that was read. If offset is not NULL, then sendfile() does not modify the current file offset of in_fd; otherwise the current file offset is adjusted to reflect the number of bytes read from in_fd. If offset is NULL, then data will be read from in_fd starting at the current file offset, and the file offset will be updated by the call. count is the number of bytes to copy between the file descriptors. Presently (Linux 2.6.9): in_fd, must correspond to a file which supports mmap(2)-like operations (i.e., it cannot be a socket); and out_fd must refer to a socket. Applications may wish to fall back to read(2)/write(2) in the case where sendfile() fails with EINVAL or ENOSYS. RETURN VALUE
If the transfer was successful, the number of bytes written to out_fd is returned. On error, -1 is returned, and errno is set appropri- ately. ERRORS
EAGAIN Nonblocking I/O has been selected using O_NONBLOCK and the write would block. EBADF The input file was not opened for reading or the output file was not opened for writing. EFAULT Bad address. EINVAL Descriptor is not valid or locked, or an mmap(2)-like operation is not available for in_fd. EIO Unspecified error while reading from in_fd. ENOMEM Insufficient memory to read from in_fd. VERSIONS
sendfile() is a new feature in Linux 2.2. The include file <sys/sendfile.h> is present since glibc 2.1. CONFORMING TO
Not specified in POSIX.1-2001, or other standards. Other Unix systems implement sendfile() with different semantics and prototypes. It should not be used in portable programs. NOTES
If you plan to use sendfile() for sending files to a TCP socket, but need to send some header data in front of the file contents, you will find it useful to employ the TCP_CORK option, described in tcp(7), to minimize the number of packets and to tune performance. In Linux 2.4 and earlier, out_fd could refer to a regular file, and sendfile() changed the current offset of that file. SEE ALSO
mmap(2), open(2), socket(2), splice(2) COLOPHON
This page is part of release 3.27 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2010-02-15 SENDFILE(2)
All times are GMT -4. The time now is 04:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy