# \[Sharing\] Socket Programming 101 - UDP

**URL:** <https://forum.amebaiot.com/t/sharing-socket-programming-101-udp/1159>\
**Category:** Tools\
**Created:** [March 19, 2022, 3:28am UTC](https://forum.amebaiot.com/t/sharing-socket-programming-101-udp/1159 "2022-03-19T03:28:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dakamaster](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.amebaiot.com/dakamaster/32/3588_2.png) [@dakamaster](https://forum.amebaiot.com/u/dakamaster)\
**Post date:** [March 19, 2022, 3:28am UTC](https://forum.amebaiot.com/t/sharing-socket-programming-101-udp/1159/1 "2022-03-19T03:28:39Z")

</div>

In this article, we will have a basic understanding of Socket Programming first, then get into the detailed code of socket programming to further understand the difference between TCP and UDP transport protocol. Socket Programming is widely used in lower-level embedded systems such as Ameba (Amb21, 22, and 23) or BW16.

📣 I have uploaded the code for testing on GitHub, feel free to clone the repository to your local environment and perform the test, so that you can have a better understanding of the code.

🔗 [GitHub - S10143806H/socket-programming-exercises: Develop a socket program in UNIX/Linux that uses TCP and UDP as the transport protocol for transferring message between a client and server.](https://github.com/S10143806H/socket-programming-exercises)

* * *

# What is Socket?

- Used to do communication between 2 processes running on two different machines

- An object (special file) acts as an endpoint between two machines

# UDP (Connectionless)

 ![image](https://us1.discourse-cdn.com/flex016/uploads/ameba/original/1X/817273d6b82df2edf438913c293fddb3d50f6c19.png)

- Here is the flow of socket programming flow used in UDP. UDP is connectionless because there is not necessary to wait for ACK from the receiver end once the sender sent any data packet.

- Basically, the client-side will need to create a socket and using the `SendTo()` function transfer data to the server-side, then the socket will be closed after data is transmitted successfully.

- While on the UDP server-side, similarly, a socket needed to be created first, then waiting for the binding procedure from the UDP client side. Upon the connection is established, the server is ready for data received using the `ReceiveFrom()` function. Upon the data has been correctly received, the socket on the server side will be closed as well.

- Detailed illustration is showing in the graph below:  

## Code Explanation

I have attached the socket programming code of the UDP client [here](https://github.com/S10143806H/socket-programming-exercises/blob/main/Ex1/udp_client1.c), you can find code for UDP server, TCP client & server as well.

1. On the UDP client side, the code can be categorized into 2 sections:

2. For UDP Server, there are also two main functions:

In the next article, we will compare the socket programming process in TCP and compare it with UDP processes.

* * *

# Running Example Code

## Step 1

All the example code has been uploaded to GitHub, feel free to download and read the code. If you have a Linux OS or you have installed Cygwin on Windows OS, you can modify the `.c` code and use the command below:

```auto
gcc udp_server.c -o upd_server

```

to generate a `udp_server.exe` executable file. Similarly, a `udp_client.exe` can also be generated.

## Step 2

Upon the two executable files are generated successfully, you can use 2 terminals to execute server-side and client-side executable on two terminals using the command below:

```auto
.\udp_server

```
