Here is what I have:
I have a tosscatch.h
Code:
/* tosscatch.h
*/
/*****************************************************************************/
/*** tosscatch.h ***/
/*** ***/
/*** Structure for file's meta information (name, mode and size). ***/
/*****************************************************************************/
#define DEFAULT_PORT 9818
typedef struct
{
int Length;
mode_t Mode;
char Name[100];
} TFileMsg;
I have a server file:
Code:
/* catchfile.cfile:///usr/share/doc/HTML/index.htm
*/
/*****************************************************************************/
/*** catchfile.c ***/
/*** ***/
/*** This is a quick tool to accept files sent from tossfile. Please be ***/
/*** aware that this does not check for security. It also is incomplete. ***/
/*** directories that are either inaccessible or non-existent will report ***/
/*** an error instead of either getting permission or creating the needed ***/
/*** directories. ***/
/*****************************************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <stdlib.h>
#include "tosscatch.h"
/*---------------------------------------------------------------------*/
/*--- CreatePath - create the needed path for the destination. ---*/
/*---------------------------------------------------------------------*/
int CreatePath(char *filepath)
{ char *fpath, *c;
fpath = strdup(filepath);
if ( (c = strrchr(fpath, '/')) != NULL )
{
c = 0;
/*--Incomplete---*/
}
free(fpath);
return 0;
}
/*---------------------------------------------------------------------*/
/*--- CatchFile - get filename/contents and create a local file ---*/
/*---------------------------------------------------------------------*/
void CatchFile(int sd)
{ int fd, size, len=0;
TFileMsg msg;
if ( recv(sd, &msg, sizeof(msg), 0 ) < 0 )
{
perror("Header failure");
return;
}
printf("%s\n", msg.Name);
if ( CreatePath(msg.Name) != 0 )
{
perror("Filename failure");
return;
}
if ( (fd = creat(msg.Name, ntohs(msg.Mode))) < 0 )
{
msg.Length = htonl(-1);
send(sd, &msg, sizeof(msg), 0);
perror(msg.Name);
return;
}
send(sd, &msg, sizeof(msg), 0);
size = ntohl(msg.Length);
while ( len < size )
{ char buf[32768];
int bytes;
bytes = recv(sd, buf, sizeof(buf), 0);
if ( bytes < 0 )
perror("recv()");
else
{
write(fd, buf, bytes);
len += bytes;
}
}
close(fd);
close(sd);
}
#define panic(msg) {perror(msg);exit(1);}
/*---------------------------------------------------------------------*/
/*--- main - set up server and recieve connections. ---*/
/*---------------------------------------------------------------------*/
int main(int count, char *strings[])
{ int sd, port = DEFAULT_PORT;
struct sockaddr_in addr;
if ( count == 2 )
port = atoi(strings[1]);
sd = socket(PF_INET, SOCK_STREAM, 0);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
if ( bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0 )
panic("bind()");
if ( listen(sd, 4) != 0 )
panic("listen()");
while (1)
{ int i, len=sizeof(addr);
short numfiles;
int client = accept(sd, (struct sockaddr *)&addr, &len);
fprintf(stderr, "host(%s:%d): ", inet_ntoa(addr.sin_addr),
ntohs(addr.sin_port));
recv(client, &numfiles, sizeof(numfiles), 0);
numfiles = ntohs(numfiles);
for ( i = 0; i < numfiles; i++ )
{
fprintf(stderr, "%d/%d: ", i+1, numfiles);
CatchFile(client);
}
}
close(sd);
}
I also have a client Program
Code:
/* tossfile.c
*/
/*****************************************************************************/
/*** tossfile.c ***/
/*** ***/
/*** Takes the files from the command line and ships a copy to the ***/
/*** destination. Note that this program has no security. ***/
/*****************************************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include "tosscatch.h"
/*---------------------------------------------------------------------*/
/*--- FillAddress - convert the host:port to the socket address ---*/
/*---------------------------------------------------------------------*/
void FillAddress(struct sockaddr_in *addr, char *name)
{ int port=DEFAULT_PORT;
char *p = strchr(name, ':');
struct hostent *host;
if ( p != NULL )
{
port = atoi(p+1);
*p = 0;
}
if ( port < 1024 )
{
fprintf(stderr, "Illegal port number (must be >= 1024)\n");
abort();
}
host = gethostbyname(name);
memset(addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
addr->sin_addr.s_addr = *(long*)host->h_addr;
}
/*---------------------------------------------------------------------*/
/*--- SendFile - send the file to the host. ---*/
/*---------------------------------------------------------------------*/
void SendFile(int sd, char *fname)
{ int fd, len=0;
TFileMsg msg;
struct stat stat;
time_t t;
if ( (fd = open(fname, O_RDONLY)) < 0 )
{
perror(fname);
return;
}
if ( fstat(fd, &stat) < 0 )
{
perror(fname);
close(fd);
return;
}
memset(&msg, 0, sizeof(msg));
msg.Length = htonl(stat.st_size);
msg.Mode = htons(stat.st_mode);
strncpy(msg.Name, fname, sizeof(msg.Name));
msg.Name[sizeof(msg.Name)-1] = 0;
if ( send(sd, &msg, sizeof(msg), 0) < 0 )
{
perror("can't send file");
close(fd);
return;
}
if ( recv(sd, &msg, sizeof(msg), 0) < 0 || ntohl(msg.Length) == -1 )
{
perror("can't establish file");
close(fd);
return;
}
time(&t);
while ( len < stat.st_size )
{ char buf[32768];
int bytes;
bytes = read(fd, buf, sizeof(buf));
if ( send(sd, buf, bytes, 0) < bytes )
printf("short buffer!\n");
len += bytes;
}
printf("Secs=%d\n", time(0)-t);
close(fd);
}
/*---------------------------------------------------------------------*/
/*--- main - open socket and send out file one at a time ---*/
/*---------------------------------------------------------------------*/
int main(int count, char *strings[])
{ int sd;
struct sockaddr_in addr;
if ( count < 3 )
{
printf("Usage: %s <host>[:<port>] <filename> ... \n", strings[0]);
exit(0);
}
sd = socket(PF_INET, SOCK_STREAM, 0);
FillAddress(&addr, strings[1]);
if ( connect(sd, &addr, sizeof(addr)) != 0 )
perror("Could not connect to host");
else
{ int i;
unsigned short numfiles;
count -= 2;
strings += 2;
numfiles = htons((short)count);
send(sd, &numfiles, sizeof(numfiles), 0);
for ( i = 0; i < count; i++ )
SendFile(sd, strings[i]);
}
close(sd);
}
When I run this code on one machine:
./catchfile
I then run this code on the other machine:
./tossfile host IP catchfile.c
The ./catchfile program would produce this response:
host(192.168.236.111:48591): 1/1: catchfile.c
I am trying to do: ./tossfile host IP tossfile.c catchfile.c in one command.
What is the command to do this is my question........