示例#1
0
int process_func(int trans_sock)
{
	assert(trans_sock > 0);

	struct trans_data_t *ptrans = NULL;
	int max_len = sizeof(struct trans_data_t) + MAX_TRANSMIT_DATA_SIZE;

	ptrans = (struct trans_data_t *)calloc(1, max_len);
	if (NULL == ptrans)
	{
		lerror("calloc memory failed, err: %s", strerror(errno));
		goto ERR;
	}
	ptrans->data_len = MAX_TRANSMIT_DATA_SIZE;

	if (-1 == recv_data(trans_sock, ptrans, max_len))
	{
		lerror("call recv_data() failed");
		goto ERR;
	}

	switch (ptrans->cmd)
	{
		case UD_UPLOAD:
			(void)process_upload(ptrans);
			break;
		case UD_DOWNLOAD:
			(void)process_download(trans_sock, ptrans);
			break;
		default:
			lerror("unknown cmd type, cmd: %d", ptrans->cmd);
			break;
	}

	free(ptrans);

	return 0;
ERR:
	if (NULL != ptrans)
	{
		free(ptrans);
	}
	return -1;
}
示例#2
0
int process_response(struct server_attr *attr)
{
	switch (attr->resp.code) {
	case RESP_LS:
		process_ls(attr);
		break;
	case RESP_CD:
		process_cd(attr);
		break;
	case RESP_PWD:
		process_pwd(attr);
		break;
	case RESP_MKDIR:		/* Success */
		break;
	case RESP_RM:
		break;
	case RESP_DOWNLOAD:
		process_download(attr);
		break;
	case RESP_UPLOAD:
		process_upload(attr);
		break;
	case RESP_LS_ERR:		/* Print error message just */
	case RESP_CD_ERR:
	case RESP_MKDIR_ERR:
	case RESP_RM_ERR:
	case RESP_DOWNLOAD_ERR:
	case RESP_UPLOAD_ERR:
		printf("%s\n", attr->data);
		break;
	case RESP_EXIT:
		return RESP_EXIT;	/* Receive exit confirm */
		break;
	default:
		err_exit(0, "UNKNOWN RESPONSE: (%#X)", attr->resp.code);
	}
	return 0;
}
示例#3
0
int main(int argc, const char *argv[])
{

	int sockfd;
	int acceptfd;
	struct sockaddr_in serveraddr, clientaddr;
	char buf[N] = {};

	if(argc < 3)
	{
		fprintf(stderr, "Usage:%s serverip port.", argv[0]);
		return -1;
	}

	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		err_log("fail to socket");
	}

	printf("sockfd = %d\n", sockfd);

	serveraddr.sin_family = AF_INET;
	serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
	serveraddr.sin_port = htons(atoi(argv[2]));

	if(bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
	{
		err_log("fail to bind");
	}

	if(listen(sockfd, 15) < 0)
	{
		err_log("fail to listen");
	}


	socklen_t addrlen = sizeof(struct sockaddr);

	while(1)
	{
		if((acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr,&addrlen)) < 0)
		{
			err_log("fail to accept");
		}
		printf("acceptfd = %d\n", acceptfd);
		
		recv(acceptfd, buf, N, 0);

		switch(buf[0])
		{
		case 'L':
			process_list(acceptfd);
			break;
		case 'P':
			process_upload(acceptfd, buf+2);
			break;
		case 'G':  // G filename
			process_download(acceptfd,buf+2);
			break;
		case 'Q':
			process_quit();
			break;
		default:
			printf("Error cmd.\n");
		}

	}

	close(acceptfd);
	close(sockfd);

	return 0;
}