Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While sending a file I am only gett

Status
Not open for further replies.
Apr 8, 2014
13
US
While sending a file I am only getting the first line on the receiving end. What do I need to change?

receive.c
Code:
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <sys/types.h>

#define SA struct sockaddr
#define LISTENQ 5
#define PACKAGE argv[0]
#define VERSION "1.0"

void print_usage(int exval, char *p, char *v);
int hostname_to_ip(char *, char *);

int main(int argc,char *argv[])
{

  char *hostname = argv[1];
  char ip[100];
  char buffer[100];
  FILE *fp;
  int fd,sockfd,listenfd,connfd;
  pid_t childpid;
  socklen_t client;
  int retval;

  hostname_to_ip(hostname, ip);
  printf("%s resolved to %s\n", hostname, ip);

  struct sockaddr_in servaddr,cliaddr;
  listenfd=socket(AF_INET,SOCK_STREAM,0);
  bzero(&servaddr,sizeof(servaddr));
  servaddr.sin_family=AF_INET;
  servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
  servaddr.sin_port=htons(atoi(ip));
  bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
  listen(listenfd,LISTENQ);
  client=sizeof(cliaddr);
  connfd=accept(listenfd,(SA*)&cliaddr,&client);
  read(connfd,buffer,100);

  fp = fopen("/tmp/junk/testfile1.txt", "w");
  fprintf(fp,"%s",buffer);
  retval = chmod("/tmp/junk/testfile1.txt", 0644);
  retval = chown("/tmp/junk/testfile1.txt", 12345, 1234);
  printf("the file was received successfully");
  printf("the new file created is add1.txt");
  close(connfd);
}

void print_usage(int exval, char *p, char *v) {
  printf("%s [hostname] [filename]\n", p);
  printf("Usage: %s [-h] [-V]]\n", p);
  printf("       -h Print this help and exit\n");
  printf("       -V Print version and exit\n");
  exit(exval);
}

int hostname_to_ip(char * hostname, char* ip)
{
  struct hostent *he;
  struct in_addr **addr_list;
  int i;

  if ((he = gethostbyname(hostname)) == NULL)
  {
    herror("gethostbyname");
    return 1;
  }

  addr_list = (struct in_addr **) he->h_addr_list;

  for(i=0; addr_list[i] != NULL; i++)
  {
    strcpy(ip , inet_ntoa(*addr_list[i]));
    return 0;
  }

  return 1;
}

send.c
Code:
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/stat.h>

#define SA struct sockaddr
#define PACKAGE argv[0]
#define VERSION "1.0"

void print_usage(int exval, char *p, char *v);
int hostname_to_ip(char *, char *);

int main(int argc,char *argv[])
{
  int sockfd;
  char fname[25];
  int len;
  int arg;
  FILE *fd;
  FILE *f;
  char *hostname = argv[1];
  char ip[100];
  char buffer[100];

  hostname_to_ip(hostname, ip);
  printf("%s resolved to %s\n", hostname, ip);

  while((arg = getopt(argc, argv, "Vh")) != 1) {
    switch(arg) {
      case 'h':
        print_usage(0,PACKAGE,VERSION);
        break;
      case 'V':
        printf("%s %s\n\n", PACKAGE, VERSION);
        exit(0);
        break;
      case '?':
        fprintf(stderr, "%s: Error - No such option: `%c'\n\n", PACKAGE, optopt);
        print_usage(1,PACKAGE,VERSION);
        break;
      default:
        if ((fd = fopen("testfile.txt", "r")) == NULL) {
          printf("Unable to open %s\n", optarg);
          exit(EXIT_FAILURE);
        }

        struct sockaddr_in servaddr,cliaddr;
        sockfd=socket(AF_INET,SOCK_STREAM,0);
        bzero(&servaddr,sizeof(servaddr));
        servaddr.sin_family=AF_INET;
        servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
        servaddr.sin_port=htons(atoi(ip));
        inet_pton(AF_INET,ip,&servaddr.sin_addr);
        connect(sockfd,(SA*)&servaddr,sizeof(servaddr));

        fscanf(fd,"%s",buffer);
        write(sockfd,buffer,100);
        printf("the file was sent successfully");
        break;
        exit(EXIT_SUCCESS);
    }
  }
}

void print_usage(int exval, char *p, char *v) {
  printf("%s [hostname] [filename]\n", p);
  printf("Usage: %s [-h] [-V]]\n", p);
  printf("       -h Print this help and exit\n");
  printf("       -V Print version and exit\n");
  exit(exval);
}

int hostname_to_ip(char * hostname, char* ip)
{
  struct hostent *he;
  struct in_addr **addr_list;
  int i;

  if ((he = gethostbyname(hostname)) == NULL)
  {
    herror("gethostbyname");
    return 1;
  }

  addr_list = (struct in_addr **) he->h_addr_list;

  for(i=0; addr_list[i] != NULL; i++)
  {
    strcpy(ip , inet_ntoa(*addr_list[i]));
    return 0;
  }

  return 1;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top