Solved

C++ multithread Server

Posted on 2000-03-16
1
559 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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now