Receiving the data
After we have correctly set up the sockets and sent the data, the next step is to receive the data. Receiving data is pretty simple and just involves a few lines of code.
Getting ready
To work through this recipe, you will need a machine running Windows and Visual Studio.
How to do it…
In this recipe, we will see how easy it is to receive data over the network. There are two ways to do it, either by using the recv
function or by using the recvfrom
function:
#define WIN32_LEAN_AND_MEAN #include <winsock2.h> #include <Ws2tcpip.h> #include <stdio.h> // Link with ws2_32.lib #pragma comment(lib, "Ws2_32.lib") #define DEFAULT_BUFLEN 512 #define DEFAULT_PORT "27015" int __cdecl main() { //---------------------- // Declare and initialize variables. WSADATA wsaData; int iResult; SOCKET ConnectSocket = INVALID_SOCKET; struct sockaddr_in clientService; char *sendbuf = "this is a test"; char recvbuf...