#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
 
#define SERVERPORT 8888
#define MAXBUF 1024
 
int main(int argc, char* argv[]) {
	int sockd;
	int counter;
	int fd;
	struct sockaddr_in xferServer;
	char buf[MAXBUF];
	int returnStatus;
 
	if(argc < 3) {
		fprintf(stderr, "Usage: %s <ip address> <filename> <dest filename>\n", argv[0]);
		return 1;
	}
 
	/* create socket */
	sockd = socket(AF_INET, SOCK_STREAM, 0);
 
	if(sockd == -1) {
		fprintf(stderr, "Could not create socket!\n");
		return 1;
	}
 
	/* Set up server information */
	xferServer.sin_family = AF_INET;
	xferServer.sin_addr.s_addr = inet_addr(argv[1]);
	xferServer.sin_port = htons(SERVERPORT);
 
	/* connect to server */
	returnStatus = connect(sockd, (struct sockaddr*)&xferServer, sizeof(xferServer));
 
	if(returnStatus == -1) {
		fprintf(stderr, "Count not connect to the server!\n");
		return 1;
	}
 
	/* send the name of the file we want to the server */
	returnStatus = write(sockd, argv[2], strlen(argv[2])+1);
 
	if(returnStatus == -1) {
		fprintf(stderr, "Could not send the filename to the server!\n");
		return 1;
	}
 
	/* call shutdown to set our socket to read-only */
	shutdown(sockd, SHUT_WR);
 
	/* oen up a handle to our dest file */
	fd = open(argv[3], O_WRONLY | O_CREAT | O_APPEND);
 
	if(fd == -1) {
		fprintf(stderr, "Could not open file, using stdout.\n");
		fd = 1;
	}
 
	/* read the file from the socket */
	while((counter = read(sockd, buf, MAXBUF)) > 0) {
		write(fd, buf, counter);
	}
 
	if(counter == -1) {
		fprintf(stderr, "Could not read file from server!\n");
		return 1;
	}
 
	/* clean up */
	close(sockd);
	return 0;
}