Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a winsock2 client server application in which server should be a multithreaded that can accept multiple clients.I am new new to c++.Using single thread it is all done.Now i want to connect multiple clients.I am very new to multithreading implementation.Can any one help me........... thanks in advance

Here is my part of code
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
C++
int					ErrorNo;
class FTP_Server
{
private:
   WSADATA              wsaData;
   SOCKET               ListeningSocket;
   SOCKET               NewConnection;
   SOCKADDR_IN          ServerAddr;
   SOCKADDR_IN          ClientAddr;
   char					SERVER_IP[16];
   char					U_Name[10];
   char					Password[40];
   char					C_U_Name[10];
   char					C_Password[10];
   int 					PORT_NO;
   int                  Len;
   char                 ReceiveBuf[1024];
   char                 BUFFER[1024];
   int                  BufLength;
  
   FILE					*READCONF;
   FILE					*SENDFILE;
   long					lSize;
   char                 PATH[100];
   WIN32_FIND_DATA		ffd;
   LARGE_INTEGER		filesize;
   TCHAR				szDir[MAX_PATH];
   size_t				length_of_arg;
   HANDLE				hFind ;
   DWORD				dwError;
   wchar_t				lpStr[1024];
   char					mbstr[1024];
   int					noOfFiles;
   char					*sharedFiles[50];
   map<string ,int>     CSOptions;
public:
	FTP_Server()
	{
		BufLength = 1024;
		Len = sizeof(ClientAddr);
		CSOptions["CommRequest"] = 1;
		CSOptions["getSharedFiles"] = 2 ;
		CSOptions["getPropFiles"] = 3;
		CSOptions["getFile"] = 4;
		CSOptions["EXIT"] = 5;
		
	}
   void ReadConfigurationFile(void);//reading configuration data from a file
   void CreateSocket(void); //socket creation 
   void BindSocket(void);//Bind and listen
   void AcceptClient(void);//Accept 
   int  AuthenticateClient(void);//authentication Uname Psss
   void AcceptRequest(void);//Incoming requests
   void CommunicationWithClients(void);
   void SendSharedFiles(void);
   void SendPropertiesOfFile(void);
   void SendFile(void);
   void CloseSockets(void);
   void CleanUp(void);
   void CommunicateWithClients(void);
   ~FTP_Server(){}
};
Posted
Updated 20-Jun-11 21:08pm
v2

1 solution

Basically, you need to create two threads on server side: one listening to new connections, another one communicating with all the clients.

You need to support a list/queue of all clients and talk to them in a round-robin order. What exactly you send and receive depends on your application-level protocol. As you need to lock out the access to the list of clients from the two threads. Use Mutex (Critical Section on Windows) or, better yet, a multiple-readers, single-writer lock.

When a client is disconnected, you need to catch an exception and schedule the client's remote socket for removal. To make it possible, you need to keep each communication cycle in try-catch block and repeat communication in cycle after the exception is handled. You can handle the situations when, for example, a client computer looses power or is shut down be any reason. You cannot handle the situation with disconnected cable. For this purpose, a timeout can work. This is my opinion about "graceful" disconnection: it is redundant. As non-graceful disconnection is always possible and should be properly handles anyway, there is no sense in bothering about graceful disconnection protocols.

See also my past answers:
Exception handling in C++: Unhandled Exception : Access Violation[^];
Networking with multiple clients (the answer is about .NET, but many ideas are similar): Multple clients from same port Number[^].

Worst thing you could do is dedicating a separate thread to each client. Actually, this is a well-known architectural mistake. It makes the system very unstable, as nobody knows how many clients will try to connect during run-time. Number of threads should be predictable and better be constant.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900