The Receive Callback
(ดูตัวอย่างโปรแกรมในลิ้งค์เอานะครับ)
The receive callback ถูกเรียก, เมื่อ the server รับบาง data จาก the client. ต่อไปนี้แสดงวิธีจัดการ data นี้.
• ก่อนอื่นผมกำลังสร้าง a new Packet Buffer (txBuf), ซึ่งจะถูกใช้ส่ง the data.
• char *remoteIP = ipaddr_ntoa(addr); แปลง the IP address จาก integer format เป็น the regular IP format.
• นี่เป็นเพียงกรณีที่เราต้องการใช้ the address and port ของ the client.
• ต่อไปฉันกำลังผสม the incoming data กับ some additional data.
• เพื่อส่ง data ใหม่นี้ไปยัง the client, สิ่งแรกเราจำเป็นต้องจัดสรรบาง memory สำหรับ the packet buffer.
• นี้สามารถถูกทำด้วย pbuf_alloc.
• จากนั้นเราจะคัดลอก the data ลงใน the packet buffer โดยใช้ pbuf_take.
• ถัดไปเราจะเชื่อมต่อถึง the client.
• นี้สามารถถูกทำโดยการใช้ udp_connect. The parameters, address and port ของ the client, เป็นส่วนหนึ่งของ the
function, เมื่อ udp_connect กำลังถูกเรียก.
• จากนั้นเราส่ง the data โดยใช้ the udp_send(upcb, txBuf);
ที่นี่ upcb คือ the control block ซึ่งมี the information ทั้งหมดเกี่ยวกับ the local and remote connection.
txBuf คือ the packet buffer, และมันมี the information ทั้งหมดเกี่ยวกับ the data.
• เมื่อ the data ถูกส่งแล้ว, เราจะยกเลิกการเชื่อมต่อ the client, ดังนั้น a new client สามารถเชื่อมต่อได้.
• และในท้ายสุดเราจะปล่อยฟรี the memories ทั้งหมด.