Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Winsock: accept() and select()

Author
Message
Master Xilo
18
Years of Service
User Offline
Joined: 8th Sep 2006
Location: Bern, Switzerland
Posted: 30th Jun 2008 13:31 Edited at: 30th Jun 2008 13:38
HI, I'm currently having a look at winsock programming. Maybe here are some people with experience around.

I have 2 questions:

1. I can give the accept() function a pointer to an address sturcture that it'll fill with the clients address. How can I get the ip as a string (char*) from that structure?

2. What's the better and faster way to make a server accept multiple clients?
- use select() and always fill a structure with all sockets to check
- make a endless loop and use some kind of event (WSAEvent...) (<-not exactly sure how this works, I don't want to use windows-messages as my server should be a console app)
- make a thread for every client

Thanks for every help!

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 30th Jun 2008 14:18 Edited at: 30th Jun 2008 14:20
1. Take a look at the documentation on the sockaddr structure. The sin_addr member contains all 4 bytes of the IP address in big-endian (with 127.0.0.1 the first byte would be 127, followed by 0, etc). Alternately you could just use this function.

2. It depends on how many clients you want to support. More efficient mechanisms are more difficult to program.

select() isn't very efficient because you have to reset the socket descriptor sets every time you call it. However it's fairly simple to program and supports a small amount of sockets just fine.

Event objects (which are unrelated to window events) aren't a bad idea but you're limited to only 64 a thread.

Using the thread-per-client model can be fairly simple but it has a lot of overhead and doesn't scale well to a lot of clients.

The most efficient method is probably overlapped I/O, in conjunction with I/O completion ports. This allows you to have one or more threads sit in a loop, processing each notification.

[edit] And one feels this should be in Programming Talk.

Master Xilo
18
Years of Service
User Offline
Joined: 8th Sep 2006
Location: Bern, Switzerland
Posted: 30th Jun 2008 18:52 Edited at: 30th Jun 2008 19:05
How does that "overlapped I/O, in conjunction with I/O completion ports" exactly work? Can you explain? Can you post a simple example or a link to a tut?

ty

Quote: "[edit] And one feels this should be in Programming Talk."

Yeah, but noone seems to visit that

EDIT
I can now get his IP by doing the following:

SOCKADDR_IN address;
int len=sizeof(SOCKADDR_IN);
SOCKET clients=accept(acceptSocket,(SOCKADDR*)&address,&len);
printf("New Client\n",i);
printf("His IP: %s",inet_ntoa((in_addr)address.sin_addr));

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 30th Jun 2008 20:20
This is a fairly good article. I did have some more links but they are on my PC which I don't have at the moment. Note that to implement this method of I/O you'll need to have some experience with threading and synchronization.

Master Xilo
18
Years of Service
User Offline
Joined: 8th Sep 2006
Location: Bern, Switzerland
Posted: 30th Jun 2008 20:29
"threading"? So then what's the diff between this and "make a thread for every client"

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 30th Jun 2008 20:42
You only need a single thread to manage many clients.

Master Xilo
18
Years of Service
User Offline
Joined: 8th Sep 2006
Location: Bern, Switzerland
Posted: 1st Jul 2008 13:13
How does MS(multisync) do it? It seems to use the windows message pump, but it works in a console window too. (?!)

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 1st Jul 2008 14:47
Yes, it uses asynchronous sockets which uses the windows message pump. Why shouldn't it work in a console application?

Master Xilo
18
Years of Service
User Offline
Joined: 8th Sep 2006
Location: Bern, Switzerland
Posted: 1st Jul 2008 18:29
How do I make my own message handler and add my own messages in a console application. I don't even know the window handle...

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 1st Jul 2008 18:42 Edited at: 1st Jul 2008 18:44
Have you done any window programming yet? If not, I'd suggest doing so to get some experience of it.

This MSDN page gives you pretty much all the information you need to know about asynchronous sockets.

Create an invisible window, associate the socket(s) with it using WSASyncSelect, and implement a message pump like so:



This code should be executed from the same thread as the thread that owns the window (which I'm guessing will be the main thread), and it works in a way that simply checks for messages rather than blocking while waiting for window messages (the latter is the standard way of writing window applications), this allows you to execute this code in a main loop. However, if the application only needs to do anything when there is socket activity, replace PeekMessage with GetMessage, so that you don't suck up CPU time unnecessarily.

I can give you more precise instructions when I've picked my PC up.

Master Xilo
18
Years of Service
User Offline
Joined: 8th Sep 2006
Location: Bern, Switzerland
Posted: 1st Jul 2008 19:00 Edited at: 1st Jul 2008 19:02
Ah ok, so you do HAVE to create a window for the message pump. I already knew about WSASyncSelect (Isn't it WSAAsyncSelect?)
But i didn't want to make an application with a windows window.

So, thank you!
(I hope you'll pick up your pc soon for the Multisync .lib thing... )

EDIT:
Do I have to add a WSAAsyncSelect(s, hWnd, wMsg, ...);
for every client or is it enough to do it for the accept socket?

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 1st Jul 2008 19:04
Quote: "Ah ok, so you do HAVE to create a window for the message pump"

Yes, you have to create a window to associate sockets with it. You could try using the console window but I think that belongs to another thread.

Quote: "Isn't it WSAAsyncSelect?"

Oops, yes!

Quote: "So, thank you!"

No problem.

Login to post a reply

Server time is: 2024-11-20 10:46:18
Your offset time is: 2024-11-20 10:46:18