Click here to Skip to main content
15,906,341 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to dynamically add objects to TTabControl?! Pin
[ Jûroehn ]4-Nov-03 18:23
[ Jûroehn ]4-Nov-03 18:23 
GeneralHelp with file size.. Pin
Snyp4-Nov-03 12:38
Snyp4-Nov-03 12:38 
GeneralRe: Help with file size.. Pin
Joe Woodbury4-Nov-03 15:04
professionalJoe Woodbury4-Nov-03 15:04 
GeneralRe: Help with file size.. Pin
Snyp4-Nov-03 15:35
Snyp4-Nov-03 15:35 
GeneralRe: Help with file size.. Pin
Joe Woodbury4-Nov-03 17:35
professionalJoe Woodbury4-Nov-03 17:35 
GeneralWizard 97 problem Pin
Joe Woodbury4-Nov-03 12:33
professionalJoe Woodbury4-Nov-03 12:33 
GeneralRe: Wizard 97 problem Pin
Joe Woodbury4-Nov-03 12:50
professionalJoe Woodbury4-Nov-03 12:50 
GeneralThreads and sockets Pin
michael_cowan4-Nov-03 12:08
michael_cowan4-Nov-03 12:08 
Hi.

I'm currently developing a program that uses a pool of worker threads to create network connections. My current model does not work however. I want to have all the network and socket code in a DLL so I can update it as necessary without having to re write the program.

I am looking for the best way to impliment this. Is it best to create say 10 sockets (one for each thread) and have the thread use that socket to connect() with until the pool is empty, or is it better to create a new socket each time the thread is called?

Also what is the best way to impliment non-blocking sockets in this situation? The only reason I am using non-blocking sockets in a worker thread is so that I can set a timeout for connecting and send/recv calls.

I have been having great trouble getting this to work and any thoughts, opinions are welcome however I would greatly appreciate some sample code.

Thanks,
Mike

What I have tried:

The following code is held in a dll. The dll and it's functions are resolved before the thread pool is created. As I create the worker threads they are each given the pointer to these functions (at the moment there is only ServerConnect). I use the pointer to call the functions from the worker threads.

SOCKET*		pSocket;
.
.
extern "C" __declspec (dllexport) int ServerConnect (LPCSTR lpServerName, UINT nPort)
{
	struct hostent*		pHostent;
	unsigned int		addr;
	struct sockaddr_in	server;
	int			nErrorValue;
	WSADATA			wsaData;
		
	// Initialise winsock
	if(WSAStartup(0x101,&wsaData)) {
		AfxMessageBox("WSAStartup failed");
		return-1;
	}
		
	// create socket
	pSocket = new SOCKET;
	*pSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	if(*pSocket==INVALID_SOCKET) {
		AfxMessageBox("socket failed");
		return -1;
	}
		
	// resolve target
	if(inet_addr(lpServerName)==INADDR_NONE)
		pHostent=gethostbyname(lpServerName);
	else {
		addr=inet_addr(lpServerName);
		pHostent=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
	}
	if(pHostent==NULL) {
		AfxMessageBox("gethostbyname/addr failed");
		closesocket(*pSocket);
		return -1;
	}	
		
	// connect socket to target
	server.sin_addr.s_addr=*((unsigned long*)pHostent->h_addr);
	server.sin_family=AF_INET;
	server.sin_port=htons(nPort);
	if(connect(*pSocket,(struct sockaddr*)&server,sizeof(server))==SOCKET_ERROR) {
		
		nErrorValue = WSAGetLastError();
		
		if(nErrorValue==WSAEINVAL) 
			AfxMessageBox(CString("connect() failed: ") + CString("WSAEINVAL"));
		else
			AfxMessageBox("connect() failed for another reason");
		
		closesocket(*pSocket);
		return -1;	
	}
	
	return *pSocket;
}


This works fine if I use 1-5 threads, but above that and connect() returns WSAEINVAL.

Any clues anyone?
GeneralRe: Threads and sockets Pin
valikac5-Nov-03 6:16
valikac5-Nov-03 6:16 
GeneralRe: Threads and sockets Pin
michael_cowan5-Nov-03 9:54
michael_cowan5-Nov-03 9:54 
GeneralGetClientRect...whats wrong?! Pin
Schniddel4-Nov-03 10:15
Schniddel4-Nov-03 10:15 
GeneralRe: GetClientRect...whats wrong?! Pin
JDasari6-Nov-03 7:54
JDasari6-Nov-03 7:54 
GeneralMySQL 3.51 driver and MSVC wizards Pin
Antti Keskinen4-Nov-03 9:58
Antti Keskinen4-Nov-03 9:58 
GeneralRe: MySQL 3.51 driver and MSVC wizards Pin
Antti Keskinen5-Nov-03 1:24
Antti Keskinen5-Nov-03 1:24 
GeneralDon't know how do i call it. Pin
vss1114-Nov-03 9:39
vss1114-Nov-03 9:39 
GeneralRe: Don't know how do i call it. Pin
Dominik Reichl4-Nov-03 9:45
Dominik Reichl4-Nov-03 9:45 
GeneralCHtmlView Full screen Pin
Daniel13244-Nov-03 8:20
Daniel13244-Nov-03 8:20 
QuestionHow to get count of bytes SENT and RECEIVED ? Pin
_skidrow_vn_4-Nov-03 7:53
_skidrow_vn_4-Nov-03 7:53 
AnswerRe: How to get count of bytes SENT and RECEIVED ? Pin
valikac5-Nov-03 6:19
valikac5-Nov-03 6:19 
GeneralHTML Encode a string Pin
peterchen4-Nov-03 7:28
peterchen4-Nov-03 7:28 
GeneralRe: HTML Encode a string Pin
BadJerry4-Nov-03 11:50
BadJerry4-Nov-03 11:50 
GeneralRe: HTML Encode a string Pin
Terry O'Nolley4-Nov-03 13:09
Terry O'Nolley4-Nov-03 13:09 
GeneralReBar with ComboBox Pin
Cynicannibal4-Nov-03 7:17
Cynicannibal4-Nov-03 7:17 
GeneralCDataGrid Pin
vancouver7774-Nov-03 7:01
vancouver7774-Nov-03 7:01 
GeneralPausing a download thread Pin
Todd Smith4-Nov-03 6:14
Todd Smith4-Nov-03 6:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.