Thread: Sending two files in one command?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    23

    Sending two files in one command?

    IS there a quick way to send two files below in one command?

    ./tossfile hostip file1.c
    ./tossfile hostip file2.c

    while I am running the server as ./catchfile

    I can send one file but for some reason I can't send file1.c and file2.c at the same time.....
    Last edited by Mankthetank19; 12-10-2008 at 06:34 PM.

  2. #2
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    {No}

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    Why is that - please explain?

  4. #4
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Cuz tossfile is 4 doing jus dat. tossin' a file. Not tossin 2 files. tossdir?

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    I don't think tossdir will work. I am trying to "socket program"

  6. #6
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    I do not understand ur approche.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    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("&#37;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........

  8. #8
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Can I ass you a kestion? Wen u cpy ovr a directorie duz ur computar cpy ovor multiple fyls at the same time? or dos it secuentally send files? cuz las time I cheqkd it dos secuental.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    I am not talking about the commands of cp and moving commands. I am trying to run a ftp server client program.

  10. #10
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Jus do 1 cmd, then loop to da nex 1, den da nex one until ur dun.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    All I know if the main loop is altered in the client program, I believe the ./tossfile host IP tossfile.c catchfile.c will work but I don't know what to alter.

  12. #12
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    I do not see much ftp commands.

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    Well within the server file, I needed to commented out the following part. Everything works now.

    Code:
    }
    	close(fd);
    	//close(sd);
    }

  14. #14
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Oh shnippidy shnap-crackidy! I didn't c that! Good jorb bud.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. system command not executing with format specifier
    By ridhamshah in forum C Programming
    Replies: 6
    Last Post: 11-08-2006, 05:58 AM
  2. sending output from a command to file
    By smegly in forum C Programming
    Replies: 3
    Last Post: 05-19-2004, 11:20 AM
  3. sending output to a file from the command line...
    By GGrrl in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2003, 10:11 AM
  4. Command Line Question
    By Sebastiani in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2002, 01:46 AM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM