Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending string over UDP in C++

Tags:

I would like to send a string: "Jane Doe" to intranet ip 192.168.0.4 to port 9000 over UDP. I have done this many times via UDP and TCP by Java, but now I have to do it with standard C++ libraries and I can't find any samples only topics where people just can't make it work.

I know that I have to encode "Jane Doe" as array of bytes then just open socket, pack it in datagram and send it.

C++ is not my first language and this is small part of code I can't figure out, I've chosen UDP because it is always much simpler than TCP.

like image 415
Yoda Avatar asked Jul 03 '14 17:07

Yoda


People also ask

Can you use Send with UDP?

UDP sockets can be connected or unconnected - in the first case send should be used and in the second sendto . This does not affect the protocol used, i.e. it still is unreliable UDP. It only affects where the destination is taken from: from the socket or given as argument.

Does UDP client need to bind?

As far as I remember bind is not required for a UDP socket because a bind call is made for you by the stack.

Can UDP client communicate with TCP server?

No, you can't have a TCP server communicating with UDP clients.


1 Answers

A good source for network programming is Beej's Guide to Network Programming. Below is some sample Unix code.

If this is Windows programming:

  • "sock" should be of type SOCKET instead of int.
  • Use closesocket instead of close
  • #include <winsock2.h> instead of all those unix headers

#include <sys/types.h> #include <unistd.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> #include <memory.h> #include <ifaddrs.h> #include <net/if.h> #include <errno.h> #include <stdlib.h> #include <iostream>   int resolvehelper(const char* hostname, int family, const char* service, sockaddr_storage* pAddr) {     int result;     addrinfo* result_list = NULL;     addrinfo hints = {};     hints.ai_family = family;     hints.ai_socktype = SOCK_DGRAM; // without this flag, getaddrinfo will return 3x the number of addresses (one for each socket type).     result = getaddrinfo(hostname, service, &hints, &result_list);     if (result == 0)     {         //ASSERT(result_list->ai_addrlen <= sizeof(sockaddr_in));         memcpy(pAddr, result_list->ai_addr, result_list->ai_addrlen);         freeaddrinfo(result_list);     }      return result; }   int main() {     int result = 0;     int sock = socket(AF_INET, SOCK_DGRAM, 0);      char szIP[100];      sockaddr_in addrListen = {}; // zero-int, sin_port is 0, which picks a random port for bind.     addrListen.sin_family = AF_INET;     result = bind(sock, (sockaddr*)&addrListen, sizeof(addrListen));     if (result == -1)     {        int lasterror = errno;        std::cout << "error: " << lasterror;        exit(1);     }       sockaddr_storage addrDest = {};     result = resolvehelper("192.168.0.4", AF_INET, "9000", &addrDest);     if (result != 0)     {        int lasterror = errno;        std::cout << "error: " << lasterror;        exit(1);     }      const char* msg = "Jane Doe";     size_t msg_length = strlen(msg);      result = sendto(sock, msg, msg_length, 0, (sockaddr*)&addrDest, sizeof(addrDest));      std::cout << result << " bytes sent" << std::endl;          return 0;  }  
like image 56
selbie Avatar answered Oct 16 '22 04:10

selbie