How to solve the problem with receiving messages from the client to winsock using UDP?

Deal with winsock, particularly with udp. When making/waiting for messages out even though the code is almost all from the microsoft documentation.
recvfrom gives an error with code 10057. Like it is connected with the socket, although the socket and bind are successful.
Server code:
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
#include <WinSock2.h>
#include <string>
#pragma warning(disable: 4996)
#include <Ws2tcpip.h>
#include <stdio.h>

#define RED "\x1B[31m"
#define GRN "\x1B[32m"
#define RESET "\x1B[0m"
int main(int argc, char* argv[] )

 int iResult = 0;
 short Port = 5000;
 char RecvBuf[1024];
 int BufLen = 1024;

 // initialize library
 WSADATA wsaData;
 WORD DLLVersion = MAKEWORD(2, 2);

 // check for error
 iResult = WSAStartup(DLLVersion, &wsaData);
 if (iResult != 0) {
 printf(RED "error" RESET);

 // create the socket 
 SOCKET sListen = socket(AF_INET, SOCK_STREAM, 0);
 // check for error
 if (sListen == INVALID_SOCKET) {
 printf(RED "socket failed with error: %ld\n" RESET, WSAGetLastError());
 return 1;

 // structure for storing data about the sender
 SOCKADDR_IN SenderAddr;
 int SenderAddrSize = sizeof(SenderAddr);

 // fill the socket structure
 int sizeOfAddr = sizeof(addr);
 addr.sin_addr.s_addr = ADDR_ANY;
 addr.sin_port = htons(Port);
 addr.sin_family was = AF_INET;

 iResult = bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
 // check for error 
 if (iResult == SOCKET_ERROR) {
 printf(RED "bind failed with error: %d\n" RESET, WSAGetLastError());
 return 1;

 printf(GRN "Server has been started\n" RESET);

 do {

 ZeroMemory(RecvBuf, BufLen);

 iResult = recvfrom(sListen, RecvBuf, 1024, 0, (SOCKADDR*)&SenderAddr, &SenderAddrSize);
 if (iResult == SOCKET_ERROR) {
 printf(RED "recvfrom failed with error: %d\n" RESET, WSAGetLastError());

 printf("Received message from %s: %s\n", inet_ntoa(SenderAddr.sin_addr), RecvBuf);

 } while (iResult > 0);


 return 0;
April 19th 20 at 12:05
2 answers
April 19th 20 at 12:07
So you have created a TCP (SOCK_STREAM) socket, which is not recvfrom

Exactly sealed - foster commented on April 19th 20 at 12:10
April 19th 20 at 12:09
You're using SOCK_STREAM, which is designed for TCP and requires the pre-installation connections (listen/connect).
Works via UDP datagrams SOCK_DGRAM.

Find more questions by tags UDPC++