![]() |
| | #1 |
| Registered User Join Date: Oct 2004
Posts: 93
| Help in experience gaining for network programming I believe a firm understanding of network programming is something every programmer should have in his armory. Although I know how to use the basics of socket programming to communicate between a client and a server I do not have experience of network programming "best practices". To help me learn this I intend to write a client and server application like a messaing program, purely to help me learn how to use winsock correctly. The server will be ran on my PC and allow multiple buddies to connect to it and chat, share files, play games etc etc. To make it simple the multiple client buddies cant talk to each other but only to the single server buddy. The server buddy must be able to chat to multiple buddies at any time and show when each buddy is typing a message. It should also be able to read the status of buddies and acknowledge any changes to their screen name. Now with my current winsock knowledge I can write the above program already but like most programming designs there is the right way to do it and there are many wrong ways to do it. It is the learning of the right way to do it that I plan to learn with this project. So my question is to you is (without requring code examples just "best practices"): How would you design such a system to make it stable, flexible and easily maintainable? Should the socket be wrapped in a RAII class and im assuming it should all be multithreaded for each buddy connection? If so how stable is winsock in a multithreaded environment? Also how would you design the communication between the server and client so they communicate effectively in the face of errors, lost packets etc. I know im asking quite a lot but I consider this knowledge to be invaluable. Thanks for any help |
| cloudy is offline | |
| | #2 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,748
| Winsock provides the (roughly) same API as POSIX sockets, so it's not really winsock you need to learn, but the basic ideas underlying network programming in general. If you're already using UNIX sockets, you should be able to convert to winsock with few problems. At the heart of any network communications program is a main loop that looks like this: Code: while(TRUE)
{
wait_for_io_activity();
read_incoming_messages();
process_incoming_messages();
send_outgoing_messages();
}
If your application generates events from timers (in other words, it does something more than just respond to client input), the wait_for_io_activity() function is where timeouts are handled. The general idea is that you have queues of incoming and outgoing messages, and these are serialized/deserialized from the data between client and server. If you want to avoid blocking, this introduces a layer of buffering which makes things harder. Thus the trend recently to handle only a single client per each thread.
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot |
| brewbuck is offline | |
| | #3 | |
| mastering the obvious Join Date: Jul 2008 Location: SE Queens
Posts: 5,061
| Quote:
You can also use select() to do this, but I'm not sure if you can use select() with winsock. Anyway, it will much simplify things while you get all the networking details worked out. Or if you are that comfortable with threading, you could just start writing it that way from scratch.
__________________ “The essential element in the black art of obscurantism is not that it wants to darken individual understanding, but that it wants to blacken our picture of the world, and darken our idea of existence.” [Friedrich Wilhelm Nietzsche, 1878] | |
| MK27 is online now | |
| | #4 |
| CSharpener Join Date: Oct 2006
Posts: 5,284
| Yes you can
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #5 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,748
| Quote:
My preferred method for handling client IO and timeout events is to maintain a heap (the data structure, not the memory allocator) of timer events, with the soonest-firing event on the top of the heap. Before calling select(), you remove the heap root, check its timeout, and use that value for your call to select(). If select() times out, you know the timer has expired and you can go off and handle it. If select() doesn't time out, you must compute the time remaining until the event fires, then reinsert it into the heap. This "asynchronous core" is common to most network programs, and it's something you should write once, get it working, and leverage that in future projects. As far as handling each client in a single thread, this is popular these days, but it has some serious downsides. For one, now you are writing multithreaded code, and you need to deal with synchronization properly. For another, how are you going to handle 100 clients? Are you really going to start 100 threads?
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot | |
| brewbuck is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Programming opportunities! (Midway Games, Inc) | Midwayrecruiter | Projects and Job Recruitment | 0 | 08-20-2008 11:02 AM |
| Are You An Experienced Programmer? Do You Like Football/soccer? (uk) | Mark Smulders | Projects and Job Recruitment | 2 | 06-28-2007 01:53 AM |
| Printing in XP using C | j2005 | C Programming | 16 | 03-07-2006 06:34 AM |
| 32-bit ASM or COM :: Experience | kuphryn | Windows Programming | 3 | 10-04-2002 12:39 PM |
| Gaining Experience in C/C++ Programming | stevey | A Brief History of Cprogramming.com | 19 | 03-29-2002 12:45 PM |