Esempio n. 1
0
main(int argc, char *argv[]) {
	pthread_t recv_thread; /* thread variables */

	sfd = socket(AF_INET, SOCK_DGRAM, 0);
	bzero(&server, sizeof(server));
	server.sin_family = AF_INET;
	client.sin_family = AF_INET;
	server.sin_port = htons(1300);
	if (argc != 1) {
		printf("  argc :%d \n",argc);
		printf("./udp_server \n");
		return 1;
	}
	signal(SIGTERM, sigTerm);
	signal(SIGINT, sigTerm);
	inet_aton("0.0.0.0", &server.sin_addr);
	bind(sfd, &server, sizeof(server));
	send_pkts = recv_pkts = 0;

	pkt_size = 0;
	total_pkts = 0;

	printf(" Starting the Server:  \n");
	recv_func();
	recv_stop = 1;

	printf(
			"NEW pktsize:%d send:%d recved:%d  loss:%d SBit rate:%d Mbps Rbir rate:%d\n",
			pkt_size, send_pkts, recv_pkts, (send_pkts - recv_pkts),
			(send_pkts * pkt_size * 8) / (duration * 1000000),
			(recv_pkts * pkt_size * 8) / (duration * 1000000));
} 
void sig_urg(int signum)
{
	int	sav_err;
	char buf[1024];
	bzero(buf, sizeof(buf));
	recv_func(confd, buf, sizeof(buf), MSG_OOB);
	fprintf(stdout, "this is urgent data :%s\n",buf);
	sav_err	= errno;
	errno 	= sav_err;
	return;
}
Esempio n. 3
0
int tcpdiscard(int sock, const int bytes, const int timeout, \
		int64_t *total_recv_bytes)
{
	char buff[FDFS_WRITE_BUFF_SIZE];
	int remain_bytes;
	int recv_bytes;
	int result;
	int flags;
	int count;
	tcprecvdata_exfunc recv_func;

	*total_recv_bytes = 0;
	flags = fcntl(sock, F_GETFL, 0);
	if (flags < 0)
	{
		return errno != 0 ? errno : EACCES;
	}

	if (flags & O_NONBLOCK)
	{
		recv_func = tcprecvdata_nb_ex;
	}
	else
	{
		recv_func = tcprecvdata_ex;
	}
	
	remain_bytes = bytes;
	while (remain_bytes > 0)
	{
		if (remain_bytes > sizeof(buff))
		{
			recv_bytes = sizeof(buff);
		}
		else
		{
			recv_bytes = remain_bytes;
		}

		result = recv_func(sock, buff, recv_bytes, \
				timeout, &count);
		*total_recv_bytes += count;
		if (result != 0)
		{
			return result;
		}

		remain_bytes -= recv_bytes;
	}

	return 0;
}
Esempio n. 4
0
int tcprecvfile_ex(int sock, const char *filename, const int64_t file_bytes, \
		const int fsync_after_written_bytes, \
		unsigned int *hash_codes, const int timeout)
{
	int fd;
	char buff[FDFS_WRITE_BUFF_SIZE];
	int64_t remain_bytes;
	int recv_bytes;
	int written_bytes;
	int result;
	int flags;
	tcprecvdata_exfunc recv_func;

	flags = fcntl(sock, F_GETFL, 0);
	if (flags < 0)
	{
		return errno != 0 ? errno : EACCES;
	}

	if (flags & O_NONBLOCK)
	{
		recv_func = tcprecvdata_nb_ex;
	}
	else
	{
		recv_func = tcprecvdata_ex;
	}

	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
	if (fd < 0)
	{
		return errno != 0 ? errno : EACCES;
	}

	INIT_HASH_CODES4(hash_codes)
	
	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=recv_func(sock, buff, recv_bytes, \
				timeout, NULL)) != 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;
				}
			}
		}

		CALC_HASH_CODES4(buff, recv_bytes, hash_codes)

		remain_bytes -= recv_bytes;
	}

	close(fd);

	FINISH_HASH_CODES4(hash_codes)

	return 0;
}
Esempio n. 5
0
int tcprecvfile(int sock, const char *filename, const int64_t file_bytes, \
		const int fsync_after_written_bytes, const int timeout, \
		int64_t *true_file_bytes)
{
	int write_fd;
	char buff[FDFS_WRITE_BUFF_SIZE];
	int64_t remain_bytes;
	int recv_bytes;
	int written_bytes;
	int result;
	int flags;
	int count;
	tcprecvdata_exfunc recv_func;

	*true_file_bytes = 0;
	flags = fcntl(sock, F_GETFL, 0);
	if (flags < 0)
	{
		return errno != 0 ? errno : EACCES;
	}

	if (flags & O_NONBLOCK)
	{
		recv_func = tcprecvdata_nb_ex;
	}
	else
	{
		recv_func = tcprecvdata_ex;
	}

	write_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
	if (write_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;
		}

		result = recv_func(sock, buff, recv_bytes, \
				timeout, &count);
		if (result != 0)
		{
			if (file_bytes != INFINITE_FILE_SIZE)
			{
				close(write_fd);
				unlink(filename);
				return result;
			}
		}

		if (count > 0 && write(write_fd, buff, count) != count)
		{
			result = errno != 0 ? errno: EIO;
			close(write_fd);
			unlink(filename);
			return result;
		}

		*true_file_bytes += count;
		if (fsync_after_written_bytes > 0)
		{
			written_bytes += count;
			if (written_bytes >= fsync_after_written_bytes)
			{
				written_bytes = 0;
				if (fsync(write_fd) != 0)
				{
					result = errno != 0 ? errno: EIO;
					close(write_fd);
					unlink(filename);
					return result;
				}
			}
		}

		if (result != 0)  //recv infinite file, does not delete the file
		{
			int read_fd;
			read_fd = -1;

			do
			{
				if (*true_file_bytes < 8)
				{
					break;
				}

				read_fd = open(filename, O_RDONLY);
				if (read_fd < 0)
				{
					return errno != 0 ? errno : EACCES;
				}

				if (lseek(read_fd, -8, SEEK_END) < 0)
				{
					result = errno != 0 ? errno : EIO;
					break;
				}

				if (read(read_fd, buff, 8) != 8)
				{
					result = errno != 0 ? errno : EIO;
					break;
				}

				*true_file_bytes -= 8;
				if (buff2long(buff) != *true_file_bytes)
				{
					result = EINVAL;
					break;
				}

				if (ftruncate(write_fd, *true_file_bytes) != 0)
				{
					result = errno != 0 ? errno : EIO;
					break;
				}

				result = 0;
			} while (0);
		
			close(write_fd);
			if (read_fd >= 0)
			{
				close(read_fd);
			}

			if (result != 0)
			{
				unlink(filename);
			}

			return result;
		}

		remain_bytes -= count;
	}

	close(write_fd);
	return 0;
}
int main(int argc, char *argv[])
{
	/* conbine socket and bind and listen */
	int lisfd;
	lisfd = create_lisfd();
/* choise select or siganl for operation */
#define SELECT_SIGNAL_FLAG	1
#if SELECT_SIGNAL_FLAG
	/* set nonbloc for listen file descriptor */
	int opt;
	opt = fcntl_func(lisfd, F_GETFL, 0);
	fcntl_func(lisfd, F_SETFL, opt | O_NONBLOCK);
	/* set received buffer size */
	int recvbufsize = 1024;
	setsockopt_func(lisfd, SOL_SOCKET, SO_RCVBUF, &recvbufsize, sizeof(recvbufsize));
	/* waiting for connect of client */
	fd_set accset;
	FD_ZERO(&accset);
	FD_SET(lisfd, &accset);
	select_func(lisfd + 1, &accset, NULL, NULL, NULL);
#endif
	/* accept */
	confd = accept(lisfd, NULL, NULL);
/* choise select or siganl for operation */
#if SELECT_SIGNAL_FLAG
	int en = 1;
	signal_func(SIGURG, sig_urg);
	fcntl_func(confd, F_SETOWN, getpid());
	char buf[1024];
	int i = 0;
	for ( ; ; )
	{
		bzero(buf, sizeof(buf));
		if ((recv_func(confd, buf, sizeof(buf), 0)) == 0)
		{
			fprintf(stdout, "client close \n");
			close(confd);
			confd = -1;
			break;
		}
		fprintf(stdout, "%d :normal data buf :%s\n", ++i, buf);
		sleep(1);
	}
#else
	char buf[1024];
	fd_set rset;
	fd_set xset;
	FD_ZERO(&rset);
	FD_ZERO(&xset);
	int i = 0;
	int n = 0;
	for ( ; ; )
	{
		FD_SET(confd, &rset);
		FD_SET(confd, &xset);
		select_func(confd + 1, &rset, NULL, &xset, NULL);
		if (FD_ISSET(confd, &rset))
		{
			bzero(buf, sizeof(buf));
			if ((recv_func(confd, buf, sizeof(buf), 0)) == 0)
			{
				fprintf(stdout, "client close\n");
				close(confd);
				confd = -1;
				break;
			}
			fprintf(stdout, "this select operated %d :buf: %s\n", ++i, buf);
		}
		if (FD_ISSET(confd, &xset))
		{
			bzero(buf, sizeof(buf));
			/* enforce set not in first byte of buffer */
			buf[0] = 'x';
			n = recv_func(confd, buf, sizeof(buf), MSG_OOB);
			/* not urgent data also return 1 */
			fprintf(stdout, "n:%d\n", n);
			/* show recv buf content, already buf[0] rewritten */
			fprintf(stdout, "is urgent data :%s\n", buf);
		}
		sleep(1);
	}
#endif
	close(confd);
	close(lisfd);
	
	
	return 0;
}