hostYP.c 2.2 KB
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define Port 23
#define Board_addr "192.168.2.99"

int sockfd;

typedef struct {
	char filename[256];
	char result_filename[256];
	char sender[30];
	char pwd[256];
	int pass;
} Info;
 
main(int argc, char *argv[])
{
	if(argc != 3) {
		printf("Usage: %s destination_dir sender_ID@sender_IP\n", argv[0]);
		exit(1);
	}

	Info info;
	info.pass = 0;
	int	n, len;
	struct sockaddr_in	boardAddr;
	char board_dir[256];
	// info 붙여넣기
	strcpy(info.sender, argv[2]);
	if(getcwd(info.pwd, 256) == NULL) {
		perror("getcwd");
		exit(1);
	}
	
	while(1) {
		if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)  {
			perror("socket");
			exit(1);
		}
	
		bzero((char *)&boardAddr, sizeof(boardAddr));
		boardAddr.sin_family = PF_INET;
		boardAddr.sin_addr.s_addr = inet_addr(Board_addr);
		boardAddr.sin_port = htons(Port);
			
		if (connect(sockfd, (struct sockaddr*)&boardAddr, sizeof(boardAddr)) < 0)  {
			perror("connect");
			exit(1);
		}
		
		printf("\nsend file name : ");
		scanf("%s", &info.filename);

		char scp[256] = "scp ";	
		char root[30] = " root@";
		char fname[256];
		//scp 명령어 만들기
		strcat(scp, info.filename);
		strcat(root, Board_addr);
		len = strlen(Board_addr);
		root[6+len] = ':';
		strcat(scp, root);
		strcat(scp, argv[1]);
		len = strlen(scp);
		scp[len] = '\n';
		// 결과 파일 이름 만들기
		strcpy(fname, info.filename);
		char* ptr = strtok(fname, ".");
		char result_name[256];
		strcpy(result_name, ptr);
		char result[256] = "_result.jpg"; // 출력이 jpg로 고정되어있음
		strcat(result_name, result);
		strcpy(info.result_filename, result_name);

		printf("Do : %s\n", scp);
		if(system(scp)) { // scp 명령어 실행
			perror("system");
			exit(1); 
		}
	
		if(write(sockfd, (char*)&info, sizeof(info)) < 0) { // info 전달 
			perror("write");
			exit(1);
		} 
	
		printf("\nwait result\n");
			
		if((n = read(sockfd, (char*)&info, sizeof(info))) < 0) {
			perror("read");
			exit(1);
		}
	
		if(info.pass == 1)
			printf("Success\n======================================================\n");
		
		close(sockfd);
	}
}