666|1

250

帖子

5

TA的资源

纯净的硅(初级)

楼主
 

【得捷电子Follow me第4期】基础任务二:主控板建立TCPIP或UDP服务器 [复制链接]

 

 这部分我们先看UDP协议,UDP他也是有例子的,我们可以查看下。


#include <Ethernet.h>

#include <EthernetUdp.h>



// Enter a MAC address and IP address for your controller below.

// The IP address will be dependent on your local network:

byte mac[] = {

  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED

};

IPAddress ip(192, 168, 2, 177);



unsigned int localPort = 8888;      // local port to listen on



// buffers for receiving and sending data

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  // buffer to hold incoming packet,

char ReplyBuffer[] = "acknowledged";        // a string to send back



// An EthernetUDP instance to let us send and receive packets over UDP

EthernetUDP Udp;



void setup() {

  // You can use Ethernet.init(pin) to configure the CS pin

  //Ethernet.init(10);  // Most Arduino shields

  //Ethernet.init(5);   // MKR ETH shield

  //Ethernet.init(0);   // Teensy 2.0

  //Ethernet.init(20);  // Teensy++ 2.0

  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet

  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  Ethernet.init(17);  // WIZnet W5100S-EVB-Pico W5500-EVB-Pico W6100-EVB-Pico



  // start the Ethernet

  Ethernet.begin(mac, ip);



  // Open serial communications and wait for port to open:

  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }



  // Check for Ethernet hardware present

  if (Ethernet.hardwareStatus() == EthernetNoHardware) {

    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");

    while (true) {

      delay(1); // do nothing, no point running without Ethernet hardware

    }

  }

  if (Ethernet.linkStatus() == LinkOFF) {

    Serial.println("Ethernet cable is not connected.");

  }



  // start UDP

  Udp.begin(localPort);

}



void loop() {

  // if there's data available, read a packet

  int packetSize = Udp.parsePacket();

  if (packetSize) {

    Serial.print("Received packet of size ");

    Serial.println(packetSize);

    Serial.print("From ");

    IPAddress remote = Udp.remoteIP();

    for (int i=0; i < 4; i++) {

      Serial.print(remote[i], DEC);

      if (i < 3) {

        Serial.print(".");

      }

    }

    Serial.print(", port ");

    Serial.println(Udp.remotePort());



    // read the packet into packetBufffer

    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);

    Serial.println("Contents:");

    Serial.println(packetBuffer);



    // send a reply to the IP address and port that sent us the packet we received

    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());

    Udp.write(ReplyBuffer);

    Udp.endPacket();

  }

  delay(10);

}

这部分还比较好理解,主要就是创建一个UDP服务,端口是8888,通过电脑软件也建一个UDP服务连接他。

 

连接目标机器的IP地址和端口。

 

连接上之后会有反馈。接收信息也会打印的。

 

 

网络分析就是看数据段,数据是吻合的。

 

最新回复

创建一个UDP服务,端口是8888,通过电脑软件也建一个UDP服务连接他,这是重点,,,   详情 回复 发表于 2024-2-22 07:22
点赞 关注
 
 

回复
举报

6845

帖子

0

TA的资源

五彩晶圆(高级)

沙发
 

创建一个UDP服务,端口是8888,通过电脑软件也建一个UDP服务连接他,这是重点,,,

 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/7 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表