shvtr159

proj source file

This diff is collapsed. Click to expand it.
1 +// 최종 결합된 data를 socket을 이용해 수신
2 +
3 +#include <stdio.h>
4 +#include <sys/types.h>
5 +#include <sys/socket.h>
6 +#include <netinet/in.h>
7 +#include <arpa/inet.h>
8 +#include <stdlib.h>
9 +#include <string.h>
10 +#include <unistd.h>
11 +#include <signal.h>
12 +
13 +#define Port 12300
14 +#define Board_addr "192.168.2.99"
15 +#define BUF_SIZE 1024
16 +
17 +int Sockfd;
18 +
19 +void receive_result() {
20 + static int ind = 0;
21 +
22 + char info[2000];
23 + strcpy(info, "");
24 + if (read(Sockfd, (char*)&info, sizeof(info)) <= 0) {
25 + perror("read");
26 + exit(1);
27 + }
28 + printf("%s\n", info);
29 +
30 + /*int now_size = 0, read_size;
31 + char file_name[256], msg_buf[BUF_SIZE], sync = '\0';
32 + FILE* image;
33 + unsigned int fsize;
34 +
35 + sprintf(file_name, "img_%03d_result.jpg", ind); // save file name
36 +
37 + image = fopen(file_name, "w");
38 +
39 + // recv file size
40 + msg_buf[fsize] = '\0';
41 + fsize = atoi(msg_buf);
42 + printf("%d\n", fsize);
43 +
44 + // recv image file
45 + while(1) {
46 + memset(msg_buf, 0, BUF_SIZE);
47 + read_size = read(Sockfd, (char*)&msg_buf, BUF_SIZE); // get image from socket
48 + fwrite(msg_buf, 1, read_size, image); // write image
49 + now_size += read_size;
50 + if(now_size == fsize)
51 + break;
52 + else if(now_size > fsize) {
53 + printf("file error\n");
54 + exit(1);
55 + }
56 + write(Sockfd, (char*)&sync, sizeof(sync)); // for sync
57 + }
58 +
59 + printf("Receive done : %s\n", file_name);
60 +
61 + if(write(Sockfd, (char*)&file_name, sizeof(file_name)) < 0) { // 종료 전송
62 + perror("write");
63 + exit(1);
64 + }
65 +
66 + fclose(image);
67 + ind++;
68 + if(ind == 999) ind = 0; // max 1000 file
69 +*/
70 +}
71 +
72 +main(int argc, char *argv[])
73 +{
74 + int n, len;
75 + struct sockaddr_in boardAddr;
76 + char cwd[256];
77 +
78 + if(getcwd(cwd, 256) == NULL) {
79 + perror("getcwd");
80 + exit(1);
81 + }
82 +
83 + if ((Sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
84 + perror("socket");
85 + exit(1);
86 + }
87 +
88 + bzero((char *)&boardAddr, sizeof(boardAddr));
89 + boardAddr.sin_family = PF_INET;
90 + boardAddr.sin_addr.s_addr = inet_addr(Board_addr);
91 + boardAddr.sin_port = htons(Port);
92 +
93 + if (connect(Sockfd, (struct sockaddr*)&boardAddr, sizeof(boardAddr)) < 0) {
94 + perror("connect");
95 + exit(1);
96 + }
97 +
98 + printf("connect\n");
99 + /*
100 + if(write(Sockfd, (char*)&cwd, sizeof(cwd)) < 0) { // cwd 전송
101 + perror("write");
102 + exit(1);
103 + }
104 + */
105 + while(1) {
106 + printf("\n\n");
107 + receive_result(); // 받은 파일이름 출력
108 + }
109 + close(Sockfd);
110 + return 0;
111 +}