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

📄 tcp_server.c

📁 embedded_ethernet_complete_code_rabbit
💻 C
字号:
// Basic TCP server.
// The Rabbit waits for a connection from a remote host.
// On establishing a connection, the Rabbit waits to receive a byte,
// then increments the byte and sends it back.
// The server accepts connection requests to the specified local port
// from any remote IP address and remote port.

// This file and other embedded Ethernet and Internet code and resources are
// available from www.Lvr.com.

// Select a network configuration from \lib\tcpip\tcp_config.lib
// in the Dynamic C distribution.
#define TCPCONFIG 1

// The local port to receive TCP segments on
#define LOCAL_PORT	5551

//The dcrtcp library supports IP and TCP.
#use "dcrtcp.lib"

char server_buffer[255];
int bytes_read;
int return_value;

//The socket to use for TCP communications.
tcp_Socket server_socket;

void service_request();

main() {

	// Wait for and respond to messages in received TCP segments.

	int data_available;

	// sock_init must be called before using other functions in dcrtcp.lib.
	// If the return value isn't zero, the network isn't available.
	return_value = sock_init();

	if (return_value == 0) {
		while(1) {

      // Endless loop.
			costate {

				// Handle incoming TCP requests.

				//	Listen for a connection request on the specified local port.
				tcp_listen(&server_socket,LOCAL_PORT,0,0,NULL,0);
				printf("Waiting for connection...\n");

            // Find out if a connection has been established.
            // If yes, continue with the next statement.
				// If no, go to the costatement's closing brace
				// and return here the next time through.
				waitfor (connection_established());
				printf("Connection established. \n");

				// Find out if a byte is available to read or a timeout has occurred.
            // DelaySec specifies the timeout in seconds.
				// If yes, continue with the next statement.
				// If no, go to the costatement's closing brace
				// and return here the next time through.
				waitfor (check_for_received_data() || DelaySec(20));

            // Find out if data has been received.
				data_available = check_for_received_data();

				// If data has been received, service the request.
				if (data_available > 0) {
					service_request();
					}
				else {
					printf("Timeout: the remote host provided no data. \n");
					}

				// The communications are complete, so close the connection.
				sock_close(&server_socket);
				printf("The connection is closed. \n");

			} // costate

			// Can place code to perform other tasks here.

		} // while(1)
	} // if (return_value == 0)
	else {

		// The call to sock_init failed.
		printf("The network is not available. \n");
		exit(0);
		}
} // end main

int connection_established() {

	// Return 1 if a connection has been established or if the socket is closed
   // but there are one or more bytes waiting to be read.
	tcp_tick(NULL);
	if (!sock_established(&server_socket) && sock_bytesready(&server_socket) == -1) {
		return 0;
	}
	else {
		return 1;
	}
} // end connection_established

int check_for_received_data() {

	// Process network packets on the TCP socket.
	tcp_tick(&server_socket);

	// Return 1 if there is a byte waiting to be read.
	if (sock_bytesready(&server_socket) == -1) {
		return 0;
	}
	else {
		return 1;
	}
} // end check_for_received_data

void service_request() {

	// Attempt to read a received byte.
	bytes_read = sock_fastread(&server_socket,server_buffer,sizeof(server_buffer));

 	if (bytes_read > 0) {

     // If a byte is available, write it to the stdio window.
		printf("Byte received = %d \n", server_buffer[0]);

		//Increment the received byte, resetting to 0 on 255.
		if (server_buffer[0] == 255) {
		  server_buffer[0] = 0;
		  }
		else {
		  server_buffer[0]=server_buffer[0] + 1;
		  }

		// Write the incremented byte to the socket to send it back to the sender.
		return_value = sock_write(&server_socket, server_buffer, 1);
		if (return_value != -1) {
			printf("Byte sent = %d \n", server_buffer[0]);
			}
		else {
			printf("Error writing to socket. \n");
			}
	}	//	if (bytes_read > 0)
	else {
		printf("Error reading from socket. \n");
	}
} // end service_request



⌨️ 快捷键说明

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