|
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
|
|
|
|
|
Hi
This work perfectly
Thanks for assistant
|
|
|
|
|
how can i make my .cpp file member of my project?
|
|
|
|
|
dhon wrote:
how can i make my .cpp file member of my project?
From the "Project" menu, take "Add to Project" and then "New"
Nish
The posting stats are now in PDF:-
http://www.busterboy.org/codeproject/
Feel free to make your comments.
Updated - May 04th, Saturday
|
|
|
|
|
what does it take to make a borderless splitter window in VC6? I want to put a few views one next to each other, without the "border" between them.
is there any "simple" way to do it (meaning, one call does the work) or should I derive a class from CSplitterWnd, change a few internal member variables (sizes & colors), and force them to draw a splitter which blends with the background?
thanks.
|
|
|
|
|
Views are just windows, so you can arrange them in what ever way pleases you. If it works best for you to use CSplitterWnd, then do so; it is not too difficult to create a derived class that will draw the splitters in any way you choose. Do a search here for splitter; I think you'll have several articles to choose from.
--------
I don't think that I'm a sell-out but I do "Enjoy Coke!"... -- Bloodhound Gang, The Inevitable Return Of The Great White Dope
|
|
|
|
|
How I know what my screen mate move pass each window component (such as edge of window..etc.) ?
Thank a lot
|
|
|
|
|
What is a screen mate?
(and if you reply "that square thing in front of you mate", i will hurt you )
--------
I don't think that I'm a sell-out but I do "Enjoy Coke!"... -- Bloodhound Gang, The Inevitable Return Of The Great White Dope
|
|
|
|
|
Shog9 wrote:
and if you reply "that square thing in front of you mate", i will hurt you
LOL
Seriously I think he means some kinda funny desktop program that does fun stuff, like walk into the screen randomly and strip or something.
Nish
The posting stats are now in PDF:-
http://www.busterboy.org/codeproject/
Feel free to make your comments.
Updated - May 04th, Saturday
|
|
|
|