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

📄 sockopt.c

📁 文件系统源代码!!!!! 文件系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*** Copyright (C) 2008 Happy Fish / YuQing** FastDFS may be copied only under the terms of the GNU General* Public License V3, which may be found in the FastDFS source kit.* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.**///socketopt.c#include "common_define.h"#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <time.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netinet/tcp.h>#include <netdb.h>#include <fcntl.h>#ifdef USE_SENDFILE#ifdef OS_LINUX#include <sys/sendfile.h>#else#ifdef OS_FREEBSD#include <sys/uio.h>#endif#endif#endif#include "sockopt.h"#include "logger.h"#include "fdfs_global.h"int tcpgets(int sock, char* s, const int size, const int timeout){	int result;	char t;	int i=1;	if (s == NULL || size <= 0)	{#ifdef __DEBUG__		fprintf(stderr,"%s,%d:tcpgets argument is illegal.\n",				__FILE__,__LINE__);#endif		return EINVAL;	}	while (i < size)	{		result = tcprecvdata(sock, &t, 1, timeout);		if (result != 0)		{#ifdef __DEBUG__			fprintf(stderr,"%s,%d:tcpgets call tcprecvdata failed.\n",					__FILE__,__LINE__);#endif			*s = 0;			return result;		}		if (t == '\r')		{			continue;		}		if (t == '\n')		{			*s = t;			s++;			*s = 0;			return 0;		}		*s = t;		s++,i++;	}	*s = 0;	return 0;}int tcprecvdata_ex(int sock, void *data, const int size, \		const int timeout, int *count){	int left_bytes;	int read_bytes;	int res;	int ret_code;	unsigned char* p;	fd_set read_set;	struct timeval t;	if (data == NULL)	{#ifdef __DEBUG__		fprintf(stderr,"%s,%d:tcprecvdata argument data is NULL.\n",			__FILE__,__LINE__);#endif		return EINVAL;	}	FD_ZERO(&read_set);	FD_SET(sock, &read_set);	read_bytes = 0;	ret_code = 0;	p = (unsigned char*)data;	left_bytes = size;	while (left_bytes > 0)	{		if (timeout <= 0)		{			res = select(sock+1, &read_set, NULL, NULL, NULL);		}		else		{			t.tv_usec = 0;			t.tv_sec = timeout;			res = select(sock+1, &read_set, NULL, NULL, &t);		}				/*			struct timespec ts;		sigset_t sigmask;		sigemptyset(&sigmask);		if (timeout <= 0)		{			res = pselect(sock+1, &read_set, NULL, NULL, NULL, &sigmask);		}		else		{			ts.tv_nsec = 0;			ts.tv_sec = timeout;			res = pselect(sock+1, &read_set, NULL, NULL, &ts, &sigmask);		}		*/			if (res < 0)		{#ifdef __DEBUG__			fprintf(stderr,"%s,%d:tcprecvdata call select failed:%s.\n",				__FILE__,__LINE__,strerror(errno));#endif			ret_code = errno != 0 ? errno : EINTR;			break;		}		else if (res == 0)		{#ifdef __DEBUG__			fprintf(stderr,"%s,%d:tcprecvdata call select timeout.\n",				__FILE__,__LINE__);#endif			ret_code = ETIMEDOUT;			break;		}				//if (FD_ISSET(sock, &read_set))		{			read_bytes = read(sock, p, left_bytes);			if (read_bytes < 0)			{#ifdef __DEBUG__				fprintf(stderr,"%s,%d:tcprecvdata call read failed:%s.\n",					__FILE__,__LINE__,strerror(errno));#endif				ret_code = errno != 0 ? errno : EINTR;				break;			}			if (read_bytes == 0)			{#ifdef __DEBUG__				fprintf(stderr, "%s,%d:tcprecvdata call read return 0, remote close connection? errno:%d, error info:%s.\n",					__FILE__, __LINE__, errno, strerror(errno));#endif				ret_code = ENOTCONN;				break;			}			left_bytes -= read_bytes;			p += read_bytes;		}	}	if (count != NULL)	{		*count = size - left_bytes;	}	return ret_code;}int tcpsenddata(int sock, void* data, const int size, const int timeout){	int left_bytes;	int write_bytes;	int result;	unsigned char* p;	fd_set write_set;	struct timeval t;	if (data == NULL)	{#ifdef __DEBUG__		fprintf(stderr,"%s,%d:tcpsenddata argument data is NULL.\n",			__FILE__,__LINE__);#endif		return EINVAL;	}	FD_ZERO(&write_set);	FD_SET(sock, &write_set);	p = (unsigned char*)data;	left_bytes = size;	while (left_bytes > 0)	{		if (timeout <= 0)		{			result = select(sock+1, NULL, &write_set, NULL, NULL);		}		else		{			t.tv_usec = 0;			t.tv_sec = timeout;			result = select(sock+1, NULL, &write_set, NULL, &t);		}		if (result < 0)		{#ifdef __DEBUG__			fprintf(stderr,"%s,%d:tcpsenddata call select failed:%s.\n",				__FILE__,__LINE__,strerror(errno));#endif			return errno != 0 ? errno : EINTR;		}		else if (result == 0)		{#ifdef __DEBUG__			fprintf(stderr,"%s,%d:tcpsenddata call select timeout.\n",				__FILE__,__LINE__);#endif			return ETIMEDOUT;		}		//if (FD_ISSET(sock, &write_set))		{			write_bytes = write(sock, p, left_bytes);			if (write_bytes < 0)			{#ifdef __DEBUG__				fprintf(stderr,"%s,%d:tcpsenddata call write failed:%s.\n",					__FILE__,__LINE__,strerror(errno));#endif							return errno != 0 ? errno : EINTR;			}			left_bytes -= write_bytes;			p += write_bytes;		}	}	return 0;}int connectserverbyip(int sock, char* ip, short port){	int result;	struct sockaddr_in addr;	addr.sin_family = PF_INET;	addr.sin_port = htons(port);	result = inet_aton(ip, &addr.sin_addr);	if (result == 0 )	{#ifdef __DEBUG__		fprintf(stderr,"file: %s, line: %d:connectserverbyip call " \			"inet_aton failed: errno: %d, error info: %s.\n",			__FILE__, __LINE__, errno, strerror(errno));#endif		return EINVAL;	}	result = connect(sock, (const struct sockaddr*)&addr, sizeof(addr));	if (result < 0)	{#ifdef __DEBUG__		fprintf(stderr,"file: %s, line: %d, connectserverbyip " \			"%s:%d, call connect is failed, " \			"errno: %d, error info: %s.\n",			__FILE__, __LINE__, ip, port, errno, strerror(errno));#endif		return errno != 0 ? errno : EINTR;	}	return 0;}in_addr_t getIpaddr(getnamefunc getname, int sock, \		char *buff, const int bufferSize){	struct sockaddr_in addr;	socklen_t addrlen;	memset(&addr, 0, sizeof(addr));	addrlen = sizeof(addr);		if (getname(sock, (struct sockaddr *)&addr, &addrlen) != 0)	{		buff[0] = '\0';		return INADDR_NONE;	}		if (addrlen > 0)	{		if (inet_ntop(AF_INET, &addr.sin_addr, buff, bufferSize) == NULL)		{			*buff = '\0';		}	}	else	{		*buff = '\0';	}		return addr.sin_addr.s_addr;}char *getHostnameByIp(const char *szIpAddr, char *buff, const int bufferSize){	struct in_addr ip_addr;	struct hostent *ent;	if (inet_pton(AF_INET, szIpAddr, &ip_addr) != 1)	{		*buff = '\0';		return buff;	}	ent = gethostbyaddr((char *)&ip_addr, sizeof(ip_addr), AF_INET);	if (ent == NULL || ent->h_name == NULL)	{		*buff = '\0';	}	else	{		snprintf(buff, bufferSize, "%s", ent->h_name);	}	return buff;}in_addr_t getIpaddrByName(const char *name, char *buff, const int bufferSize){    	struct in_addr ip_addr;	struct hostent *ent;	in_addr_t **addr_list;	if (inet_pton(AF_INET, name, &ip_addr) == 1)	{		if (buff != NULL)		{			snprintf(buff, bufferSize, "%s", name);		}		return ip_addr.s_addr;	}	ent = gethostbyname(name);	if (ent == NULL)	{		return INADDR_NONE;	}        addr_list = (in_addr_t **)ent->h_addr_list;	if (addr_list[0] == NULL)	{		return INADDR_NONE;	}	memset(&ip_addr, 0, sizeof(ip_addr));	ip_addr.s_addr = *(addr_list[0]);	if (buff != NULL)	{		if (inet_ntop(AF_INET, &ip_addr, buff, bufferSize) == NULL)		{			*buff = '\0';		}	}	return ip_addr.s_addr;}int nbaccept(int sock, const int timeout, int *err_no){	struct sockaddr_in inaddr;	unsigned int sockaddr_len;	fd_set read_set;	struct timeval t;	int result;		if (timeout > 0)	{		t.tv_usec = 0;		t.tv_sec = timeout;				FD_ZERO(&read_set);		FD_SET(sock, &read_set);				result = select(sock+1, &read_set, NULL, NULL, &t);		if (result == 0)  //timeout		{			*err_no = ETIMEDOUT;			return -1;		}		else if (result < 0) //error		{			*err_no = errno != 0 ? errno : EINTR;			return -1;		}			/*			if (!FD_ISSET(sock, &read_set))		{			*err_no = EAGAIN;			return -1;		}		*/	}		sockaddr_len = sizeof(inaddr);	result = accept(sock, (struct sockaddr*)&inaddr, &sockaddr_len);	if (result < 0)

⌨️ 快捷键说明

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