Simple Winsock Question?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Simple Winsock Question?

 
0
  #1
Jan 10th, 2009
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:

  1. char msg[256];
  2. bool quit = false;
  3. while(!quit)
  4. {
  5. if(recv(msg)) // as it goes through the loop, if it received somthing
  6. cout << "User: " << msg << endl; // display it
  7. if(GetKeyState(VK_RETURN)<0) // when you type enter
  8. {
  9. cout << "Message: "; // prompt for your message
  10. cin.getline(msg,256); // input your message
  11. send(msg); // send it
  12. }
  13. if(GetKeyState(VK_ESCAPE)<0)
  14. quit = true;
  15. }

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.
Last edited by u8sand; Jan 10th, 2009 at 10:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Simple Winsock Question?

 
0
  #2
Jan 10th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: Simple Winsock Question?

 
0
  #3
Jan 10th, 2009
Im sorry. What is ioctl or FIONBIO? and in what way would i use function pointers?
Sorry, im new to winsock programing.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Simple Winsock Question?

 
0
  #4
Jan 10th, 2009
ioctl is a function to alter the way input/output is controlled/handled/works. http://msdn.microsoft.com/en-us/libr...50(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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 117
Reputation: u8sand is on a distinguished road 
Solved Threads: 15
u8sand's Avatar
u8sand u8sand is offline Offline
Junior Poster

Re: Simple Winsock Question?

 
0
  #5
Jan 10th, 2009
Thanks so much, ill have to look more into this. But it looks like this will do the job.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: lashatt2 has a little shameless behaviour in the past 
Solved Threads: 0
lashatt2 lashatt2 is offline Offline
Newbie Poster
 
0
  #6
22 Days Ago
I can send messages in local but can't send in global. What should I do? What IP and PORT should I write?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC