Transfer files using sockets, urgent!!
- From: petekevin@xxxxxxxxx
- Date: 7 Mar 2006 15:53:00 -0800
Hi
I wanted to transfer large files between a client and server back and
forth using TCP connection
(though the file size coming in one direction is only large, the data
coming
from the other direction is just a few parameters). Right now, i am
using the following
code send..c and recv.c to send and recieve the file at both ends. Now
my questions
to all the experts in socket programming:
1. Can I have another way of transfering the smaller data (which is
just a few parameters in another way) to fasten up things
2. The speed needed is also not that upto the mark, i need to transfer
some large files (from server
to client. )
in the order of Gigabytes, so can I further modify these programs and
make it much faster.
I have been looking at things like Multisocket, tuning the buffer size
etc, does that do any good.
If so, any references for them would be appreciated
This is the first time I am posting on google groups, so would expect
some
positive results.......
Here is the code .....
/*************************************** send.c
********************************/
/* actually the send.c and recv.c do things exactly opposite to their
names but anyhow
the nomenclature should not be a problem to change */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <signal.h>
#include <sys/file.h>
#include <string.h>
#include <sys/times.h>
#include <unistd.h>
/* #include <iostream> */
#define DEBUG
#define DEF_PORTNO 1818
#define BUFSZ 10240 /* 10 KB buf */
#define STRLEN 80
#define FD_STDIN 0
#define FD_STDOUT 1
#define FD_STDERR 2
/* using namespace std; */
static char *progname;
void Usage()
{
fprintf(stderr, "Usage: %s [-v] [-p port_number] [filename]\n",
progname);
exit(-1);
}
int main(int argc, char *argv[])
{
int sock, s;
struct sockaddr_in server;
int portno = DEF_PORTNO;
int read_sz, c;
int out_fd = FD_STDOUT;
char buf[BUFSZ];
char filename[STRLEN];
unsigned long xfersz;
int verbose = 0;
extern char *optarg;
extern int optind;
/* struct tms tmsstart, tmsend;
time_t startingTime, endingTime;
startingTime = times(&tmsstart); */
progname = argv[0];
filename[0] = '\0';
while ((c = getopt(argc, argv, "vp:")) != EOF) {
switch (c) {
case 'p':
portno = atoi(optarg);
if (portno <= 1024)
Usage();
break;
case 'v':
verbose++;
break;
case '?':
default:
Usage();
break;
}
}
if (optind == argc - 1) {
strcpy(filename, argv[optind]);
} else if (optind != argc)
Usage();
fprintf(stderr, "portno = %d\n", portno);
fprintf(stderr, "filename = %s\n",
filename[0] != '\0' ? filename : "Standard output");
if (filename[0] != '\0') {
out_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0755);
if (out_fd < 0) {
fprintf(stderr, "Cannot create output file %s; exiting\n",
filename);
return -1;
}
}
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
fprintf(stderr, "socket() failed: exiting");
return -1;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(portno);
if (bind(sock, (struct sockaddr *) & server, sizeof(server)) < 0) {
fprintf(stderr, "bind() failed: exiting\n");
return -1;
}
listen(sock, 1);
fprintf(stderr, "Waiting for connection request...\n");
s = accept(sock, (struct sockaddr *) 0, (unsigned int *) 0);
if (s == -1) {
fprintf(stderr, "accept() failed: exiting\n");
return -1;
} else {
close(sock);
fprintf(stderr, "Connected...\n");
}
fprintf(stderr, "Receving data .\r");
fflush(stderr);
while (1) {
read_sz = read(s, buf, BUFSZ);
if (read_sz <= 0) {
fprintf(stderr, "\nRemote client close connection\n");
fprintf(stderr, "\n%ld bytes received\n", xfersz);
close(s);
close(out_fd);
/* endingTime = times(&tmsend);
printf ("\n Watch out for the time %f :\n", (endingTime -
startingTime)/( (double)(sysconf(_SC_CLK_TCK)) ) ); */
return 0;
}
if (write(out_fd, buf, read_sz) != read_sz) {
fprintf(stderr, "\nwrite() failed: exiting\n");
close(s);
close(out_fd);
return -1;
}
xfersz += read_sz;
if (verbose) {
fprintf(stderr, "Receiving data %ld bytes ...\r", xfersz);
fflush(stderr);
}
}
return(0);
}
/********************************************** recv.c
***************************************/
/* recv.c */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <signal.h>
#include <sys/file.h>
#include <string.h>
#include <sys/times.h>
#include <unistd.h>
#define DEBUG
#define DEF_PORTNO 1818
#define BUFSZ 10240 /* 10 KB buf */
#define STRLEN 80
#define FD_STDIN 0
#define FD_STDOUT 1
#define FD_STDERR 2
static char *progname;
#ifdef SYSV
#define bzero(x, l)memset(x, 0, l)
#define bcopy(s, d, l)memcpy(d, s, l)
#endif
void
Usage ()
{
fprintf (stderr,
"Usage: %s [-v] [-p port_number] remote_host [filename]\n",
progname);
exit (-1);
}
main (argc, argv)
int argc;
char **argv;
{
typedef unsigned char uchar;
int sock;
struct sockaddr_in client;
int portno = DEF_PORTNO;
int read_sz, c;
int in_fd = FD_STDIN;
char buf[BUFSZ];
char filename[STRLEN];
char host[STRLEN];
int verbose = 0;
unsigned long xfersz;
struct hostent *h;
extern char *optarg;
extern int optind;
struct tms tmsstart, tmsend;
time_t startingTime, endingTime;
startingTime = times(&tmsstart);
progname = argv[0];
filename[0] = '\0';
host[0] = '\0';
while ((c = getopt (argc, argv, "vp:")) != EOF)
{
switch (c)
{
case 'p':
portno = atoi (optarg);
if (portno <= 1024)
Usage ();
break;
case 'v':
verbose++;
break;
case '?':
default:
Usage ();
break;
}
}
if (optind < argc)
{
strcpy (host, argv[optind++]);
}
if (optind < argc)
{
strcpy (filename, argv[optind++]);
}
if (optind < argc)
{
Usage ();
}
if (host[0] == '\0')
Usage ();
fprintf (stderr, "portno = %d\n", portno);
fprintf (stderr, "host = %s\n", host);
fprintf (stderr, "filename = %s\n",
filename[0] != '\0' ? filename : "Standard Input");
if (filename[0] != '\0')
{
in_fd = open (filename, O_RDONLY);
if (in_fd < 0)
{
fprintf (stderr, "Cannot open file %s; exiting\n", filename);
return -1;
}
}
if ((h = gethostbyname(host)) == NULL)
{
fprintf (stderr, "Unknown host [%s]", host);
return -1;
}
bzero (&client, sizeof (client));
client.sin_port = portno;
client.sin_family = h->h_addrtype;
bcopy (h->h_addr, &client.sin_addr, h->h_length);
sock = socket (client.sin_family, SOCK_STREAM, 0);
if (sock < 0)
{
fprintf (stderr, "socket() failed: exiting");
return -1;
}
fprintf (stderr, "Trying to connect %u.%u.%u.%u ...\n",
(uchar) h->h_addr[0], (uchar) h->h_addr[1],
(uchar) h->h_addr[2], (uchar) h->h_addr[3]);
if (connect (sock, (struct sockaddr *) &client, sizeof (client)) < 0)
{
fprintf (stderr, "connect() failed: exiting");
return -1;
}
else
{
fprintf (stderr, "Connected...\n");
}
fprintf (stderr, "Sending data ....\r");
fflush (stderr);
xfersz = 0;
while (1)
{
read_sz = read (in_fd, buf, BUFSZ);
if (read_sz <= 0)
{
close (sock);
close (in_fd);
fprintf (stderr, "\nDone.. %ld bytes transimtted\n", xfersz);
endingTime = times(&tmsend);
printf ("\n Time to send the Data %f :\n", (endingTime -
startingTime)/( (double)(sysconf(_SC_CLK_TCK)) ) );
return 0;
}
if (write (sock, buf, read_sz) != read_sz)
{
fprintf (stderr, "\nwrite() failed: exiting\n");
close (sock);
close (in_fd);
return -1;
}
xfersz += read_sz;
if (verbose)
{
fprintf (stderr, "Sending data %ld bytes ...\r", xfersz);
fflush (stderr);
}
}
}
Thanks all in advance
.
- Follow-Ups:
- Re: Transfer files using sockets, urgent!!
- From: "Nils O. Selåsdal"
- Re: Transfer files using sockets, urgent!!
- From: Doug
- Re: Transfer files using sockets, urgent!!
- From: Ian Collins
- Re: Transfer files using sockets, urgent!!
- From: Barry Margolin
- Re: Transfer files using sockets, urgent!!
- Prev by Date: Reconnecting to the same socket
- Next by Date: Re: Malloc & sbrk
- Previous by thread: Reconnecting to the same socket
- Next by thread: Re: Transfer files using sockets, urgent!!
- Index(es):
Relevant Pages
|