|
This[^] isn't a bad example of how to use select() in a traditional C application. There's only one non-portable thing in there as far as I can tell and that could be easily replaced.
Hope that helps,
Cheers,
Ash
|
|
|
|
|
Hi, can you tell if you have a specific question or rather want to be pointed to some examples?
/M
|
|
|
|
|
If I could get a specific example of how to use an asynchronous socket ( I know how to create), but how do I use it to get/send information to the client and it has to be non-blocking. That would help greatly thank you.
Gemini S.
|
|
|
|
|
Here is the theory: An asynchronous server will notify you when something interesting happens, e.g. about a new incoming connection, when data could be sent, or data could be received on a socket. So you do not have to to start a new thread for every incoming client connection. Instead you can handle many sockets within a single thread context, on Windows for example WSAAsyncSelect[^] will notify you if any of your socket status changes (see FD_READ and FD_WRITE ).
Here are two practical examples with source code:
- Programming Windows TCP Sockets in C++ for the Beginner[^]
- CAsyncSocketEx - Replacement for CAsyncSocket with proxy and SSL support[^]
Btw, there are also a couple of C++ network libraries with asynchronous sockets. You sounded like you wanted to implement it on you own, so just wanted to let you know that there are working solutions.
Happy coding
/M
|
|
|
|
|
nice kicka$$ that is what I thought needed to hear it to have it clear it up now I just need to get the syntax down and learn how to implement it and that will work. Thanks for the help /M
Gemini S...
|
|
|
|
|
Hi,
With select you check if you can write to a socket or read data from it. But there are some things about performance...
If you target Linux... it is best to use poll.
If you can, group many sockets in one select call (usually max is 64 sockets).
It you target Win2000 or later... try advanced io completion ports. The best thing.
Best regards,
Mauro H. Leggieri
P.D.: Check setsockopt to make a socket non-blocking.
|
|
|
|
|
thaanks alot this is helpful too... Mauro preciate the help.
Gemini S...
|
|
|
|
|
Hi everyone
I've a project where i have webBrowser control to display web page contents. When a user wants to save pictures(image(s)) to hard drive instead of he/she right clicking the picture(image) and clicking 'Save Picture As...', I have a button the user click that button and programatically saves the picture(image).
I googled, post my question at MSDN, here, for a year and half no luck. I found some codes but they only save the portion of picture (image) which is displayed by screen. When ever the picture(image) is larger than the computer's physical screen size, I endes up saving partial picture(image).
This forum seems more live than any other forum I know. Please help. Help.
|
|
|
|
|
By image, do you mean the rendered html page as an image, or individual images on the page (img tags)?
I assume the first since the latter is not very difficult.
Have you tried IHTMLElementRender.DrawToDC on the root element, and supplying your own DC with a selected bitmap?
Edit: maybe it was the latter case anyway. I looked to see if there was an easy way, but it seems you cannot easily get a handle to the actual bitmap of the image. But you could still use IHTMLElementRender.DrawToDC on the img element itself I suppose.
See also:
Capture an HTML document as an image[^]
http://www.nathanm.com/ihtmlelementrender-opacity/[^]
home
modified on Sunday, June 6, 2010 4:21 PM
|
|
|
|
|
Actually, I'm tring to save individual images in a web page. The problem is if any individual image is larger than the physical size of my computr's screen, IHTMLElementRender::DrawToDC() only draws to the size of my computer screen size.
So I ended up with partial image. Do you konw how to save an entire image?
|
|
|
|
|
|
Ive tried to capture a particular area of the screen to the memory dc.
CDC dcDisplay, dcMem;
CBitmap btmDisplay, *pbtmOld;
int nWidth = GetSystemMetrics( SM_CXSCREEN );
int nHeight = GetSystemMetrics( SM_CYSCREEN );
dcDisplay.CreateDCW( (LPCTSTR)"DISPLAY", NULL, NULL, NULL );
btmDisplay.CreateCompatibleBitmap( &dcDisplay, nWidth, nHeight );
dcMem.CreateCompatibleDC( &dcDisplay );
pbtmOld = dcMem.SelectObject( &btmDisplay );
ClientToScreen( &rc );
dcMem.BitBlt( 0, 0, rc.Width(), rc.Height(), &dcDisplay, rc.left, rc.top, SRCCOPY );
dcMem.SelectObject( pbtmOld );
Now i wanna paint this bitmap to the dialog dc. But i dunno how to write the codes. Can anyone help?
PS: the above codes may have faults
|
|
|
|
|
1. Create a memory DC
2. Select your image into it
3. Blit to the paint DC (of the dialog)
4. Unselect the image from mem DC
|
|
|
|
|
I have a 3d model rendering in a simple little direct3d app. I need to render it on top of a live webcam feed, and at the end of the day I need to this to packaged as a source filter for direct show. I have seen references to vmr-9 filters being a good way to go, but honestly, I just need this work.
Any ideas/code/tutorials/anything helpful?
|
|
|
|
|
Hello everyone !
After searching the web for any kind of information about hash tables, I still don't know how to implement one in C.
basically all I know about them is that hash tables are arrays of structures, the main reason for using them are their data access speed;the information(keys) are stored in buckets; you need a hash function(division method, or multiplication method); because there are more keys than the size of table, collisions appear(most of the time resovled by chaining), let me know if I've omitted something.
|
|
|
|
|
sounds pretty good.
it also is the kind of information Wikipedia handles rather well, see here[^].
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
thanks, but not the theoretical part is my problem, but the implementation itself (practically I'm asking for a simple code in C that shows the implementation of a hash table; how do i create it, how do I rehash it if the quota exceeds 0.5 for example, etc)
|
|
|
|
|
Here is the way I imagine it could be done:
- for each item, calculate the hash value any way you see fit (but consistently of course);
- use the lowest N bits of the hash as the bucket number;
- have a bucket array with 2^N entries, each holding one pointer (initially null);
- represent each bucket as a linked list.
So to find an existing item, calculate hash, mask away all but N bits, get the list head from the bucket array, start scanning the list.
When the linked lists start to grow beyond some threshold, increase N and recreate the entire hash table.
I'm not saying that is how it is done, I'm not claiming that is the best way to do it either, but I have done such things myself in such way (long before libraries or .NET offered them). The easiest way to store words of text would be to use the first letter as a hash (not a good probability distribution, but extremely fast!).
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
|
I've decided to write the code form scratch(inspiring myself from the internet) and this what i came up with:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
typedef struct _List_t{
char *string;
struct _List_t *next;
}List_t;
typedef struct _HashTable_t{
int size;
List_t **table;
}HashTable_t;
HashTable_t *CreateTable(int);
HashTable_t *CreateTable(int size)
{
HashTable_t *NewTable;
NewTable=(HashTable_t*)malloc(sizeof(HashTable_t));
NewTable->table=(List_t**)malloc(sizeof(HashTable_t));
for(int i=0;i<size;i++)
NewTable->table[i]=NULL;
NewTable->size=size;
return NewTable;
}
unsigned int hash(HashTable_t *hashtable, char *str)
{
int a,b,p;
unsigned int hashval=0;
for(int i=0;i<<9; i++)
hashval = str[i] + (hashval << 5) - hashval;
return hashval%hashtable->size;
}
List_t *search(HashTable_t *hashtable, char *str)
{
List_t *list;
unsigned int hashval=hash(hashtable,str);
for(list=hashtable->table[hashval];list!=NULL;list=list->next)
if(strcmp(str,list->string)==0)
{
return list;
}
return NULL;
}
int insert(HashTable_t *hashtable, char *str)
{
List_t *NewList;
List_t *CurentList;
unsigned int hashval=hash(hashtable,str);
NewList=(List_t*)malloc(sizeof(List_t));
CurentList=search(hashtable,str);
if(CurentList !=NULL) return 2;
NewList->string=_strdup(str);
NewList->next=hashtable->table[hashval];
hashtable->table[hashval]=NewList;
return 0;
}
void ClearKey(HashTable_t *hashtable,char *str);
void ClearKey(HashTable_t *hashtable,char *str){
List_t *list,*temp;
int hashval=hash(hashtable,str);
list=hashtable->table[hashval];
while(list!=NULL){
if(list->string=str)
temp=list;
list=list->next;
free(temp);
}
}
void ClearTable(HashTable_t);
void ClearTable(HashTable_t *hashtable)
{
List_t *list,*temp;
if(hashtable!=NULL)
for(int i=0;i<hashtable->size;i++){
list=hashtable->table[i];
while(list!=NULL){
temp=list;
list=list->next;
free(temp); }
}
free(hashtable->table);
free(hashtable);
}
void main()
{
HashTable_t *MyHashTable;
char *s;
int sel,TableSize=12;
MyHashTable=CreateTable(TableSize);
printf("HashTable created!\n");
for(int i=0;i<MyHashTable->size;i++)
printf("%d",MyHashTable->table[i]);
printf("Select an operation\n");
printf("1. Insert 2. Search 3. Erase Other=Quit\n");
scanf("%c",&sel);
printf("Your selection is:%c",sel);
printf("\nEnter the key:");
s=(char*)malloc(sizeof(10));
scanf("%s",s);
printf("Key is %s",s);
while(sel<5){
if(sel=1)
insert(MyHashTable,s);
else if(sel=2)
search(MyHashTable,s);
else if(sel=3)
ClearKey(MyHashTable,s);
else
printf("Invalid Selection");
}
getch();
}
this is what i get when I compile it in Visual Studio
First-chance exception at 0x77052374 in HT.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x7703317f in HT.exe: 0xC0000005: Access violation reading location 0x00000004.
First-chance exception at 0x7707713d in HT.exe: 0xC0000005: Access violation reading location 0x00000014.
First-chance exception at 0x7708ac07 in HT.exe: 0xC0000005: Access violation reading location 0x06b2efe2.
First-chance exception at 0x66a2d914 (msvcr90d.dll) in HT.exe: 0xC0000005: Access violation writing location 0x06b2f010.
The thread 'Win32 Thread' (0x138) has exited with code -1073741510 (0xc000013a).
Unhandled exception at 0x66a2d914 (msvcr90d.dll) in HT.exe: 0xC0000005: Access violation writing location 0x06b2f010.
modified on Monday, June 21, 2010 8:00 PM
|
|
|
|
|
When I test following code, it cose more than 800M memory.
Why does it use so many memory?
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <map>
using namespace std;
typedef std::map<int,int> IntMap;
int _tmain(int argc, _TCHAR* argv[])
{
IntMap testMap;
int i=0;
int j =0;
for( i=1; i< 10000000;i++)
{
int k =j++;
testMap.insert(IntMap::value_type(i,i));
if( 0 == i%10000)
{
cout << "Has Deal " << i << "lines" << endl;
}
}
getchar();
return 0;
}
modified on Wednesday, July 7, 2010 11:37 AM
|
|
|
|
|
You're storing 10 million pairs of integers in a map - the storage space for 20 million integers is 80M on it's own. Each node in the map contains at least two pointers for another 160M. Quite where the rest is is anyone's guess but it doesn't take much to clock up with multiple of 10M as the smallest denomination.
Cheers,
Ash
PS: Another way of looking at it is that's only 80 bytes an entry in the map.
|
|
|
|
|
I run the program in debug mode.
I find that it still cost 388,756K memories in Release mode.
It is still using more memories than 80 + 160M.
|
|
|
|
|
Cool, not surprised it eats RAM in debug - 80 bytes per tree node isn't that unreasonable.
As for release mode, have you considered looking at the source code for map to find out how big each node is? Presumably not as on one compiler I use each node contains three pointers, the data and the key. For a 32 bit machine that's 20 bytes and the allocator rounds everything up to the nearest multiple of 16 (so that 20 bytes becomes 32...) giving you ~ 320Mb.
So you're getting the correct magnitude, I wouldn't worry about it.
Ash
|
|
|
|
|
I agree, this is huge memory usage.Are you sure that the code is compiled into release mode.If you need to allocate such huge memory blocks why don't you consider dynamic allocation?This forum thread discusses how to estimate memory used by STL map.
Life is a stage and we are all actors!
|
|
|
|
|