• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Endemoniada

recv() questions

6 posts in this topic

Hi guys, as a programming exerise I'm using Winsock to download images. I'm using regular blocking sockets.

 

// send request
result=send(...);
 
// I got this from the MS example (see below)
result=shutdown(hSocket,SD_SEND);
 
// receive
 
// big buffer
int bufferSize=1048576;
char* buffer=new char[bufferSize];
 
// receive buffer
int bufferlen=512;
char inbuffer[512];

int total=0;
 
do {

 result=recv(hSocket,inbuffer,bufferlen,0);

 if(result > 0){
 
  // copy to big buffer
  for(i=0;i<result;i++){
   buffer[total+i]=inbuffer[i];
  }

  total+=result;

 }

} while(result > 0);

 

Am I doing that correctly ? It seems to work alright. Is it true that recv() will block until the buffer I pass to it is full ? If I remove the shutdown line it hangs (but eventually completes), I don't know why. And should I be checking for any specific errors ? I'm just not fully understanding the recv() function.

 

Thanks.

 

0

Share this post


Link to post
Share on other sites

Hi guys, as a programming exerise I'm using Winsock to download images. I'm using regular blocking sockets.

 

 

// send request
result=send(...);
 
// I got this from the MS example (see below)
result=shutdown(hSocket,SD_SEND);
 
// receive
 
// big buffer
int bufferSize=1048576;
char* buffer=new char[bufferSize];
 
// receive buffer
int bufferlen=512;
char inbuffer[512];

int total=0;
 
do {

 result=recv(hSocket,inbuffer,bufferlen,0);

 if(result > 0){
 
  // copy to big buffer
  for(i=0;i<result;i++){
   buffer[total+i]=inbuffer[i];
  }

  total+=result;

 }

} while(result > 0);

 

Am I doing that correctly ? It seems to work alright. Is it true that recv() will block until the buffer I pass to it is full ? If I remove the shutdown line it hangs (but eventually completes), I don't know why. And should I be checking for any specific errors ? I'm just not fully understanding the recv() function.

 

Thanks.

recv blocks until it receives data, that data might fill your buffer, or it might be a partial message(recv returns what it has received, and it is imperative that you ensure the message size is correct by comparing it's result to the expected file size of the message(what determines the expected file size is determined by some protocol you have implemented to negotiate file/message transfers).

0

Share this post


Link to post
Share on other sites

The reason you need shutdown is because recv() will not return 0 until the server decides to disconnect you. If the program doesn't exit immediately after receiving the image, the server is probably keeping the connection open until it decides to time out and disconnect you later. If you use shutdown with SD_SEND you tell the server that you will not be sending anything more, so it knows that it's OK to disconnect you when the image is sent.

 

You are using recv() correctly. It will not necessarily block until the buffer is full, only until at least one byte is received or an error occurs. You already handle that correctly by saving "result" number of bytes. An error is when result < 0 (disconnect from timeout or similar), graceful disconnect is when result == 0, and result > 0 is received data as you already know.

 

Assuming this is HTTP.. and you have some more recv() code earlier to handle the HTTP header?

You could ask the server to disconnect you faster by sending a "Connection: close" HTTP header in your request, and hopefully it will conform to protocol and do so.

 

You will obviously get a problem if the image is larger than 1 MB or whatever size you use for new[]. If you want to improve your program and this is HTTP, you can parse the Content-Length: header in the response before the image, to allocate exactly the number of bytes needed.

1

Share this post


Link to post
Share on other sites

It doesn't have anything to do with your question, but since you're excercising, you might want to use memcpy() instead of the for loop as it may be faster depending on the amount of data you're copying.

 

//copy to big buffer
memcpy(buffer+total, inbuffer, sizeof(char)*result);

 

even better you could try:

 

total += recv(hSocket,buffer+total,bufferSize-total,0);

which you end the need to copy from inbuffer to buffer at each iteration in the first place.

0

Share this post


Link to post
Share on other sites
First, recv() will block until it can return at least one byte. Once it's decided to unblock, it returns as many bytes as it can in one go (meaning, if more than one byte is available in the incoming buffer.)

Second, protocols over TCP always include some kind of framing that lets you decide at the application level how much data to wait for, and when to break out of your loop.

Third, real network programs don't typically sit in a blocking loop like that, but instead are implemented as big state machines that use select() or something like it to figure out what needs to be done (if anything) at any one time. Thus, each iteration of the receive would be driven off of select() saying there is data available.

Fourth, you don't need to receive into a temporary buffer; you can receive straight into the buffer you want to get out, instead of doing a memcpy(). This saves some copying, and also lets you pass a larger amount of bytes into the potential recv() buffer, as you can pass in the distance between end-of-buffer and your current revceive buffer offset.
0

Share this post


Link to post
Share on other sites

Hi guys, thanks a lot.

 

I'm using HTTP and was not geting the header first, as Erik asked, I just learned how to do that with HEAD. When I retrieve the header do I do that with a seperate operation like this:

 

// get the header
hSocket=socket(...);
connect(...);
send(...);  //  use HEAD request
shutdown(hSocket,SD_SEND);
recv();  //  loop
closesocket(hSocket);
 
//  get the content using same steps above except GET instead of HEAD request
...
...

 

When I get the actual content can I request not to get the header ? Right now I get the whole thing, parse for the Content-Length, then offset the pointer to get at the image.

 

I was also going to ask about that temporary buffer but hplus0603 cleared that up for me.

 

Eventually I'll learn about select() but for now I just want something basic that's more fun than getting text.

 

Thanks again.

 

0

Share this post


Link to post
Share on other sites
You get the header as part of a GET request. It will tell you how many bytes are in the actual body (Content-Length) in HTTP 1.1; or the connection will be closed when all data is sent (HTTP 1.0.)

In general, when you receive data from a network, you receive into one long, contiguous buffer. Then you can decide what to do with all the data in the buffer. If it's a line-based protocol, you can look for a newline between the start and the end of the buffer. If you find the newline, process that part as a line, and then move whatever is left in the buffer down to the beginning. (Sliding the "start offset" value forward for a while is generally more efficient than actually memcopy-ing data.)

So, when receiving a HTTP response, keep reading into the buffer. Once you see \r\n\r\n, you know that's the end of the header, and the beginning of the content (let's leave Content-Transfer-* out of it for now.) The length of the data will be in the Content-Length: header, and the data will follow starting right after the \r\n\r\n separator token.

If you want to look at a simple library written in C++ to make a GET request and receive the response, you can try the HTTP-GET library and for actually using HTTP in real life, you probably want to use a ready-made library like libcurl.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0