|
Add a CRITICAL_SECTION member variable to your object, initialize/release it at construction/destruction time and use it in every method of CSomeClass to serialize access to the objec from multile threads. This post of mine shows a convenient way to add CRITICAL_SECTION protection to a method with minimum effort.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hi,
I am not able to open yr post.. the page shows
no messages..
How do i see it???
Sameer
|
|
|
|
|
It's weird, I can see the page without problems... Try copying and pasting this URL to your browser:
http:
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanx..
but when do you call DeleteCriticalSection from yr
class.. isn't it necessary??
Sameer
|
|
|
|
|
Hi,
Can you show me a macro for this..
EnterCriticalSection(&cs);
myvariable->myfunc("kfsdjhkjsgdhkdg"); // string should be passed in macro
LeaveCriticalSection(&cs);
its like i want
LOG(X)
|
|
|
|
|
Can you show me a macro for this..
I could, but I'm afraid this would deprive you of the fun of figuring it out yourself.
Also, I don't think a macro is a good solution where alternatives (like an inline function) exist.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
If you want the class to be thread safe the critical section object should be a member of the class.
class CMySafeClass
{
...
CCriticalSection m_csObj;
...
} For methods that must be thread safe do this:
void CMySafeClass::Method1()
{
m_csObj.Lock();
m_csObj.Unlock();
} Hope this helps...
Jonathan Craig
www.mcw-tech.com
|
|
|
|
|
i would like to know how to use this function
i tried to do simultaniasly read/write from COM but it didnt work
thanks.
|
|
|
|
|
Hi.
I am studying networking programming from Network Programming For Microsoft Windows, Second Edition by Anthony Jones and Jim Ohlund. I am just about ready to start on a new product that will be my first real Winsock program. I want to design and implement a simple message program (chat) that work over the IP protocol and TCP protocol.
I have the basic program core design in my mind. However, I am stuck with one problem and it is a problem that even Jones and Ohlund implied could be difficult to overcome for inexperience network programmers.
How do you predetermine the size of the data you receive?
Winsock has two basic functions for receiving data: revc (IPv4 or Winsock 1.1+) and WSARecv (IPv6 or Winsock 2). WSARecv is a powerful tool for *predetermined* data size. You can send multiple packages at one time, but the data size must be predetermined (i.e. 10k, 20k, and finally 30k). I will probably use revc since it is more flexible. Even with recv, however, you must give a specific size (byte) to read. Jones and Ohlund recommend sends the size of the data in the first four byte of the data stream. However, what if the user wants to send something that is larger than what a bour byte char variable can hold?
Please post any possible solutions.
Thanks,
Kuphryn
|
|
|
|
|
You can use as many bytes as you please to store the size. Consider using 8 bytes. If your platform doesn't support 8 byte integers, you can easily split it up in two parts where, say, the first four bytes tell how many full 4.2 GByte you have, and the second four bytes are the remainder. You could even send a custom header in all your transmissions to tell how many bytes hold up the size information. This way you will never run out of space. (Read, 640 kByte RAM is more than anyone will ever need )
|
|
|
|
|
Thanks.
Can you give me an example?
Let say I want to sent this message:
"Please give an example of how you would receive this message."
// send(socket, &message, sizeof(message), 0);
Kuphryn
|
|
|
|
|
... and to receive the message you do:
const int MAX_BUFF = 1024;
char buffer[MAX_BUFF];
recv(theSocket,&buffer, MAX_BUFF, MSG_OOB);
------------------------------------
Rickard Andersson, Suza Computing
ICQ#: 50302279
I'm from the winter country SWEDEN!
------------------------------------
|
|
|
|
|
Hi,
I think that highly depends on your protocol.
I was collecting some ideas about networking and Gnutella protocol (a P2P filesharing application). Gnutella is interesting here, because it uses line buffered data stream (e.g. HTTP handshaking) and also block buffered data (e.g. binary Gnutella protocol). The article can be found at: Programming a Gnutella Client, part II: network core
Let me know if it helps, I'm learning...
|
|
|
|
|
Well, I don't think it was the right answer
But, I just wanted to know how it worked... I'm still preparing how I will build my chat program!
------------------------------------
Rickard Andersson, Suza Computing
ICQ#: 50302279
I'm from the winter country SWEDEN!
------------------------------------
|
|
|
|
|
Rickard Andersson wrote:
I'm still preparing how I will build my chat program
Have fun!
I'm on irc.p2pchat.org, if you wanna test my chat application.
|
|
|
|
|
Hi.
Okay. Basically, the program will be a client and a server. If it is connected to any other client/server, than the connect feature will be disabled.
As for the message, I plan to send and receive message and and forth. As for I/O mode and I/O model, I will use the simple WSAAsynSelect (non-blocking) model since it uses the Windows (MFC) messaging system.
Kuphryn
|
|
|
|
|
|
From heart:
char szMsg[] = "A message";
long nMsgSize = strlen(szMsg);
long nSize = nMsgSize;
nSize = htonl(nSize);
send(sock, &nSize, sizeof(long), 0);
send(sock, szMsg, nMsgSize, 0); This example uses a four byte length header.
Now, when reading
long nSize;
recv(sock, &nSize, sizeof(long), 0);
nSize = ntohl(nSize);
char *pszMsg = new char[nSize + 1];
recv(sock, pszMsg, nSize, 0);
pszMsg[nSize] = '\0'; pszMsg will now, hopefully , contain your message.
|
|
|
|
|
Wow. Nice!
Thanks.
Okay. I want to walk through your code one part at a time.
1)According to the above, you did not use "4" as byte count anywhere in the code. You used a long variable as the static size of the first four bytes. Is that correct? If yes, what is the advantage to using sizeof(long) stead of just this:
// char fourbyte[4];
//send(sock, fourbyte, 4, 0);
2) You used the function htonl() to convert to "net byte order." Jones and Ohlund dedicated a section or so to four functions that converts between byte order (host byte order to/from network byte order). Can you explain why we eed htonl()?
Dire.Wolf posted another technique using WSARevc(). Here is a link to the code.
http://www.gamedev.net/community/forums/topic.asp?topic_id=93771
I will try both your technique and the one Dire.Wolf mentioned. Although, your technique is more flexible in the sense that we do not need to know the size of the data after the first four byte. The size of the data after the first four byte will be calculated and then a character array is created at that size. Very nice!
Thanks again,
Kuphry
|
|
|
|
|
Note from MSDN
int recv(
SOCKET s,
char FAR *buf,
int len, //sizeof buffer NOT amount to read
int flags
);
"For connection-oriented sockets (type SOCK_STREAM for example), calling recv will return as much information as is currently available—up to the size of the buffer supplied"
Correct me here if I am wrong but;
So if ALL the info is not available at the time of the call it will return with less. The next time you call recv() you will be expecting a size amount but the remainder of the previous string will be there. Splat!
while(iRecv < iLen)
iRecv+= recv(socket ,&sBuffer[iRecv] ,iLen, FLAGS); //this could work
Now what happens if the amount in the socket is greater than the buffer size (ie Next string is there as well)?
recv() returns an error, WSAEMSGSIZE and waits until a buffer large enough is supplied. But you can't get a buffer large enough as you need to read from the socket to get the size of the second msg.
|
|
|
|
|
Hi.
I would like to know how to enable/disable a specific view inside a split window. For example, let there the split window comes default with three visible windows.
-----
View1
-----
View2
-----
View3
-----
If the user select an option in the menu to hide view2 or maybe a calculated needs to disable view2, is there a way to do that in MFC?
Most important that is the entire window must be redrawn. I am not sure how to redraw multiviews inside of main in SDI architecture.
Thanks,
Kuphryn
|
|
|
|
|
Hi
Could anyone kindly show me how to remove the last character in CString pls
I've tried Delete and Remove but it didnt' work
Thanks
|
|
|
|
|
Delete should work
CString cs = "String";
cs.Delete(cs.GetLength()-1);
|
|
|
|
|
Hi
Your solution just doesn't work. But thanks anyway.
Cheers
|
|
|
|
|
tcuong wrote:
Could anyone kindly show me how to remove the last character in CString pls
m_string=m_string.Left(m_string.GetLength()-1);
Nish
The posting stats are now in PDF:-
http://www.busterboy.org/codeproject/
Feel free to make your comments.
Updated - May 04th, Saturday
|
|
|
|