Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C++ multithread Server

Posted on 2000-03-16
1
Medium Priority
?
581 Views
Last Modified: 2008-01-09
What's the most efficient way to implement a C++ multithread server using sockets?

I have a server that has an infinite loop. This loop has an accept socket function, and when a client connects with the server socket, the server creates a thread and relays the communication to this thread. Then, the server returns to the  loop.

Are there any more efficient ways to implement this functionality?
0
Comment
Question by:Lucsat
1 Comment
 
LVL 1

Accepted Solution

by:
karlww earned 300 total points
ID: 2625686
I assume you are refering to a console application in blocking mode.  If so, you got it;

while (1)
{
   SOCKET sa = accept(m_s,
                      (sockaddr *) &sai,
                      &nSize);
   // send sa to thread for processing
}

You can use non-blocking sockets if using WinSock, which takes more than the 1000 character this is limited to.

Good books on WinSock, number 1 covers blocking, 2 both but mainly nonblocking after a couple chapters.
1. Internetworking With TCP/IP Volume III - WinSock Version.

2. Windows Sockets Network Programming.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question