Hello everyone. Im sure this is a simple winsock question but maybe im wrong...
I searched everywhere and keep hitting dead ends.
Im just learning how to use winsock and still have yet to find a good tutorial. But i found a class which i will start with. Only problem is...
When i try to recv(...); the program waits until it receives somthing. Is there a way to do like:

char msg[256];
bool quit = false;
while(!quit)
{
    if(recv(msg)) // as it goes through the loop, if it received somthing
        cout << "User: " << msg << endl; // display it
    if(GetKeyState(VK_RETURN)<0) // when you type enter
    {
        cout << "Message: "; // prompt for your message
        cin.getline(msg,256); // input your message
        send(msg); // send it
    }
    if(GetKeyState(VK_ESCAPE)<0)
        quit = true;
}

And if you know of a good winsock tutorial that starts from the very basics, doesent do like the windows ones and most of them and just gives u what you put. I wana actualy understand what im doing : /

Thanks!

BTW: i know this code would never work and/or come up w/ errors, its just a general idea of what i would like to do.

Dani AI

Generated

As showed, a plain blocking recv() will stop your loop until data arrives. was right to point out you can avoid that — here are practical, beginner-friendly ways to keep the UI/console responsive and still receive data reliably.

A simple approach on Windows is to poll the socket with select() (or use a short timeout) and call recv() only when the socket is readable. Note: select() only watches sockets on Windows (not console input), so keep keyboard polling or console reads on the main thread or use a second thread for input.

fd_set readfds;
timeval tv;
char buf[512];

FD_ZERO(&readfds);
FD_SET(sock, &readfds);
tv.tv_sec = 0;
tv.tv_usec = 100000; // 100 ms

int r = select(0, &readfds, NULL, NULL, &tv);
if (r > 0 && FD_ISSET(sock, &readfds)) {
    int n = recv(sock, buf, sizeof(buf)-1, 0);
    if (n > 0) { buf[n] = '\0'; printf("Remote: %s\n", buf); }
    else if (n == 0) { /* peer closed connection */ }
    else { int err = WSAGetLastError(); /* handle error */ }
}

Other options: run a dedicated receiver thread that blocks on recv() and posts messages to the main thread (easiest for console programs), or set a receive timeout (SO_RCVTIMEO) if you want recv() to fail after a period. For production or high-performance code consider asynchronous/overlapped IO or IOCP. Always handle recv() return values: >0 = bytes, 0 = orderly close, <0 = error (check WSAGetLastError()); when using non-blocking sockets you must handle WSAEWOULDBLOCK. Finally, implement message framing (length prefix or delimiter) — TCP can split or coalesce your sends.

To : “local” typically means loopback or LAN addresses (127.0.0.1 / 192.168.x.x). For global connections the client must use the server’s public IP and the server/router must forward the server port (and your firewall must allow it). For UDP broadcasts set SO_BROADCAST and send to the broadcast address, but many routers block directed broadcasts.

Recommended Answers

All 5 Replies

You can use ioctl on the socket with FIONBIO. Should make the socket "non-blocking" which allows it to basically continue if there is nothing on the socket. Alternative, use threads, with function pointers (or functors), and let the socket block ;)

Im sorry. What is ioctl or FIONBIO? and in what way would i use function pointers?
Sorry, im new to winsock programing.

ioctl is a function to alter the way input/output is controlled/handled/works. http://msdn.microsoft.com/en-us/library/bb736550(VS.85).aspx is a helpful site about using ioctl with winsock. One of the constants that you pass to ioctl, in order to stop the socket from blocking (blocking means waits for input) is FIONBIO.
The alternative measure is a messy way to do things, and mostly was put there jokingly. While threads and function pointers can be a great asset (when designed very carefully), they usually lead to more trouble than they end up being worth.

Thanks so much, ill have to look more into this. But it looks like this will do the job.

I can send messages in local but can't send in global. What should I do? What IP and PORT should I write?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.