INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

While sending a file I am only gett

While sending a file I am only gett

(OP)
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;
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close