{"id":18077380,"url":"https://github.com/parisneo/udp","last_synced_at":"2025-04-05T20:13:03.341Z","repository":{"id":39727299,"uuid":"504698336","full_name":"ParisNeo/udp","owner":"ParisNeo","description":"Simple Linux C UDP library to help using udp communication on Linux platforms in a very simple way.","archived":false,"fork":false,"pushed_at":"2022-07-09T20:25:36.000Z","size":52,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-18T19:46:45.460Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ParisNeo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-06-17T23:33:00.000Z","updated_at":"2022-06-27T23:09:40.000Z","dependencies_parsed_at":"2022-09-20T09:34:53.094Z","dependency_job_id":null,"html_url":"https://github.com/ParisNeo/udp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisNeo%2Fudp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisNeo%2Fudp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisNeo%2Fudp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ParisNeo%2Fudp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ParisNeo","download_url":"https://codeload.github.com/ParisNeo/udp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247393572,"owners_count":20931813,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-31T11:24:26.666Z","updated_at":"2025-04-05T20:13:03.296Z","avatar_url":"https://github.com/ParisNeo.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# udp\nSimple Linux C udp library to help using udp communication on Linux platforms in a very simple way.\n![logo](logo.png)\n\nudp is a very simple connectionless transportation protocol. Basically, one process opens a port ans listens, and the other one sends data to that port. We don't have acknowledgement mechanism, so you send and don't know if the receiver received what we've sent. But it is a very lightweight and fast communication protocol suited for realtime data transfer and inter processes communication.\n# Build\nTo build and install the library on your system, just use:\n```console\nfoo@bar:~$ make clean;make; sudo make install\n```\nYou can skip the make install part and copy the .a output file as well as the header file to your project to link it statically with your code.\n# Usage\nOnce the library is installed on your system or after adding the header file and the .a file to your project, you can use it like this.\n\nFirst import the header files\n```c\n#include\u003cudp.h\u003e\n```\nif you are using c++, make sure you add extern \"C\" protection\n```c\n#ifdef __cplusplus\nextern \"C\"\n{\n#endif\n    // udp library\n    #include \u003cudp.h\u003e\n#ifdef __cplusplus\n}\n#endif\n```\nNow you need to declare your uart configuration object:\n```c\n// UART configuration\nudp_cfg_list udp_configurations;\n```\n\nIn your main, initialize the uart configuration with whatever you need. First call the init_uart_cfg_list function to initialize the structure with default values \n\n```c\n// Initialize every thing to default\ninit_udp_cfg(\u0026udp_configuration);\n```\nNow you can setup custom port number and address.\n\n```c\n// Set port and baudrate\nudp_configuration.port = 9600;\nstrcpy(udp_configuration.str_address, \"192.168.1.1\");\n```\nYou can use any char* instead of the \"192.168.1.1\" or put any other address or port.\n\n\nWe are quite there. Now we configure the udp port. There are two functions. One to create a udp port that sends data (the port and adress are those of the destination), or a udp port that accepts data (the port and adress are those of the current process).\n\nHere we assume you created and setup udp_in_configuration as mentioned above.\n```c\n    // Connecting UART \n    printf(\"Configuring udp %s:%d ...\", udp_in_configuration.str_address, udp_in_configuration.port);\n    if(configure_udp_in(\u0026udp_in_configuration))\n    {\n        printf(\"OK\\n\"); //Success\n    }\n    else\n    {\n        printf(\"NOK\\n\");  // Failure\n        exit(1);\n    }\n```\nTo build a udp out connection use:\n```c\n    // Connecting UART \n    printf(\"Configuring udp %s:%d ...\", udp_out_configuration.str_address, udp_out_configuration.port);\n    if(configure_udp_out(\u0026udp_out_configuration))\n    {\n        printf(\"OK\\n\"); //Success\n    }\n    else\n    {\n        printf(\"NOK\\n\");  // Failure\n        exit(1);\n    }\n```\n\nNow you can talk to your device.\n\nTo read and write you need to declare input and output buffers:\n```c\n    char read_buf [256];\n    char write_buf [256];\n```\nAnd now you can read into the input buffer\n```c\n    udp_receive(\u0026udp_in_configuration, (void *)read_buf, 256);\n```\nhere 256 is the size of the buffer, MSG_WAITALL to force waiting the entire message. len is the length of the \n\nTo send data to your device you can use:\n```c\n    udp_send(\u0026udp_out_configuration, (void *)write_buf, 256);\n```\nhere len is the length of the data to send. If write_buf contains caracters, you may use strlen(write_buf) instead.\n\n# Complete example\nLet's write a sender receiver application.\n\nHere is a listener\n```c\n#include\u003cstdio.h\u003e\n#include\u003cudp.h\u003e\n\nudp_cfg udp_in_configuration;\nchar read_buf[256];\n\nint main(int argc, char ** argv)\n{\n    // Initialize every thing to default\n    init_udp_cfg(\u0026udp_in_configuration);    \n    // Set port and baudrate\n    udp_in_configuration.port = 9600;\n    strcpy(udp_in_configuration.str_address, \"192.168.1.1\");\n    // Configure as sender\n    if(configure_udp_in(\u0026udp_in_configuration))\n    {\n        printf(\"OK\\n\"); //Success\n    }\n    else\n    {\n        printf(\"NOK\\n\");  // Failure\n        exit(1);\n    }\n    // Now we are ready to listen\n    udp_receive(\u0026udp_in_configuration, (void *)read_buf, 256);\n    // That's it!\n    printf(\"Received : %s\", read_buf)\n}\n```\n\n\nHere is a sender\n```c\n#include\u003cstdio.h\u003e\n#include\u003cudp.h\u003e\n\nudp_cfg udp_out_configuration;\nchar write_buf[256];\n\nint main(int argc, char ** argv)\n{\n    // Initialize every thing to default\n    init_udp_cfg(\u0026udp_out_configuration);    \n    // Set port and baudrate\n    udp_out_configuration.port = 9600;\n    strcpy(udp_out_configuration.str_address, \"192.168.1.1\");\n    // Configure as sender\n    if(configure_udp_out(\u0026udp_out_configuration))\n    {\n        printf(\"OK\\n\"); //Success\n    }\n    else\n    {\n        printf(\"NOK\\n\");  // Failure\n        exit(1);\n    }\n    // Now we are ready to send\n    strcpy(write_buf, \"Hello world\");\n    udp_send(\u0026udp_out_configuration, (void *)write_buf, 256);\n    // That's it!\n}\n```\n# Information\nThis is one of multiple libraries I have developed to simplify some tasks we do with C on linux. The objective is for it to be open source, eazy to use and compatible with both c and cpp. The build system is make and is compatible with gcc building system. It is very eazy to use this with cmake or other build systems. All these libraries have been tested on raspberry pi with raspbian. They help starting a new application that requires configuration, communication with arduino tools and spreading information between multiple services on the raspberry pi.\n\nThe licence is MIT, so you can use this code in your projects without worrying about licence contamination that could happen when using GPL licences. So you still can use it for free in commercial applications.\n\nTests and bugfixes are welcome. Just clone it, repare it and send a pull request. I want to keep this code as clean and simple as possible so please avoid feature creaping.\n\n# Useful links\nCheck out my [cfg library](https://github.com/ParisNeo/cfg) built in the same spirit as this library.\nCheck out my [uart library](https://github.com/ParisNeo/uart) built in the same spirit as this library.\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparisneo%2Fudp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparisneo%2Fudp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparisneo%2Fudp/lists"}