Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and work.

aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

You have the vaccine, now let everyone know!
Teach Your Child To Read

BlueHost Web Hosting Black Friday

Bonsai Boy of New York Bonsai Trees and Accessories

DreamCloud The Comfortable Luxury Mattress Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

17,954 questions

24,129 answers

573 users

How to create UDP client server socket communication on windows in C

1 Answer

0 votes
/*
You need to link the library: libws2_32.a 
OR
-lwsock32
-lws2_32
from c:\TDM-GCC-64\x86_64-w64-mingw32\lib32\
OR
c:\MinGW\lib\
if you using GCC on windows
*/
   
// Client and Server are on the same computer, with different exe files and two console windows 

// UDP Server program

#include <stdio.h>
#include <WinSock2.h>

int main() {
    printf("UDP SERVER\n");

    WSADATA Winsockdata;
    SOCKET UDPServerSocket;
    struct sockaddr_in UDPClientAddr;
    int UDPClientAddrLen = sizeof(UDPClientAddr);
    char Buffer[512];

    // STEP-1 WSAStartUp
    if (WSAStartup(MAKEWORD(2, 2), &Winsockdata) != 0) {
        printf("SERVER: WSAStartUp Failed");
    }
    printf("SERVER: WSAStartUp Success\n");

    // STEP-2 Fill UDPClient Struct
    UDPClientAddr.sin_family = AF_INET;
    UDPClientAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    UDPClientAddr.sin_port = htons(8888);

    //STEP-3 Create Socket
    if ((UDPServerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET) {
        printf("SERVER: UDP Server: Create Socket Failed, Error: %d\n", WSAGetLastError());
    }
    printf("SERVER: UDP Server: Create Socket Success\n");

    //STEP-4 bind
    if (bind(UDPServerSocket, (SOCKADDR*)&UDPClientAddr, sizeof(UDPClientAddr)) == SOCKET_ERROR) {
        printf("SERVER: Binding Failed, Error: %d", WSAGetLastError());
    }
    printf("SERVER: Binding Success\n");

    // STEP-5 RecvFrom Message from Client
    if (recvfrom(UDPServerSocket, Buffer, sizeof(Buffer), MSG_PEEK, (SOCKADDR *)&UDPClientAddr, &UDPClientAddrLen) == SOCKET_ERROR) {
        printf("SERVER: Receive Failed, Error: %d\n", WSAGetLastError());
    }
    printf("SERVER: Received Message From Client: %s\n", Buffer);

    // STEP-6 Close Socket
    if (closesocket(UDPServerSocket) == SOCKET_ERROR) {
        printf("SERVER: Close Socket Failed, Error: %d\n", WSAGetLastError());
    }
    printf("SERVER: Close Socket Success\n");

    // STEP-7 Clean
    if (WSACleanup() == SOCKET_ERROR) {
        printf("SERVER: WSACleanup Failed, Error: %d\n", WSAGetLastError());
    }
    printf("SERVER: WSACleanup Success\n");

    return 0;
}




/*
run:

UDP SERVER
SERVER: WSAStartUp Success
SERVER: UDP Server: Create Socket Success
SERVER: Binding Success
SERVER: Received Message From Client: Hello from Client!
SERVER: Close Socket Success
SERVER: WSACleanup Success

*/
// UDP Client program

#include <stdio.h>
#include <WinSock2.h>

int main() {
    printf("UDP CLIENT\n");

    WSADATA WinSockData;

    SOCKET UDPClientSocket;
    struct sockaddr_in UDPServerAddr;
    char Buffer[512] = "Hello from Client!";

    //STEP-1 WSASatrtUp
    if (WSAStartup(MAKEWORD(2, 2),&WinSockData) != 0) {
        printf("CLIENT: WSAStartUp Failed\n");
    }
    printf("CLIENT: WSAStartUp Success\n");

    // STEP-2 Fill UDPServerAddr
    UDPServerAddr.sin_family = AF_INET;
    UDPServerAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    UDPServerAddr.sin_port = htons(8888);

    // STEP-3 Create Socket
    if ((UDPClientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET ) {
    //if (iConnect == SOCKET_ERROR) {
       printf("CLIENT: UDP Create Socket Failed, Error: %d\n", WSAGetLastError());
    }
    printf("CLIENT: UDP Create Socket Success\n");

    // STEP-4 Sendto Message to Server
    if (sendto(UDPClientSocket, Buffer, sizeof(Buffer), MSG_DONTROUTE, (SOCKADDR *) &UDPServerAddr, sizeof(UDPServerAddr)) == SOCKET_ERROR) {
        printf("CLIENT: Send Message Failed, Error: %d\n", WSAGetLastError());
    }
    printf("CLIENT: Message Sent!\n");

    // STEP-5 Close Socket
    if (closesocket(UDPClientSocket) == SOCKET_ERROR) {
        printf("CLIENT: Close Socket Failed, Error: %d\n", WSAGetLastError());
    }
    printf("CLIENT: Close Socket Success\n");

    // STEP-6 Clean
    if (WSACleanup() == SOCKET_ERROR){
        printf("CLIENT: WSACleanup Failed, Error: %d\n", WSAGetLastError());
    }
    printf("CLIENT: WSACleanup Success\n");

    return 0;
}




/*
run:

UDP CLIENT
CLIENT: WSAStartUp Success
CLIENT: UDP Create Socket Success
CLIENT: Message Sent!
CLIENT: Close Socket Success
CLIENT: WSACleanup Success

*/

 




answered Apr 26 by avibootz
...