netcat like file transfer


 
Thread Tools Search this Thread
Top Forums Programming netcat like file transfer
# 1  
Old 04-02-2005
netcat like file transfer

Hi Folks

I am not a c programmer .But i need help in writing a program which can do this any ideas on how to go about it .

i start a server on the target server where files need to be copied

start-server -port 5006 & ---start the server and listen it on a partcular port

on the source server do this

if=/usr/myfile |copress start-cliwnt -server target-server -port 500 of=/home/myfile

I need help in writing the client and the2server.

I looked up the unix forums and netcat was close to what i wanted .But not exactly the prog for my need.Anywhere from whe i can start.

regards
Hrishy
# 2  
Old 04-18-2005
Hi Guys

I looked up the unix man pages and Beejs guide to socket programming.I just need your opinion is the c function sendfile the answer to my question ?

man sendfile

regards
Hrishy
# 3  
Old 04-28-2005
Hi Folks

This is what i managed to write server.c
Code:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>


int main(int argc, char **argv)
{
  int port = 9000;           /* port number to use */
  int sock;                  /* socket desciptor */
  int desc;                  /* file descriptor for socket */
  int in_fd;                 /* input file descriptor */
  int out_fd;		     /*  output file descriptor */
  struct sockaddr_in addr;   /* socket parameters for bind */
  struct sockaddr_in addr1;  /* socket parameters for accept */
  int    addrlen;            /* argument to accept */
  struct stat stat_buf;      /* argument to fstat */
  off_t offset = 0;          /* file offset */
  char filename[PATH_MAX];   /* filename to send */
  int rc;                    /* holds return code of system calls */

  /* check command line arguments, handling an optional port number */
  if (argc == 2) {
    port = atoi(argv[1]);
    if (port <= 1024) { /* dont want root to run this :-) */
      fprintf(stderr, "invalid port: %s\n", argv[1]);
      exit(1);
    }
  } else if (argc != 1) {
    fprintf(stderr, "usage: %s [port]\n", argv[0]);
    exit(1);
  }

  /* create Internet domain socket */
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1) {
    fprintf(stderr, "unable to create socket: %s\n", strerror(errno));
    exit(1);
  }

  /* fill in socket structure */
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = INADDR_ANY;
  addr.sin_port = htons(port);

  /* bind socket to the port */
  rc =  bind(sock, (struct sockaddr *)&addr, sizeof(addr));
  if (rc == -1) {
    fprintf(stderr, "unable to bind to socket: %s\n", strerror(errno));
    exit(1);
  }

  /* listen for clients on the socket */
  rc = listen(sock, 1);
  if (rc == -1) {
    fprintf(stderr, "listen failed: %s\n", strerror(errno));
    exit(1);
  }

  while (1) {

    /* wait for a client to connect */
    desc = accept(sock, (struct sockaddr *)  &addr1, &addrlen);
    if (desc == -1) {
      fprintf(stderr, "accept failed: %s\n", strerror(errno));
      exit(1);
    }

    /* get the file name from the client */
    rc = recv(desc, filename, sizeof(filename), 0);
    if (rc == -1) {
      fprintf(stderr, "recv failed: %s\n", strerror(errno));
      exit(1);
    }

    /* null terminate and strip any \r and \n from filename */
		filename[rc] = '\0';
    if (filename[strlen(filename)-1] == '\n')
      filename[strlen(filename)-1] = '\0';
    if (filename[strlen(filename)-1] == '\r')
      filename[strlen(filename)-1] = '\0';

    /* exit server if filename is "quit" */
    if (strcmp(filename, "quit") == 0) {
      fprintf(stderr, "quit command received, shutting down server\n");
      break;
    }

    /* fprintf(stderr, "received request to send file %s\n", filename); */

    /* open the file to be sent */
    in_fd = open(filename, O_RDONLY);
    if (in_fd == -1) {
      fprintf(stderr, "unable to open '%s': %s\n", filename, strerror(errno));
      exit(1);
    }

    /* get the size of the file to be sent */
    fstat(in_fd, &stat_buf);

    /* copy file using sendfile */
    offset = 0;
    rc = sendfile (desc, in_fd, &offset, stat_buf.st_size);
    if (rc == -1) {
      fprintf(stderr, "error from sendfile: %s\n", strerror(errno));
      exit(1);
    }
    if (rc != stat_buf.st_size) {
      fprintf(stderr, "incomplete transfer from sendfile: %d of %d bytes\n",
              rc,
              (int)stat_buf.st_size);
      exit(1);
    }

    /* close descriptor for file that was sent */
    close(in_fd);

    /* close socket descriptor */
    close(desc);
  }

  /* close socket */
  close(sock);
  return 0;
}

For the client part
Code:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/sendfile.h>
#include <sys/stat.h>

int main (int argc, char** argv)
{
  int src;               	/* file descriptor for source file */
  int dest;              	/* file descriptor for destination file */
  struct stat stat_buf;  	/* hold information about input file */
  off_t offset = 0;      	/* byte offset used by sendfile */
  int rc;                	/* return code from sendfile */
  int port;		 	/* port on the remote server to connect to */
  struct sockaddr_in server;    /* socket address */
  int s ;			/* client socket */
  /* check for two command line arguments */
  if (argc != 4) {
    fprintf(stderr, "usage: %s <hostname> <port> <source> <destination> \n", argv[0]);
    exit(1);
  }

  /* check command line arguments, handling an optional port number */
  if (argc == 4) {
    port = atoi(argv[1]);
    if (port <= 1024) { /* dont want root to run this :-) */
      fprintf(stderr, "invalid port: %s\n", argv[1]);
      exit(1);
    }
  } 

  if ((he=gethostbyname(argv[1])) == NULL) {  /* get the host info */
            herror("gethostbyname");
            exit(1);
        }

  strcpy( buf, "Hello there!"); /* create the message */

  /* create stream socket using TCP */
  fprintf(stderr, "Creating datagram socket.\n");
  s = socket(AF_INET, SOCK_STREAM, 0);
  if( s == -1 ) {
  fprintf(stderr, "Socket was not created.\n");
  exit(1);
  }
  else
  fprintf(stderr, "Socket created successfully.\n");
  server.sin_family = AF_INET; /* set up the server name */
  server.sin_port = htons(port);
  server.sin_addr.s_addr = inet_addr( argv[1] );

  /* connect to the server */
  if( connect(s, &server, sizeof(server)) < 0) {
  fprintf(stderr, "Failed to connect to socket.\n");
  exit(1);
  }

  printf("Sending the message: %s\n", buf); /* send the message */
  if( send(s, buf, sizeof(buf), 0) < 0 ) {
  fprintf(stderr, "Failed to send data to socket.\n");
  exit(1);
  }

   /* receive the echoed message from the server */
  if( recv(s, buf, sizeof(buf), 0) < 0 ) {
  fprintf(stderr, "Failed to receive data from socket.\n");
  exit(1);
  }
  printf("The message from the server was: %s\n", buf);
  
  close(s); /* close the socket */
  printf("Client closed successfully\n"); /* successfully exit */
  exit(0);
}


The problem i have now is what message the client should pass to the server ?

I just passed strcpy( buf, "Hello there!"); but i actually need to pass the file descriptor of the file which i need to get .

And when the server responds i need to copy that file to some path on the client.

I hope somebody helps me now .Please i have a feeling i am very close.

regards
Hrishy
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Netcat ( nc -l ) as webserver

Dear Linux guru's I am trying to create a webserver using nc (netcat only) on RHEL 7.2 running on bash shell. now the easy thing is to get nc listing to a port and respond back $ while true; do { echo -e 'HTTP/1.0 200 OK\r\n'; set; } | nc -l 7877; done This when called from a... (3 Replies)
Discussion started by: chakrapani
3 Replies

2. UNIX for Advanced & Expert Users

Telnet vs netcat behavior

Currently I run a number of network tests using netcat that checks for an open port on a remote IP-address, using this syntax: netcat -v -w 5 -z 107.249.95.5 4488 For some reason, the netcat command above is hanging (although others work fine), but a telnet is showing a valid connection like... (4 Replies)
Discussion started by: ckmehta
4 Replies

3. IP Networking

Help with Netcat

Hi all, I know my question is regarding Windows and not Linux, but I simply need people who know Netcat pretty well and I'm guessing here is a good place for that. So on with my question. I'm doing some research, and I was playing around with netcat on a WinXP VM but I can't seem to get... (0 Replies)
Discussion started by: MrCrumbs
0 Replies

4. Shell Programming and Scripting

Post using nc(netcat)

Hi; I have a url like http://localhost:8080/examples/jsp/dates/nextPageToPost.jsp?name=ajay&password=pas&sex=Male&check=on&nationality=USA&description=aa&submit=submit in which i want to use nc for http post for parameters like "name","password"....etc can neone please help me how to do that... (3 Replies)
Discussion started by: ajaypadvi
3 Replies

5. Linux

Thank you radoulov for your help on netcat command (nc -lp)

Hello, Thank you very much for the line nc -lp <port> . I tried to run simple chat session with nc as it's shown in catonmatDOTorg but failed miserably with that syntax inspite of opening port 7777 by iptables . But your command example is working nicely. So a bagful of thanks :)) Only one... (0 Replies)
Discussion started by: vectrum
0 Replies

6. Solaris

Please help me to install netcat on solaris

hello guys, i want to install netcat on my solaris. after i tar and gunzip netcat i'm confuse what do i must to do ? please help me to install netcat on my solaris. I'm beginner :( (2 Replies)
Discussion started by: praset
2 Replies

7. UNIX and Linux Applications

netcat prints blank pages

Please direct me to the right forum tree if i am in the wrong section for this. i have netcat on a unix machine and there is no man nc or man netcat available. my command i am using is: cat $FILE1 | netcat -h $PRINTER -p 9100 (-h -p -d are the only flags available in this version of... (3 Replies)
Discussion started by: dunpealslyr
3 Replies

8. UNIX for Advanced & Expert Users

How does netcat manage the data it transfers ?

Hi all, When using netcat to transfer the data over socket, if no connection is established, how long will netcat keep the data; will it discard the "oldest" data ? Say for example I use "Some Command | netcat -l -p port", the command I use will generate a lot of output constantly, while no... (2 Replies)
Discussion started by: qiulang
2 Replies

9. Shell Programming and Scripting

netcat

Is there a way how to react on the message a client sent to the server? I would like as the client sent message to server: "get information such and such" and server would answer. Thank you for reply! (6 Replies)
Discussion started by: MartyIX
6 Replies

10. UNIX for Advanced & Expert Users

Netcat with Authentication?

I'd like to do a data transfer without encryption but with a guarantee that my data comes from a legit source. I'm thinking something that uses a public key scheme to sign the data. Does anyone know of something like that? Thanks! -Pileofrogs (1 Reply)
Discussion started by: pileofrogs
1 Replies
Login or Register to Ask a Question