BubLblckZ
BubLblckZ

Reputation: 463

How to create an "event" system for recieving packets

So I've been working on some different networking programs in c++ and I've come across a problem. I use Winsock (version 2) for this, no idea if this is the best thing to use but whatever. So in winsock there is a function to receive a packet (actually 2 functions) and what it does is it waits until it gets a packet and then keeps going, freezing the program while waiting for the packet. What I think I need is some kind of "event" system where there is a function that gets called every time a packet comes in, and the program keeps running while waiting for a packet.

So basically right now if I just want to print the (tcp) packets I'm receiving I have this (first some startup code and creating the sockets and stuff):

char buffer[4096];

while(true){

    ZeroMemory(buffer, 4096);

    recv(clientSocket, buffer, 4096, 0); //Program freezes until it receives something

    std::cout << buffer << std::endl;

}

But what if I have a window that I want to keep updating while waiting for a packet? The program is just going to freeze.

Upvotes: 1

Views: 918

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595827

WinSock offers several event-driven models for non-blocking socket operations:

Upvotes: 2

Related Questions