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

📄 tcp_receiver.c

📁 一款基于FPGA的对于VGA实现全彩控制的程序
💻 C
字号:
// File: tcp_receiver.c
// Author: David Van Brink

// +---------------------------------------
// | This Nios plugs library program simply
// | listens on a TCP port (1067) and accepts
// | a connection. When the connection is
// | closed (either by pressing ESC or when
// | the remote device closes the connection)
// | we report how many bytes we got and the
// | rate of transfer.
// |
// | It is intended to work with the Unix
// | sockets program "tcp_sender.c".
// |
// | To build, nios-build tcp_receiver.c.
// |
// | ex:set tabstop=4:

// +--------------------
// | Includes
// |

#include "excalibur.h"
#include "plugs.h"
#include "plugs_example_designs.h"


// +--------------------
// | Constants & Defines
// |

#define k_local_port 1067


// +--------------------
// | Prototypes
// |


int tcp_listen_proc
		(
		int plug_handle,
		void *context,
		host_32 remote_ip_address,
		host_16 remote_port
		);
// +------------------------------
// | A pointer to an instance of
// | this structure is passed between
// | the routine r_tcp_accept and the
// | to plugs callback routines, so that
// | we dont have to use globals.
// |
// | The structure keeps track of connection
// | status, as well as bytes received and
// | how long it took.
// |

typedef struct
	{
	int tcp_plug;
	int connected; // 0:waiting, 1:connected, 2:disconnected
	net_32 remote_ip_address;
	net_16 remote_port;

	long bytes_received;
	long packets_received;
	unsigned long connection_opened_time;
	unsigned long connection_closed_time;
	} s_tcp_status;


static int r_listen_again(s_tcp_status *status)
	{
	int result;


	printf("\n======================================\n\n");
	printf("Waiting for connection on port %d.\n",k_local_port);

	status->bytes_received = 0;
	result = nr_plugs_listen
			(
			status->tcp_plug,
			tcp_listen_proc,
			status
			);

	status->connected = 0;
	
	return result;
	}

// +------------------------------------
// | This plugs callback routine is responsible
// | for accepting or rejecting an incoming
// | connection request to our tcp plug.
// | We always accept it, and take the
// | opportunity to print out some information
// | about who connected to the plug.
// |

int tcp_listen_proc
		(
		int plug_handle,
		void *context,
		host_32 remote_ip_address,
		host_16 remote_port
		)
	{
	s_tcp_status *status = context;

	status->connected = 1;
	status->remote_ip_address = remote_ip_address;
	status->remote_port = remote_port;

	status->connection_opened_time = nr_timer_milliseconds();
	status->bytes_received = 0;
	status->packets_received = 0;

	printf("Accepted connection from ");
	nr_plugs_print_ip_address(nr_h2n32(remote_ip_address));
	printf(" port %d\n",remote_port);

	printf("(Press ESC to close connection.)\n\n");

	return 0; // "no error" == accept the connection
	}

// +-------------------------------
// | For our receive-data proc, we do
// | pretty close to nothing. We do NOT verify
// | anything about the contents of the data;
// | we just add the number of bytes received
// | to our status, and bump the connection to
// | "disconnected" state if we receive
// | a NULL payload. (This is how the plugs library
// | tells an application that the connection has
// | been closed by the remote network device.
// |

int tcp_proc(int plug_handle,
		void *context,
		ns_plugs_packet *p,
		void *payload,
		int payload_length)
	{
	s_tcp_status *status = context;
	int i;

	if(payload)
		{
		status->bytes_received += payload_length;
		status->packets_received ++;
		}
	else
		status->connected = 2;  // not connected any more...
	
	return 0;
	}

// +----------------------------------
// | r_tcp_accept is the main part of
// | the program. It just waits for a connection,
// | then waits for the connection to be closed,
// | and prints out how many bytes it received,
// | and how long it took.
// |
// | Then, it waits for the next connection.
// |

int r_tcp_accept(void)
	{
	int result;
	s_tcp_status status = {0,0,0,0,0,0,0,0};
	long duration;
	int c;
	
	// | Create the tcp plug. We'll keep reusing it.

	result = nr_plugs_create
			(
			&status.tcp_plug,
			ne_plugs_tcp,
			k_local_port,
			tcp_proc,
			&status,
			0
			);

	printf("created plug %d error %d\n",status.tcp_plug,result);


listen_again:

	r_listen_again(&status);

	// |
	// | Wait for connection to be established, then
	// | go to the session routine. (Allow ESC to
	// | bail out.)
	// |

	while(status.connected == 0)
		{
		if(nr_uart_rxchar(0) == 27)
			goto listen_again;
		nr_plugs_idle();
		}
	
	// |
	// | Now, wait for the connection to close (and
	// | allow the ESC key to force it closed.
	// |

	while(status.connected == 1 && nr_uart_rxchar(0) != 27)
		nr_plugs_idle();
	
	printf("  bytes received: %10d\n",status.bytes_received);
	printf("packets received: %10d\n",status.packets_received);
	if(status.packets_received)
		printf("    bytes/packet: %10d\n",
				status.bytes_received / status.packets_received);
	printf("\n---\n\n");

	goto listen_again;

go_home:
	if(status.tcp_plug)
		result = nr_plugs_destroy(status.tcp_plug);
	return 0;
	}

int main(void)
	{
	int result;

	result = nr_plugs_initialize(0,0,
			__adapter__,
			__adapter_irq__,
			__adapter_struct_addr__);
	
	if(result)
		goto go_home;

	r_tcp_accept();

go_home:
	printf("error: %d\n",result);
	return 0;
	}

⌨️ 快捷键说明

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