📄 udp_send.c
字号:
// The Rabbit sends periodic datagrams to a remote host.
// A timer determines the time between datagrams.
// 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 send datagrams from
#define LOCAL_PORT 5551
// The IP address and port to send datagrams to.
// If REMOTE_IP is a host name, you must define MY_NAMESERVER
// in tcp_config.lib or this application.
#define REMOTE_IP "192.168.111.5"
#define REMOTE_PORT 5550
//Allocate a socket buffer for the UDP socket.
#define MAX_UDP_SOCKET_BUFFERS 1
// All C functions not declared as root go to extended memory.
#memmap xmem
// The dcrtcp library supports UDP and IP communications.
#use "dcrtcp.lib"
//The socket to use for UDP communications.
udp_Socket my_socket;
int sequence;
main() {
// Initialize network support and send datagrams.
int return_value;
sequence = 255;
// sock_init must be called before using other functions in dcrtcp.lib.
// If return_value isn't zero, the network isn't available.
return_value = sock_init();
if (return_value == 0) {
printf("Nework support is initialized.\n");
}
else {
printf("The network is not available.\n");
exit(0);
}
// Attempt to open the UDP socket using the specified IP address and port
// numbers.
// If udp_open fails, stop the program.
// The resolve() function converts an IP address in dotted-quad format
// or a host name to an IP address expresses as a longword.
if(!udp_open(&my_socket, LOCAL_PORT, resolve(REMOTE_IP), REMOTE_PORT, NULL)) {
printf("udp_open failed.\n");
exit(0);
}
else {
printf("udp_open succeeded.\n");
}
// Port G bit 6 controls LED DS1 on the RCM3200 development board.
// Set bit 6 in PGDDR to configure the bit as an output.
WrPortI(PGDDR, NULL, 0x40);
while(1) {
// Endless loop.
// Process network packets.
tcp_tick(NULL);
costate {
//wait DelaySec seconds between sends.
waitfor(DelaySec(1));
//send a datagram to the remote host.
send_packet();
}
} // end while(1)
} // end main()
int send_packet(void) {
// send a datagram to the remote host.
char send_buffer[2];
int buffer_length;
int return_value;
int test_bit;
// Increment the sequence number and reset to zero after sending 255.
sequence++;
if (sequence > 255) {
sequence = 0;
}
// Place the bytes to send in send_buffer.
// The first byte in the datagram is the sequence number.
send_buffer[0] = (char)sequence;
// Code to send text messages (requires larger send_buffer).
//sprintf(send_buffer, "Message number %d", sequence);
// The second byte in the datagram is the state of Port G, bit 6.
test_bit = (BitRdPortI(PGDR, 6));
send_buffer[1] = (char)test_bit;
// Toggle the port bit on every send.
if (test_bit == 0) {
// When the bit is 0, the LED is on, so switch it off.
BitWrPortI(PGDR, &PGDRShadow, 1, 6);
}
else {
// When the bit is 1, the LED is off, so switch it on.
BitWrPortI(PGDR, &PGDRShadow, 0, 6);
}
// Send the datagram using Dynamic C's udp_send function.
// my_socket is the UDP socket.
// send_buffer contains the data to send.
// sizeof(send_buffer is the number of bytes in send_buffer to send.
// On success, returns the number of bytes sent.
return_value = udp_send(&my_socket, send_buffer, sizeof(send_buffer));
if (return_value < 0) {
// On failure, close the socket and attempt to reopen it.
printf("Error sending datagram. Closing and reopening socket.\n");
sock_close(&my_socket);
// If reopening fails, stop the program.
if(!udp_open(&my_socket, LOCAL_PORT, resolve(REMOTE_IP), REMOTE_PORT, NULL)) {
printf("udp_open failed.\n");
exit(0);
}
}
else {
printf("Sent: %d %d \n", send_buffer[0], send_buffer[1]);
}
return 1;
} // end send_packet()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -