Sign in to follow this  
pZyHo

CSocket + directx problem

Recommended Posts

Hi, I'm developing application using directx and mfc. This is like 'client-server' application. I do some drawings using directx. Periodically I receive data (for that drawings) from server (using CSocket). For this moment I refresh screen every time I receive data from server. It seems to work, but I've tried also do something like 'game-loop' to get real fps, but it doesn't work well, because socket receives data with very small frequency. To send to server request for new data I use thread. Maybe this 'refresh thread' is a problem? I didn't want to use WM_TIMER message because I thought that another thread is a better solution. Please help if you know what I'm doing wrong ;) Regards pzyho

Share this post


Link to post
Share on other sites
Are you wanting to remove the thread and just do a game loop?

If so, your game loop should do the following:

Get the data from the socket (if any)
Get user input (if any)
Calculate the changes (again, if any)
Draw the screen.

If no changes are made in any way, you only have to keep drawing the same screen over and over.

I probably missed the point of your question, so if this is not what you want, ignore it :)

Share this post


Link to post
Share on other sites

Is the client asking the server for data?

It seems that you have problems about how to design the client/server
communiaction.

Personally I would let the client render the screen every 30+ msec or so
no matter what.
And then I would consider using the select system call in the client
to be able to listen for server data, nonblocking.
In other words... no threads.

That way you can use select somewhere in the loop and it will only trigger
if there is data to read, and you are 100% sure to get all the data
from the server.

You should be able to "poll" the data from the server this way aswell (by using nonblocking select)
Just send a request for more data after a time period... say every 10 sec...

loop
{
...

if(GetTickCount() + 10000 >= time_last_server_poll) {
send_server_request("gimme data");
time_last_server_poll = GetTickCount();
}

...

// using select here to poll for data to read

...
}




Using select however is not the the easiest thing unless you are familiar with it already ^^

[Edited by - pulpfist on November 3, 2005 9:36:46 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by pulpfist
Is the client asking the server for data?

Yes
Quote:

It seems that you have problems about how to design the client/server
communiaction.

Probably yes ;) I'm not good in it :/ (yet ;)
Quote:

Personally I would let the client render the screen every 30+ msec or so
no matter what.
And then I would consider using the select system call in the client
to be able to listen for server data, nonblocking.
In other words... no threads.

Hmm.. screen is redrawing every time client receives data. Let's say that client sends "GetData" message to server. Now client is waiting for data (if it would be a 'game-loop' is should still drawing the same picture). When data are ready to receive, socket calls OnReceive method (in client). Depending on what message we sent we call method from client (in this case OnGetData()). Then we refresh the screen.

This is my scenario. I use only 2 threads (GUI thread, and the update thread to sent "GetData" message periodically).

When I don't use 'game-loop' (redrawing only after receive data) it works pretty well, but when I turn it on (draw as much as you can ;)) there are problems with getting data from socket :/
Quote:

That way you can use select somewhere in the loop and it will only trigger
if there is data to read, and you are 100% sure to get all the data
from the server.
Using select however is not the the easiest thing unless you are familiar with it already ^^

Could you say more about it? I think I don't understand what you mean :)

Regards & thanks for all answers
pzyho

Share this post


Link to post
Share on other sites

Quote:

When I don't use 'game-loop' (redrawing only after receive data) it works pretty well, but when I turn it on (draw as much as you can ;)) there are problems with getting data from socket :/


You must make sure the game-loop dont run faster than say 32 msec and see if that helps. If not i guess the cpu will be too busy to run things smoothly.
Im not sure about this tho... maybe it wont help.

Quote:

(if it would be a 'game-loop' is should still drawing the same picture)


Yes it will if you use nonblocking sockets ^^

Hmm let me see if I can find some good examples on using select.
I will post info/links of what i find

Share this post


Link to post
Share on other sites
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/select_2.asp

http://www.codersource.net/winsock_tutorial_server_select_model.html

As you can see, using select is a bit complex, since it must
be able to handle many descriptors at the same time.

Btw Im not sure you need to use select at all, maybe you can get away with just a non-blocking read operation.

Im not sure Im of much help hehe
But Im thinking that if you want to have a 'game-loop' solution
you must either make the reading socket in the client non-blocking, or
put it in a thread so it wont interrupt the screen rendering

[Edited by - pulpfist on November 3, 2005 11:38:12 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by pulpfist
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/select_2.asp
http://www.codersource.net/winsock_tutorial_server_select_model.html

Thanks for links. I will try to read and understand it.. ;)
Quote:

Btw Im not sure you need to use select at all, maybe you can get away with just a non-blocking read operation.

CSocket sux :/ I agree with you that propably this is the main problem..

Quote:

Im not sure Im of much help hehe

Just a little ;)
Quote:

But Im thinking that if you want to have a 'game-loop' solution
you must either make the reading socket in the client non-blocking, or
put it in a thread so it wont interrupt the screen rendering

Using CSocket in another thread is hard :/ (but not impossible). Maybe I will write my own socket class without CSocket. I knew that It can't be so easy stuff ;)

Thanks for all replies.
pzyho

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