receive_result.c 2.2 KB
// 최종 결합된 data를 socket을 이용해 수신

#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>
#include <signal.h>

#define Port 12300
#define Board_addr "192.168.2.99"
#define BUF_SIZE 1024

int Sockfd;

void receive_result() {
	static int ind = 0;

	char info[2000];
	strcpy(info, "");
	if (read(Sockfd, (char*)&info, sizeof(info)) <= 0) {
		perror("read");
		exit(1);
	}
	printf("%s\n", info);

	/*int now_size = 0, read_size;
	char file_name[256], msg_buf[BUF_SIZE], sync = '\0';
	FILE* image;
	unsigned int fsize;

	sprintf(file_name, "img_%03d_result.jpg", ind); // save file name
	
	image = fopen(file_name, "w");
	
	// recv file size
	msg_buf[fsize] = '\0';
	fsize = atoi(msg_buf);
	printf("%d\n", fsize);

	// recv image file
	while(1) {
		memset(msg_buf, 0, BUF_SIZE);
		read_size = read(Sockfd, (char*)&msg_buf, BUF_SIZE); // get image from socket
		fwrite(msg_buf, 1, read_size, image); // write image
		now_size += read_size;
		if(now_size == fsize)
			break;
		else if(now_size > fsize) {
			printf("file error\n");
			exit(1);
		}
		write(Sockfd, (char*)&sync, sizeof(sync)); // for sync
	}
	
	printf("Receive done : %s\n", file_name);
	
	if(write(Sockfd, (char*)&file_name, sizeof(file_name)) < 0) { // 종료 전송 
		perror("write");
		exit(1);
	}
	
	fclose(image);
	ind++;
	if(ind == 999) ind = 0; // max 1000 file
*/
}

main(int argc, char *argv[])
{
	int	n, len;
	struct sockaddr_in	boardAddr;
	char cwd[256];

	if(getcwd(cwd, 256) == NULL) {
		perror("getcwd");
		exit(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("connect\n");
	/*
	if(write(Sockfd, (char*)&cwd, sizeof(cwd)) < 0) { // cwd 전송 
		perror("write");
		exit(1);
	}
	*/
	while(1) {
		printf("\n\n");
		receive_result(); // 받은 파일이름 출력
	}
	close(Sockfd);
	return 0;
}