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

📄 tcp_sender.c

📁 很多仪器都输出同步时钟
💻 C
字号:
//   File: tcp_sender.c
// Author: Trampus Richmond / David Van Brink
//
// +------------------------------------
// | Sockets-based test program that
// | sends a lot of data over a TCP connection.
// |
// | Works well in conjunction with "tcp_receiver.c",
// | which is a Nios plugs library based program that
// | receives a lot of data on a TCP connection.
// |
// | You can build this program under Cygwin or
// | Unix (I tested it on FreeBSD, should work similarly
// | on others, give or take an include file here & there).
// |
// | gcc tcp_sender.c
// |


// |
// | Hardcoded Configuration
// |

#define k_nios_ip_address "137.57.136.11"

#define k_port_number 1067
#define k_file_size 104001191 // simulates transmission of this big a file
#define k_packet_size 234   // broken into blobs this size


// | Includes

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


// | Local Prototypes

long get_milliseconds(void);

// | Routines

// +--------------------------
// | print_time prints some info about number
// | of bytes sent, percentage complete, and
// | bytes/second sent. But it only does
// | this every so many times it's called,
// | as set by the update period constant below.
// |

void print_time(int bytes_just_sent,int force)
	{
	#define k_update_period 30 // only print the stuff for every 30 packets
	static int bytes_sent_this_update = 0;
	static int update_count = -1;
	static int last_milliseconds;
	static int total_bytes_sent;

	float percent;
	static float last_percent = 0.0;

	int milliseconds_now = get_milliseconds();

	if(force < 0)	// means "start of new transmission"
		{
		update_count = 0;
		last_milliseconds = milliseconds_now;
		total_bytes_sent = 0;
		last_percent = 0.0;
		}

	bytes_sent_this_update += bytes_just_sent;
	total_bytes_sent += bytes_just_sent;



	percent = (float)total_bytes_sent * 100 / k_file_size;

	update_count++;
	if((percent - last_percent > 2) || force)
		{
		int milliseconds = milliseconds_now;
		int interval = milliseconds - last_milliseconds;
		double bytes_per_second;

		if(interval)
			bytes_per_second =
					(float)bytes_sent_this_update * 1000.0 / (float)interval;
		else
			bytes_per_second = 0;

		update_count = 0;
		bytes_sent_this_update = 0;
		last_milliseconds = milliseconds;

		printf("Sent: %09d of %09d (%6.2f%%) at %8.0f bytes/second\n",
				total_bytes_sent,
				k_file_size,
				((float)total_bytes_sent * 100 / k_file_size),
				bytes_per_second );

		last_percent = percent;
		}
	}



int main(int argc, char* argv[])
	{
	long long all_the_bytes = 0;
	long run_number = 0;
	int packet_size = k_packet_size;
	struct sockaddr_in addr = {0,0,0,0,0};
	int err;
	unsigned int sfd;
	void *data = 0;
	fd_set sfd_set;
	unsigned int total_data;

	int failure_count = 0;	// how many times we sent less than all...

go_again:
	total_data = 0;

	printf("Starting tcp_sender.\n");
	printf("\n\n");
	printf("About to send %d bytes to %s on port %d, in chunks of %d.\n\n",
			k_file_size,
			k_nios_ip_address,
			k_port_number,
			k_packet_size);

	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = inet_addr(k_nios_ip_address);
	addr.sin_port = htons(k_port_number);

	sfd = socket (AF_INET, SOCK_STREAM,0);
	printf("socket result sfd = %d = 0x%08x\n",sfd,sfd);

	err = connect (sfd, (struct sockaddr *) &addr, sizeof(addr));
	printf("connect result err = %d = 0x%08x\n",err,err);

	if(err != 0)
		{
		printf("Error Calling Connect()\n");
		goto go_home;
		}
	data = malloc(k_packet_size);
        memset(data, 89, k_packet_size);

	print_time(0,-1);

	while(total_data < k_file_size)
		{
		int this_packet;

		this_packet = k_file_size - total_data;
		if(this_packet > k_packet_size )
			this_packet = k_packet_size;

		// (No need to select or sleep, the tcp flow
		// control keeps it all good, babe.)

		err = send(sfd, (char *)data, this_packet,0);

		if(err < 0)
			{
			printf("\n\nError: Sending Data\n");
			printf("\n\nError Code: %d\n", err);
			failure_count++;
			goto go_home;
			}
		else
			{
			total_data += err;
			print_time(this_packet,0);
			}

		}

go_home:
	print_time(0,1);
	printf("\n\n\nAll done, this time.\n\n");
	close(sfd);
	sfd = 0;

	run_number++;
	all_the_bytes += total_data;
	printf("\n\nCompleted run number %d with %lld bytes.\n\n",
			run_number,
			all_the_bytes);
	printf("failure_count = %d\n\n",failure_count);

	goto go_again;
	return 0;
}

long get_milliseconds(void)
	{
	static struct timeval theTime;
	struct timezone dummyTimeZone;
	long milliseconds;

	gettimeofday(&theTime,&dummyTimeZone);

	milliseconds = theTime.tv_usec / 1000
			+ theTime.tv_sec * 1000;

	return milliseconds;
	}

⌨️ 快捷键说明

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