Sponsored Content
Top Forums Programming Store file into a buffer to send it through a socket Post 302347472 by semash! on Tuesday 25th of August 2009 07:43:46 PM
Old 08-25-2009
Hey achenle, thanks for the reply,
I did the code as following:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <linux/if_ether.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
if(argc !=3){
printf("Usage: %s <iface> <raw filename>\n", argv[0]);
exit(1);
}

int sfd,ffd;
ssize_t bytes;
unsigned char *buffer;
struct stat sizb;
struct sockaddr from;
from.sa_family = 1;
strcpy(from.sa_data, argv[1]);

if((stat(argv[2],&sizb))!=0){
perror("stat() failed: ");
exit(1);
}

buffer=(unsigned char *)malloc(sizb.st_size);
if((ffd=open(argv[2], O_RDONLY))<0){
perror("open() failed: ");
exit(1);
}

if((bytes = read(ffd, buffer, sizb.st_size))<= 0){
perror("read() failed: ");
close(ffd);
exit(1);
} else {
close(ffd);
}

if((sfd=socket(AF_INET,SOCK_PACKET,ntohs(ETH_P_ALL)))<0){
perror("socket() failed: ");
exit(1);
}

if((sendto(sfd, buffer, sizb.st_size,0,(struct sockaddr *)&from,sizeof(from)))<0){
perror("send() failed: ");
exit(1);
}

printf("%i bytes transmitted.\n", sizb.st_size);
close(sfd);
return 0;
}

If you create a file with a hex editor, it will successfully transmit to the medium the frame Smilie.

I'll test your way, thank you.
 

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
COPY_FILE_RANGE(2)					     Linux Programmer's Manual						COPY_FILE_RANGE(2)

NAME
copy_file_range - Copy a range of data from one file to another SYNOPSIS
#define _GNU_SOURCE #include <unistd.h> ssize_t copy_file_range(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags); DESCRIPTION
The copy_file_range() system call performs an in-kernel copy between two file descriptors without the additional cost of transferring data from the kernel to user space and then back into the kernel. It copies up to len bytes of data from file descriptor fd_in to file descrip- tor fd_out, overwriting any data that exists within the requested range of the target file. The following semantics apply for off_in, and similar statements apply to off_out: * If off_in is NULL, then bytes are read from fd_in starting from the file offset, and the file offset is adjusted by the number of bytes copied. * If off_in is not NULL, then off_in must point to a buffer that specifies the starting offset where bytes from fd_in will be read. The file offset of fd_in is not changed, but off_in is adjusted appropriately. The flags argument is provided to allow for future extensions and currently must be to 0. RETURN VALUE
Upon successful completion, copy_file_range() will return the number of bytes copied between files. This could be less than the length originally requested. On error, copy_file_range() returns -1 and errno is set to indicate the error. ERRORS
EBADF One or more file descriptors are not valid; or fd_in is not open for reading; or fd_out is not open for writing; or the O_APPEND flag is set for the open file description referred to by fd_out. EFBIG An attempt was made to write a file that exceeds the implementation-defined maximum file size or the process's file size limit, or to write at a position past the maximum allowed offset. EINVAL Requested range extends beyond the end of the source file; or the flags argument is not 0. EIO A low-level I/O error occurred while copying. EISDIR fd_in or fd_out refers to a directory. ENOMEM Out of memory. ENOSPC There is not enough space on the target filesystem to complete the copy. EXDEV The files referred to by file_in and file_out are not on the same mounted filesystem. VERSIONS
The copy_file_range() system call first appeared in Linux 4.5, but glibc 2.27 provides a user-space emulation when it is not available. CONFORMING TO
The copy_file_range() system call is a nonstandard Linux and GNU extension. NOTES
If file_in is a sparse file, then copy_file_range() may expand any holes existing in the requested range. Users may benefit from calling copy_file_range() in a loop, and using the lseek(2) SEEK_DATA and SEEK_HOLE operations to find the locations of data segments. copy_file_range() gives filesystems an opportunity to implement "copy acceleration" techniques, such as the use of reflinks (i.e., two or more i-nodes that share pointers to the same copy-on-write disk blocks) or server-side-copy (in the case of NFS). EXAMPLE
#define _GNU_SOURCE #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/syscall.h> #include <unistd.h> /* On versions of glibc before 2.27, we must invoke copy_file_range() using syscall(2) */ static loff_t copy_file_range(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out, size_t len, unsigned int flags) { return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags); } int main(int argc, char **argv) { int fd_in, fd_out; struct stat stat; loff_t len, ret; if (argc != 3) { fprintf(stderr, "Usage: %s <source> <destination> ", argv[0]); exit(EXIT_FAILURE); } fd_in = open(argv[1], O_RDONLY); if (fd_in == -1) { perror("open (argv[1])"); exit(EXIT_FAILURE); } if (fstat(fd_in, &stat) == -1) { perror("fstat"); exit(EXIT_FAILURE); } len = stat.st_size; fd_out = open(argv[2], O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd_out == -1) { perror("open (argv[2])"); exit(EXIT_FAILURE); } do { ret = copy_file_range(fd_in, NULL, fd_out, NULL, len, 0); if (ret == -1) { perror("copy_file_range"); exit(EXIT_FAILURE); } len -= ret; } while (len > 0); close(fd_in); close(fd_out); exit(EXIT_SUCCESS); } SEE ALSO
lseek(2), sendfile(2), splice(2) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. Linux 2018-02-02 COPY_FILE_RANGE(2)
All times are GMT -4. The time now is 03:50 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy