![]() |
| | #1 |
| Chad Johnson Join Date: May 2004
Posts: 154
| One, with Java, you can say something like Code: SocketServer server = new SocketServer (port);
Socket client;
while (true)
{
client = server.accept ();
if (client != null)
// do something
}
Code: void classname::OnSocketEvent (wxSocketEvent &event)
{
// do something
}
Which of these two ways is best? Which should I use? What are the advantages and disadvantages? Last edited by ChadJohnson; 09-28-2004 at 03:11 PM. |
| ChadJohnson is offline | |
| | #2 | |
| Registered User Join Date: Sep 2004 Location: California
Posts: 3,020
| The second method is called asychronous socket programming, while the first method is traditional blocking sockets. Quote:
Since your post is asking about server side programming, I will talk a little bit about the pros/cons regarding server sockets. The way I usually do a server is with blocking sockets, where a new thread is spawned for each new connection. This has the advantage where each thread can handle it's own connection, and you dont have to worry about sending the wrong data to the wrong client. It is also very easy to keep track of all the connected clients this way since each thread represents a client. One thing you need to be aware of here though, is that any shared data (data that is accessed by different threads) needs to be thread safe to avoid data corruption. With asychronous sockets on a server, everything runs on a single thread. This is always nice because you never have to worry about corrupting data via threads. The downside here though is that it is easy to lock up the server and have client waiting long amounts of time. Imagine the following function gets called when data is available to be read from the socket: Code: CMySocket::OnReceive()
{
// While you are processing the data here, all other socket connections are on hold. If
// the server takes 1 second to do the processing for a connection, and there are 50
// clients connected, you are in trouble.
}
| |
| bithub is offline | |
| | #3 |
| Chad Johnson Join Date: May 2004
Posts: 154
| OK, that clears things up pretty well. I was just kind of afraid that I was doing something wrong. Couldn't you still spawn new threads with traditional blocking? I mean you could say Code: while (true)
{
client = server.accept ();
if (client != null)
// create new thread
}
|
| ChadJohnson is offline | |
| | #4 | |
| Registered User Join Date: Sep 2004 Location: California
Posts: 3,020
| Quote:
| |
| bithub is offline | |
| | #5 |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,866
| >>If you are using blocking sockets you have to spawn a new thread for each new connected client. No, you don't. I've managed to squeak by for some time using select() with a timeout of 0, to check whether any clients have events waiting... although that's almost certainly not an ideal solution, it works and doesn't have thread data-corruption problems, and you don't get the excessive context switches that occur when you have too many threads (or so I've heard). But if you do this, you can probably just go with asynchronous sockets since in essence that's what you're doing - although this way you do have the option of blocking.
__________________ Just Google It. √ (\ /) ( . .) c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination. |
| Hunter2 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| c program client server | steve1_rm | Networking/Device Communication | 3 | 01-24-2008 10:33 AM |
| constantly reading and writing to a tcp server | liri | Networking/Device Communication | 4 | 01-22-2008 08:57 AM |
| Sending messages with few fields between client and server? | nkn | C Programming | 2 | 11-23-2007 02:10 AM |
| Server and Client process | wise_ron | Networking/Device Communication | 1 | 10-07-2006 01:11 AM |
| Web Server in Router Network... | Aidman | Tech Board | 15 | 01-17-2003 10:24 PM |