서버 실행환경

 

 학과 서버 사용

 OS

 SunOS 5.10

 GCC

 3.4.6

 Java

 1.5.0_16


클라이언트 실행환경

 Desktop

 조립식

 CPU

 Intel(R) Core(TM) i7-2600K CPU @ 3.40GHz 3.40GHz

 Memory

 8.00 GB

 OS

 Windows 7 Professional K 64bit

 Java

 1.8.0_05

 MySQL

 Ver 14.14 Distrib 5.6.19, for Win64

 Web Server

 Apache Tomcat 7.0.51


서버 프로그램

#include <stdio.h>
#include <sys/types.h> 	// socket, bind, accept, open
#include <sys/socket.h> // socket, bind, listen, accept
#include <unistd.h>		// read, write
#include <sys/stat.h> 	// open
#include <fcntl.h>		// open
#include <errno.h>

#include <string.h>

#define PORT	5500
#define MAXBUF	1024

int main() {
	int server_sockfd;
	int client_sockfd;
	int des_fd;	// file num
	struct sockaddr_in serveraddr, clientaddr;
	int client_len, read_len, file_read_len;	// length
	char buf[MAXBUF];
	
	
	int check_bind;
	

	client_len = sizeof(clientaddr);

	/* socket() */
	server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(server_sockfd == -1) {
		perror("socket error : ");
		exit(0);
	}

	/* bind() */
	bzero(&serveraddr, sizeof(serveraddr));
	serveraddr.sin_family		= AF_INET;
	serveraddr.sin_addr.s_addr	= htonl(INADDR_ANY);
	serveraddr.sin_port			= htons(PORT);

	if(bind(server_sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) > 0) {
		perror("bind error : ");
		exit(0);
	}

	/* listen */
	if(listen(server_sockfd, 5) != 0) {
		perror("listen error : ");
	}

	while(1) {
		char file_name[MAXBUF]; // local val
		memset(buf, 0x00, MAXBUF);
		
		/* accept() */
		client_sockfd = accept(server_sockfd, (struct sockaddr *)&clientaddr, &client_len);
		printf("New Client Connect : %s\n", inet_ntoa(clientaddr.sin_addr));

		/* file name */
		read_len = read(client_sockfd, buf, MAXBUF);
		if(read_len > 0) {
			strcpy(file_name, buf);
			printf("%s > %s\n", inet_ntoa(clientaddr.sin_addr), file_name);
		} else {
			close(client_sockfd);
			break;
		}

		/* create file */
FILE_OPEN:
		des_fd = open(file_name, O_WRONLY | O_CREAT | O_EXCL, 0700);
		if(!des_fd) {
			perror("file open error : ");
			break;
		}
		if(errno == EEXIST) {
			close(des_fd);
			size_t len = strlen(file_name);
			file_name[len++] = '_';
			file_name[len++] = 'n';
			file_name[len] = '\0';
			goto FILE_OPEN;
		}
		
		/* file save */
		while(1) {
			memset(buf, 0x00, MAXBUF);
			file_read_len = read(client_sockfd, buf, MAXBUF);
			write(des_fd, buf, file_read_len);

			if(file_read_len == EOF | file_read_len == 0) {
				printf("finish file\n");
				break;
			}


		}


		close(client_sockfd);
		close(des_fd);
	}
	close(server_sockfd);
	return 0;
}


클라이언트 프로그램

#include <WinSock2.h>
#include <stdio.h>
#include <sys/types.h>
#include <io.h>
#include <sys/stat.h>
#include <fcntl.h>


#define PORT	5500
#define IP		"127.0.0.1"
#define MAXBUF	1024

int main() {
	WSADATA		WSAData;
	SOCKADDR_IN	addr;
	SOCKET		s;
	int			sourse_fd;
	char		buf[MAXBUF];
	int			file_name_len, read_len;
	
	if(WSAStartup(MAKEWORD(2,2), &WSAData) != 0) {
		return 1;
	}

	/* socket() */
	s = socket(AF_INET, SOCK_STREAM, 0);
	if(s == INVALID_SOCKET) {
		return 1;
	}

	addr.sin_family				= AF_INET;
	addr.sin_addr.S_un.S_addr	= inet_addr(IP);
	addr.sin_port				= htons(PORT);
	if(connect(s, (struct sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR) {
		perror("connect : ");
		printf("fail to connect.\n");
		closesocket(s);
		return 1;
	}

	memset(buf, 0x00, MAXBUF);
	printf("전송할 파일명 : ");
	scanf("%s", buf);

	printf(" > %s\n", buf);
	file_name_len = strlen(buf);

	send(s, buf, file_name_len, 0);
	sourse_fd = _open(buf, _O_RDONLY);
	if(!sourse_fd) {
		perror("Error : ");
		return 1;
	}
	
	while(1) {
		memset(buf, 0x00, MAXBUF);
		read_len = _read(sourse_fd, buf, MAXBUF);
		send(s, buf, read_len, 0);
		if(read_len == 0) {
			break;
		}
		
	}

	return 0;
}


+ Recent posts