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.

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.