Rokas Višinskas
Rokas Višinskas

Reputation: 543

Convert TCP server to UDP server

So I'm trying to create a Winsock UDP server out of a TCP server, but I just can't seem to get it to work. The official winsock documentation doesn't seem to cover UDP servers (atleast as far as I could find).

The working TCP server is here:

#include <iostream>
#include <ws2tcpip.h>
#include <windows.h>

using namespace std;

int main()
{
    const char* port = "888";
    char message[50] = {0};

// Initialize WINSOCK
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2,2), &wsaData);

// Create the listening socket
    SOCKET ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 
    SOCKET DataSocket;

// Initialize the sample struct and get another filled struct of the same type and old values
    addrinfo hints, *result(0); ZeroMemory(&hints, sizeof(hints));
    hints.ai_family   = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags    = AI_PASSIVE;

    getaddrinfo(0, port, &hints, &result);

// Bind the socket to the ip and port provided by the getaddrinfo and set the listen socket's type to listen
    bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
    listen(ListenSocket, SOMAXCONN); // Only sets the type to listen ( doesn't actually listen )

// Free unused memory
    freeaddrinfo(result);

// Accept a connection
    DataSocket = accept(ListenSocket, 0, 0);
    cout << "Connected!" << endl << endl;

// Recieve data
    while(true){
        recv(DataSocket, message, 10, 0);
        cout << "Recieved: \n\t" << message << endl << endl;
        system("cls");
        Sleep(10);
    }

// Shutdown
    shutdown(DataSocket, SD_BOTH);
    shutdown(ListenSocket, SD_BOTH);
    WSACleanup();
    exit(0);
    return 0;
}

How could I convert it to a working UDP server? In my experience just changing the protocol and socktype doesn't cut it.

Code update:

const char* port = "888";
char message[50] = {0};

// Initialize WINSOCK
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);

// Create the listening socket
SOCKET DataSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 

// Initialize the sample struct and get another filled struct of the same type and old values
addrinfo hints, *result(0); ZeroMemory(&hints, sizeof(hints));
hints.ai_family   = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
hints.ai_flags    = AI_PASSIVE;

getaddrinfo(0, port, &hints, &result);

// Bind the socket to the ip and port provided by the getaddrinfo and set the listen socket's type to listen
bind(DataSocket, result->ai_addr, (int)result->ai_addrlen);
listen(DataSocket, SOMAXCONN); // Only sets the type to listen ( doesn't actually listen )

// Free unused memory
freeaddrinfo(result);

// Recieve data
while(true){
    int bytes = recvfrom(DataSocket, message, 20, 0, 0, 0);
}

Upvotes: 0

Views: 4876

Answers (1)

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136256

To convert a TCP server into a UDP server at least the following changes must be done:

  1. Replace SOCK_STREAM with SOCK_DGRAM; IPPROTO_TCP with IPPROTO_UDP.
  2. Remove listen and accept calls.
  3. Replace recv with recvfrom.
  4. Replace send with sendto.

Upvotes: 2

Related Questions