Advertisement

networking help

Started by April 15, 2001 03:06 PM
6 comments, last by Jimster_ 23 years, 9 months ago
hi, i''ve just started network programming(today actually) with the intention to make a multiplayer game. keeping it simple though i have managed, using winsock2, to create a dos program where someone runs the program as a host(who listen()s for anyone wanting to connect), then someone runs the program on another machine and connects to the host via a socket(done over the internet). the idea was to have both users sending and recieving text messages to each other like a 2-way chatroom. however i need to run recvfrom() function to check for any messages, but this seems to loop infinately until there is a message to recieve, therefore i can only have one person sending and one recieving. i thought recvfrom() would have returned if there was no messages waiting. how can i work this to allow both sending and recieving? Thanks for any help Jim
signatures are shit.
Ok.. there are 2 ways (as far as i know) to do this.. I''ve been coding Winsock only for the last week. so.. hehe...

1.) Look in the MSDN for something like Asynchronous Sockets. It''s for Win(32?) systems only. As soon as you get a package arrives you get a Windows Message that tells you that something reveived...


2.) Multithreading. Build an extra thread that just sits an waits for messages and displays them. the other thread handles the keyboard input and sends the messages... there are some good tutorials about multithreading here on gamedev.net and also on Flipcode. the advantage of multithreading is that it isn''t Win32 only...

hope that helped,
cya,
Phil

Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states
Visit Rarebyte! and no!, there are NO kangaroos in Austria (I got this question a few times over in the states ;) )
Advertisement
ah thanks for the advice, i''ll have a go at creating a thread which just deals with checking for messages to recieve.
signatures are shit.
Phueppl1, do you happen to know any resources about multithreading in Non-Windows OS''s? I use multithreading for my networking stuff under Windows, I just don''t know how to do it under other OS''s .

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/
just to let you know, if anyone else has a similar problem, i have found an easy solution. there is a function
ioctlsocket(socket, FIONREAD, &socksize); where socksize is returned as the size of the data waiting to be sent. therefore you can test if there is any data to recieve before running recvfrom().

signatures are shit.
signatures are shit.
ok, i have another problem now.
i now have it running so that 2 people can send and receive messages to each other, no problem. now i want to allow multiple users to connect to the host, all being able to send messages to the host, and when the host sends a message it goes to all users connected. the problem i have is, i now need to (as the host)
to constantly listen() for other people trying to connect. so i created this thread:
VOID ListenThread(PVOID pvoid)
{
if(listen(sockfd, BACKLOG)!=-1)
{
int sin_size = sizeof(struct sockaddr_in);
countconnected++;
connectedsockets[countconnected] =
accept(sockfd, (struct sockaddr *)
&their_addr[countconnected], &sin_size);
printf("someones connected, now %d connected
to host\n",countconnected+1);
}
}
and i also created a thread which checks for incoming messages, and allows the host to type and send messages. the thing is, the listenthread seems to be taking up much more machine time than the other thread, so i have to hold a key down for maybe 5 seconds before it realises i have pressed a key.
can i syncronise this to say, for example, only run the listen thread for 10% of the time and run the other thread for the rest of the time?
Advertisement
ok, i have another problem now.
i now have it running so that 2 people can send and receive messages to each other, no problem. now i want to allow multiple users to connect to the host, all being able to send messages to the host, and when the host sends a message it goes to all users connected. the problem i have is, i now need to (as the host)
to constantly listen() for other people trying to connect. so i created this thread:
VOID ListenThread(PVOID pvoid)
{
if(listen(sockfd, BACKLOG)!=-1)
{
int sin_size = sizeof(struct sockaddr_in);
countconnected++;
connectedsockets[countconnected] =
accept(sockfd, (struct sockaddr *)
&their_addr[countconnected], &sin_size);
printf("someones connected, now %d connected
to host\n",countconnected+1);
}
}
and i also created a thread which checks for incoming messages, and allows the host to type and send messages. the thing is, the listenthread seems to be taking up much more machine time than the other thread, so i have to hold a key down for maybe 5 seconds before it realises i have pressed a key.
can i syncronise this to say, for example, only run the listen thread for 10% of the time and run the other thread for the rest of the time?
signatures are shit.
Yes, you can, but I don''t know how (it''s been a while since I''ve done anything like that, so I forgot). It has to do with setting thread priorities, so try looking up something about that .

"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/

This topic is closed to new replies.

Advertisement