Sponsored Content
Full Discussion: File Transfer over Sockets
Top Forums Programming File Transfer over Sockets Post 302274438 by Corona688 on Wednesday 7th of January 2009 02:51:16 PM
Old 01-07-2009
Okay. UNIX uses the open(), read() and write() calls to deal with files, and the recv() and send() calls to deal with sockets. It uses the socket() and bind() calls to create a server socket and give it an address, or the socket() and connect() calls to make and connect a client socket to something. close() works for both files and sockets. There's lots and lots of examples for these all over the internet, and probably manpages on your system, try 'man 2 socket'.

Code:
int send_file(int fd, int sock)
{
    char buf[512];
    ssize_t len;

    while( (len = read(fd, buf, 512)) > 0)
    {
      if(send(sock, buf, len, 0) != len)
      {
        perror("Couldn't send data:");
        return(-1);
      }
    }

  return(0);
}

which will work on UNIX systems in general. You mention Linux in particular, which has an especially easy and efficient way:
Code:
size_t len=512;

while(sendfile(sock, fd, NULL, len) == len);

Note that sendfile is special, it can only read from files and can only send to sockets.

See 'man 2 send', 'man 2 read', etc. for details on these functions like what include files they need.

Last edited by Corona688; 01-07-2009 at 04:00 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

Sockets and File descriptors

I am in a Systems programming class this semester, and our current project is to write a program utilizing sockets and fork. For the project, I decided to make my own instant messaging program. I have the code completed, but I have a problem that keeps old clients from communicating with new... (3 Replies)
Discussion started by: gstlouis
3 Replies

2. Shell Programming and Scripting

file transfer

hi all how do i copy a file from one server to another thanks bkan77 (4 Replies)
Discussion started by: bkan77
4 Replies

3. UNIX for Dummies Questions & Answers

Transfer the file

Dear all, Can anybody let me know how to automate a file transfer process to a remote m/c thru SFTP , automate means it will not prmpt for password. how i am going to achive this....and what all methods are available or tools are available ???? (2 Replies)
Discussion started by: manas_ranjan
2 Replies

4. Shell Programming and Scripting

File transfer

Hi All, it might not be an sound question, i have two server like A and B.. i want to transfer file from B to A ..here i have some questions.. 1) do we need to create private and public key to connect..and transferring files...from B to A..? 2) i tried with scp options like... (2 Replies)
Discussion started by: Shahul
2 Replies

5. Programming

Problem in file transfer using sockets

Hai Friends I am writing a c program to transfer files from one system to another using TCP/IP socket programming.. My Recieve Program #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> extern int errno; extern char *sys_erlist; void... (2 Replies)
Discussion started by: collins
2 Replies

6. Programming

Sending large file through sockets

Hello, I am trying to send a file (1 MB) through sockets and for some reason I am getting segmentation fault message and receiving in the server an incomplete file. The current file is about 3 pages long and need to send it from the client to the server. I've tried malloc with free(), I am now... (8 Replies)
Discussion started by: sfcddm1
8 Replies

7. Programming

File transfer in C

HI Can anyone provide me with codes for file transfer server to client or vice versa? Also please explain how to compile those programs in ubuntu terminal as i am totally new to socket programming. Thanks (0 Replies)
Discussion started by: mayhemtrigger
0 Replies

8. Shell Programming and Scripting

Avoiding file overwrite during file transfer using scp

Hi, I have written a small script to transfer a file from one unix server to other using scp command which is working fine. As I know with scp, if any file with the same name is already present on destination server, it would get overwritten without any notification to user. Could anyone help me... (14 Replies)
Discussion started by: dsa
14 Replies

9. Shell Programming and Scripting

File transfer script

Hi, I need a shell script to transfer a file from one server(unix box) to another server(windows box). I have the details of the source and destination Ip's. source path : /home/UNIX/server filename:abc.txt Destination folder: D:/UNIX/test I am using AIX server. Type of shell :... (1 Reply)
Discussion started by: NareshN
1 Replies

10. UNIX for Advanced & Expert Users

File transfer

When using FTP to transfer a file from IBM iSeries family of servers client to a non IBM Iseries family server, files might have characters appear in the wrong format Eg | in Iseries and while transferring fro Iseries system to Linux , but instead of | it is showing as ?. Please advise (3 Replies)
Discussion started by: sudhainit
3 Replies
recv(2) 							System Calls Manual							   recv(2)

Name
       recv, recvfrom, recvmsg - receive a message from a socket

Syntax
       #include <sys/types.h>
       #include <sys/socket.h>

       cc = recv(s, buf, len, flags)
       int cc, s;
       char *buf;
       int len, flags;

       cc = recvfrom(s, buf, len, flags, from, fromlen)
       int cc, s;
       char *buf;
       int len, flags;
       struct sockaddr *from;
       int *fromlen;

       cc = recvmsg(s, msg, flags)
       int cc, s;
       struct msghdr msg[];
       int flags;

Description
       The and system calls are used to receive messages from a socket.

       The call can be used only on a connected socket. The and calls can be used to receive data on a socket, whether or not it is in a connected
       state.  For further information, see

       If from is nonzero, the source address of the message is filled in.  The fromlen is a value-result parameter, initialized to  the  size	of
       the buffer associated with from, and modified on return to indicate the actual size of the address stored there.  The length of the message
       is returned in If a message is too long to fit in the supplied buffer, excess bytes can be discarded, depending on the type of  socket  the
       message is received from.  For further information, see

       If  no  messages  are  available  at  the socket, the receive call waits for a message to arrive, unless the socket is nonblocking.  If the
       socket is nonblocking, a of -1 is returned, and the external variable errno is set to EWOULDBLOCK.  For further information, see

       The call can be used to determine when more data arrives.

       The flags argument to a send call is formed by ORing one or more of the values following values:
       #define	 MSG_OOB   0x1	/* process out-of-band data */
       #define	 MSG_PEEK  0x2	/* peek at incoming message */
       The call uses a msghdr structure to minimize the number of directly supplied parameters.  This structure has the following form, as defined
       in <sys/socket.h>:
       struct msghdr {
	      caddr_t  msg_name;	/* optional address */
	      int      msg_namelen;	/* size of address */
	      struct   iov *msg_iov;	/* scatter/gather array */
	      int      msg_iovlen;	/* # elements in msg_iov */
	      caddr_t  msg_accrights;	/* access rights sent/received */
	      int      msg_accrightslen;
       };
       Here,  msg_name and msg_namelen specify the destination address if the socket is unconnected; msg_name can be given as a null pointer if no
       names are desired or required.  The msg_iov and msg_iovlen describe the scatter gather locations, as described in Access rights to be  sent
       along with the message are specified in msg_accrights , which has length msg_accrightslen .

Return Values
       These calls return the number of bytes received, or -1 if an error occurred.

Diagnostics
       The call fails under the following conditions:

       [EBADF]	      The argument s is an invalid descriptor.

       [EINVAL]       The argument length of the message is less than 0.

       [EMSGSIZE]     The message sent on the socket was larger than the internal message buffer.

       [ENOTCONN]     A call was made to from an unconnected stream socket.

       [ENOTSOCK]     The argument s is not a socket.

       [EWOULDBLOCK]  The socket is marked nonblocking and the receive operation would block.

       [EINTR]	      The receive was interrupted by delivery of a signal before any data was available for the receive.

       [EFAULT]       The  data  was  specified  to  be  received  into a nonexistent or protected part of the process address space. The argument
		      fromlen points outside the process address space.

See Also
       read(2), send(2), socket(2)

																	   recv(2)
All times are GMT -4. The time now is 12:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy