⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sockopt.c

📁 文件系统源代码!!!!! 文件系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	{		*err_no = errno != 0 ? errno : EINTR;	}	else	{		*err_no = 0;	}	return result;}int socketServer(const char *bind_ipaddr, const int port, int *err_no){	struct sockaddr_in bindaddr;	int sock;	int result;		sock = socket(AF_INET, SOCK_STREAM, 0);	if (sock < 0)	{		*err_no = errno != 0 ? errno : EMFILE;		logError("file: "__FILE__", line: %d, " \			"socket create failed, errno: %d, error info: %s.", \			__LINE__, errno, strerror(errno));		return -1;	}	result = 1;	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &result, sizeof(int)) < 0)	{		*err_no = errno != 0 ? errno : ENOMEM;		logError("file: "__FILE__", line: %d, " \			"setsockopt failed, errno: %d, error info: %s.", \			__LINE__, errno, strerror(errno));		close(sock);		return -2;	}		bindaddr.sin_family = AF_INET;	bindaddr.sin_port = htons(port);	if (bind_ipaddr == NULL || *bind_ipaddr == '\0')	{		bindaddr.sin_addr.s_addr = INADDR_ANY;	}	else	{		if (inet_aton(bind_ipaddr, &bindaddr.sin_addr) == 0)		{			*err_no = EINVAL;			logError("file: "__FILE__", line: %d, " \				"invalid ip addr %s", \				__LINE__, bind_ipaddr);			close(sock);			return -3;		}	}	result = bind(sock, (struct sockaddr*)&bindaddr, sizeof(bindaddr));	if (result < 0)	{		*err_no = errno != 0 ? errno : ENOMEM;		logError("file: "__FILE__", line: %d, " \			"bind port %d failed, " \			"errno: %d, error info: %s.", \			__LINE__, port, errno, strerror(errno));		close(sock);		return -4;	}		result = listen(sock, 128);	if (result < 0)	{		*err_no = errno != 0 ? errno : EINVAL;		logError("file: "__FILE__", line: %d, " \			"listen port %d failed, " \			"errno: %d, error info: %s.", \			__LINE__, port, errno, strerror(errno));		close(sock);		return -5;	}	*err_no = 0;	return sock;}int tcprecvfile(int sock, const char *filename, const int64_t file_bytes, \		const int fsync_after_written_bytes){	int fd;	char buff[FDFS_WRITE_BUFF_SIZE];	int64_t remain_bytes;	int recv_bytes;	int written_bytes;	int result;	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);	if (fd < 0)	{		return errno != 0 ? errno : EACCES;	}	written_bytes = 0;	remain_bytes = file_bytes;	while (remain_bytes > 0)	{		if (remain_bytes > sizeof(buff))		{			recv_bytes = sizeof(buff);		}		else		{			recv_bytes = remain_bytes;		}		if ((result=tcprecvdata(sock, buff, recv_bytes, \				g_network_timeout)) != 0)		{			close(fd);			unlink(filename);			return result;		}		if (write(fd, buff, recv_bytes) != recv_bytes)		{			result = errno != 0 ? errno: EIO;			close(fd);			unlink(filename);			return result;		}		if (fsync_after_written_bytes > 0)		{			written_bytes += recv_bytes;			if (written_bytes >= fsync_after_written_bytes)			{				written_bytes = 0;				if (fsync(fd) != 0)				{					result = errno != 0 ? errno: EIO;					close(fd);					unlink(filename);					return result;				}			}		}		remain_bytes -= recv_bytes;	}	close(fd);	return 0;}int tcpdiscard(int sock, const int bytes){	char buff[FDFS_WRITE_BUFF_SIZE];	int remain_bytes;	int recv_bytes;	int result;	remain_bytes = bytes;	while (remain_bytes > 0)	{		if (remain_bytes > sizeof(buff))		{			recv_bytes = sizeof(buff);		}		else		{			recv_bytes = remain_bytes;		}		if ((result=tcprecvdata(sock, buff, recv_bytes, \				g_network_timeout)) != 0)		{			return result;		}		remain_bytes -= recv_bytes;	}	return 0;}int tcpsendfile(int sock, const char *filename, const int64_t file_bytes){	int fd;	int send_bytes;	int result;#ifdef USE_SENDFILE	off_t offset;#endif	fd = open(filename, O_RDONLY);	if (fd < 0)	{		return errno != 0 ? errno : EACCES;	}#ifdef USE_SENDFILE	//printf("sendfile.............., file_bytes=%d\n", file_bytes);#ifdef OS_LINUX	/*	result = 1;	if (setsockopt(sock, SOL_TCP, TCP_CORK, &result, sizeof(int)) < 0)	{		logError("file: "__FILE__", line: %d, " \			"setsockopt failed, errno: %d, error info: %s.", \			__LINE__, errno, strerror(errno));		close(fd);		return errno != 0 ? errno : EIO;	}	*/	offset = 0;	send_bytes = sendfile(sock, fd, &offset, file_bytes);	close(fd);	if (send_bytes != file_bytes)	{		return errno != 0 ? errno : EIO;	}	else	{		return 0;	}#else#ifdef OS_FREEBSD	offset = 0;	result = sendfile(fd, sock, offset, file_bytes, NULL, NULL, 0);	close(fd);	if (result != 0)	{		return errno != 0 ? errno : EIO;	}	else	{		return 0;	}#endif#endif#endif	//printf("file_bytes=%d\n", file_bytes);	{	char buff[FDFS_WRITE_BUFF_SIZE];	int64_t remain_bytes;	remain_bytes = file_bytes;	while (remain_bytes > 0)	{		if (remain_bytes > sizeof(buff))		{			send_bytes = sizeof(buff);		}		else		{			send_bytes = remain_bytes;		}		if (read(fd, buff, send_bytes) != send_bytes)		{			result = errno;			close(fd);			return result != 0 ? result : EIO;		}		//printf("send bytes=%d, total send1: %d, remain_bytes1=%d\n", send_bytes, file_bytes - remain_bytes, remain_bytes);		if ((result=tcpsenddata(sock, buff, send_bytes, \				g_network_timeout)) != 0)		{			close(fd);			return result;		}		remain_bytes -= send_bytes;		//printf("total send2: %d, remain_bytes2=%d\n\n", file_bytes - remain_bytes, remain_bytes);	}	}	close(fd);	return 0;}int tcpsetnonblockopt(int fd, const int timeout){	int result;	int flags;	struct linger linger;	struct timeval waittime;	linger.l_onoff = 1;	linger.l_linger = timeout * 100;	result = setsockopt(fd, SOL_SOCKET, SO_LINGER, \                      &linger, (socklen_t)sizeof(struct linger));	if (result < 0)	{		logError("file: "__FILE__", line: %d, " \			"setsockopt failed, errno: %d, error info: %s.", \			__LINE__, errno, strerror(errno));		return errno != 0 ? errno : ENOMEM;	}	waittime.tv_sec = timeout;	waittime.tv_usec = 0;	result = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,                      &waittime, (socklen_t)sizeof(struct timeval));	if (result < 0)	{		logError("file: "__FILE__", line: %d, " \			"setsockopt failed, errno: %d, error info: %s.", \			__LINE__, errno, strerror(errno));		return errno != 0 ? errno : ENOMEM;	}	result = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,                      &waittime, (socklen_t)sizeof(struct timeval));	if (result < 0)	{		logError("file: "__FILE__", line: %d, " \			"setsockopt failed, errno: %d, error info: %s.", \			__LINE__, errno, strerror(errno));		return errno != 0 ? errno : ENOMEM;	}	/*	{	int bytes;	int size;	bytes = 0;	size = sizeof(int);	result = getsockopt(fd, SOL_SOCKET, SO_SNDBUF,			&bytes, (socklen_t *)&size);	if (result < 0)	{		logError("file: "__FILE__", line: %d, " \			"getsockopt failed, errno: %d, error info: %s.", \			__LINE__, errno, strerror(errno));		return errno != 0 ? errno : ENOMEM;	}	printf("send buff size: %d\n", bytes);	result = getsockopt(fd, SOL_SOCKET, SO_RCVBUF,			&bytes, (socklen_t *)&size);	if (result < 0)	{		logError("file: "__FILE__", line: %d, " \			"getsockopt failed, errno: %d, error info: %s.", \			__LINE__, errno, strerror(errno));		return errno != 0 ? errno : ENOMEM;	}	printf("recv buff size: %d\n", bytes);	}	*/	flags = 1;	result = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, \			(char *)&flags, sizeof(flags));	if (result < 0)	{		logError("file: "__FILE__", line: %d, " \			"setsockopt failed, errno: %d, error info: %s.", \			__LINE__, errno, strerror(errno));		return errno != 0 ? errno : ENOMEM;	}	flags = fcntl(fd, F_GETFL, 0);	if (flags < 0)	{		logError("file: "__FILE__", line: %d, " \			"fcntl failed, errno: %d, error info: %s.", \			__LINE__, errno, strerror(errno));		return errno != 0 ? errno : EACCES;	}	if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)	{		logError("file: "__FILE__", line: %d, " \			"fcntl failed, errno: %d, error info: %s.", \			__LINE__, errno, strerror(errno));		return errno != 0 ? errno : EACCES;	}	return 0;}int gethostaddrs(char ip_addrs[][IP_ADDRESS_SIZE], \	const int max_count, int *count){	struct hostent *ent;	char hostname[128];	char ip_addr[IP_ADDRESS_SIZE];	int k;	*count = 0;	if (gethostname(hostname, sizeof(hostname)) != 0)	{		logError("file: "__FILE__", line: %d, " \			"call gethostname fail, " \			"error no: %d, error info: %s", \			__LINE__, errno, strerror(errno));		return errno != 0 ? errno : EFAULT;	}	memset(ip_addr, 0, sizeof(ip_addr));        ent = gethostbyname(hostname);	if (ent == NULL)	{		*count = 0;		return h_errno != 0 ? h_errno : EFAULT;	}	k = 0;	while (ent->h_addr_list[k] != NULL)	{		if (*count >= max_count)		{			break;		}		if (inet_ntop(ent->h_addrtype, ent->h_addr_list[k], \			ip_addrs[*count], IP_ADDRESS_SIZE) != NULL)		{			(*count)++;		}		k++;	}	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -