|
CTRL + W
Add member variable
Find and replace
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
Hello, this is probably my first post ever in this forum.
I need your help with writting two simple and small programs that simulate a server and a client running on the same computer(same host). In addition, one pair of client/server would concern TCP(stream sockets) and another that works with UDP(datagram sockets).
The two main classes that would interest me are CAsyncSocket and CSocket. If there is anyone who could show me a very simple code (like an echo application) or provide me a tutorial/link with such programs ,I would be most happy.
Note that if you're too bored or unwilling to provide code ,I wouldn't mind if you provided me a tutorial on how to build such a program.
Thank you in advance.
|
|
|
|
|
|
Here is some server code that listens on port 13:
void main( void )
{
WSADATA wsaData;
int rVal;
SOCKET client;
WSAStartup(MAKEWORD(1, 1), &wsaData);
SOCKET s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
fprintf(stderr, "Failed socket()\n");
WSACleanup();
return;
}
SOCKADDR_IN sin, clientaddr;
sin.sin_family = PF_INET;
sin.sin_port = htons(13);
sin.sin_addr.s_addr = INADDR_ANY;
rVal = bind(s, (LPSOCKADDR) &sin, sizeof(sin));
if (rVal == SOCKET_ERROR)
{
fprintf(stderr, "Failed bind()\n");
WSACleanup();
return;
}
rVal = listen(s, 2);
if (rVal == SOCKET_ERROR)
{
fprintf(stderr, "Failed listen()\n");
WSACleanup();
return;
}
for (;; )
{
int addrlen = sizeof(clientaddr);
client = accept(s, (struct sockaddr *) &clientaddr, &addrlen);
if (client < 0)
continue;
char *clienthost = inet_ntoa(clientaddr.sin_addr);
int port = ntohs(clientaddr.sin_port);
fprintf(stderr, "Received request from [%s] on port [%d]\n", clienthost, port);
rVal = send(client, "Have a nice day!", 16, 0);
closesocket(client);
}
closesocket(s);
} Here is some client code that requests data from port 13:
void main( void )
{
WSADATA rWSAData;
int nIndex = 0,
nResult,
nBytes;
SOCKET rSocket;
SOCKADDR_IN rSocketAddr;
char sBuffer[128];
if (WSAStartup(MAKEWORD(1, 1), &rWSAData) == 0)
{
rSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (INVALID_SOCKET != rSocket)
{
unsigned long ul = 1;
nResult = ioctlsocket(rSocket, FIONBIO, (unsigned long *) &ul);
rSocketAddr.sin_family = AF_INET;
rSocketAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
rSocketAddr.sin_port = htons(13);
nResult = connect(rSocket, (LPSOCKADDR) &rSocketAddr, sizeof(rSocketAddr));
if (SOCKET_ERROR == nResult && WSAGetLastError() == WSAEWOULDBLOCK)
{
timeval tv = {0};
tv.tv_sec = 5;
fd_set fdread;
FD_ZERO(&fdread);
FD_SET(rSocket, &fdread);
nResult = select(0, &fdread, NULL, NULL, &tv);
if (nResult > 0)
{
if (FD_ISSET(rSocket, &fdread) != 0)
{
nBytes = recv(rSocket, sBuffer, sizeof(sBuffer), 0);
if (nBytes > 0)
{
sBuffer[nBytes] = '\0';
fprintf(stderr, "Data from %s is ]%s[", inet_ntoa(rSocketAddr.sin_addr), sBuffer);
}
else
fprintf(stderr, "recv() failed. Error = %lu\n", WSAGetLastError());
}
else
fprintf(stderr, "FD_ISSET() failed.\n");
}
else
{
if (0 == nResult)
fprintf(stderr, "select() failed. Timeout\n");
else
fprintf(stderr, "select() failed. Error = %lu\n", WSAGetLastError());
}
}
else
fprintf(stderr, "connect() failed. Error = %lu\n", WSAGetLastError());
if (closesocket(rSocket) == SOCKET_ERROR)
fprintf(stderr, "closesocket() failed. Error = %lu\n", WSAGetLastError());
}
else
fprintf(stderr, "socket() failed. Error = %lu\n", WSAGetLastError());
nIndex++;
if (WSACleanup() == SOCKET_ERROR)
fprintf(stderr, "WSACleanup() failed. Error = %lu\n", WSAGetLastError());
}
else
fprintf(stderr, "WSAStartup() failed. Error = %lu\n", WSAGetLastError());
}
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
|
|
|
|
|
The MFC classes are generally considered to be a bit useless. They're based around the essentially obsolete window-based model. New applications should probably do their communications on a separate thread.
The Winsock Programmer's FAQ[^] is a useful resource.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
|
Is there another way to detect when a user presses control plus "C" other than the signal command? Thanks.
|
|
|
|
|
In a console application, see SetConsoleCtrlHandler. For a Windows application, Ctrl+C doesn't get any special handling - you simply get WM_KEYDOWN , WM_CHAR , WM_KEYUP messages.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Sorry My English is very bad.
I start to learn VC++. you help me.
I want to see to create a member variable of type a class ( exam: CPolyBtn)
Thank a lot
Hello
|
|
|
|
|
if i understand you correctly
//don't forget to include the header file
#include "PolyBtn.h"
class CMySnazzyClass {
public:
CPolyBtn m_polybtn;
};
hope this is what you were asking otherwise i'll be embarrassed
i can't even remember what my point is now, but anyway, i'm correct
Barring unforeseen acts of God and Adminstrators, my server will be up tomorrow. I'm more worried about the Adminstrators.
|
|
|
|
|
as you said, it's not very easy to understand you, but anyway, i've got a little idea of what you want.
that is very simple. When you've designed a class, then you can use it as a nem type (user-defined type). You can so create variables of that types of any kind... globals, variables, members... check this :
<font style="color:blue;">class</font> A {
<font style="color:green;">
<font style="color:blue;">public</font>:
<font style="color:green;">
};
<font style="color:blue;">class</font> B {
A m_myAMember; <font style="color:green;">
<font style="color:blue;">public</font>:
<font style="color:green;">
};
hope that was helpful
TOXCCT >>> GEII power
|
|
|
|
|
Thank a lot
but it's wrong
I have a Class: CPolyBtn
I have a control: Button : IDC_BUTTON1
in MFC ClassWinzard I need :
ControlIDs Type Member
IDC_BUTTON1 CPolyBtn m_polybtn
In Dialog : Add Member Variable. I need:
Variable Type:
Cpolybtn
Help me. Thank alot
Hello
|
|
|
|
|
i'm not sure to understand this time. you want to add a CPolyBtn into a Button control ?
is that is right, you have to override the CButton class, an the add the member :
<font style="color:blue;">class</font> CMyButton : <font style="color:blue;">public</font> CButton {
CPolyBtn m_polybtn;
<font style="color:blue;">public</font>:
<font style="color:green;">
};
is that correct ?
TOXCCT >>> GEII power
|
|
|
|
|
oh sorry, i start to understand...
<font style="color:blue;">class</font> CPolyBtn: <font style="color:blue;">public</font> CButton {
<font style="color:green;"></font> m_polybtn;
<font style="color:blue;">public</font>:
<font style="color:green;">
};
but, what type will be your variable member ?
TOXCCT >>> GEII power
|
|
|
|
|
I think I understand what you saying to us.
What you want to do.
From the way I construe it is he wants to connect IDC_BUTTON1 with m_polybtn of type CPolyBtn.
There are two ways of doing it.
- Is to usethe class wizard in the normal way setting IDC_BUTTON1 of type CButton then going to header (*.h) file and replacing CButton with CPolyButton making sure that you add
#include"PloyButton.h" you also get there by using the menu Edit->Find in files type in m_polybtn or
- You delete the class wizzard (*.clw) file and start again. adding all the files then when you add a variable the CPolyButton will come up this time
I hope you've understood my English.
I'll e-mail you a sample project with and picture explantion
Alton
|
|
|
|
|
Thank Alton Williams. That's right.
Thank everybody
Hello
|
|
|
|
|
Hello All,
I have a Subclassed CListCtrl. I am using a bitmap as my custom scrollbar over the orignial scollbar. I understand that the scrollbar used in CListCtrl is not a window control in true sense.
Is there a way to stop the MFC, painting the original scroll bar. i.e in cases like when I drag the header control, the original scroll bar should not get displayed. Currently my own scrollbar (implemented as bitmap) is getting displayed over the original scrollbar (brown colour) and this is giving a flickering effect. Hiding the scrollbar is not a solution for me and I have tried that. I cannot use the ModifyStyle function to hide nor use the Flat Scrollbar concept.
Is it possible to intercept the SBM_SETRANGEREDRAW message, which I think is used everytime to draw the orginal scrollbar. Am I correct? How can I intercept this message. I tried overloading WindowProc but of no help.
I don't want the orignial scrollbar to get displayed in other cases too. Like draggging the thumb etc...
Though I have reduced the thumb flickering to a large extent. Some amount of flickering is still there. I want to avoid this too by stopping the painting of original MFC scrollbar. Any idea please.
Thanks
Rajdeep
|
|
|
|
|
I'll chip in after about 6 months of feddling with scroll-bars in the background of my work.
Up to and including Windows-2000:
You can eliminate window's scroll-bars by simple overriding OnNcCalcSize and OnNcPaint . You can either have OnNcCalcSize return no Non-Client area and bham, no scroll-bar (that's because scroll-bars are not autonomous windows, they are siply drawn in the window's Non-Client area).
You can also return some non-client area and your OnNcPaint can paint your scroll-bar there, or just override OnNcPaint and don't do anything there, that way you can have a non-client area and STILL have windows not draw it's scroll-bars (remember, in these versions of windows, the scroll-bars are drawn by OnNcPaint ).
Windows-XP:
This version of windows introduces a new problem. The minute you have a non-client area, windows will manage to throw it's scroll-bars in there. No matter if you disabled them using the Flat-ScrollBar API, or even overrided OnNcPaint , it will draw them, I suspect it does it in the common-control's OnScroll.
I hope this gave you good enough information.
My final approach for this all was to have my scrollbar as an independant window right next to my scrollable-control. The control itself had no non-client area (so that window's XP won't draw it's own scroll-bar there), and I tied everything up nicely...
Hope this helped...
|
|
|
|
|
Hello,
I am not exactly able to figure out how do I make CListCtrl::OnNcCalcSize() return a no Non-Client area information. Basically I need to have a CListCtrl created with no Non Client area. So that the system doesn't draw it's own scroll-bar there - in win xp.
Any suggestions or a small code snippet will be of great help in this regard.
Thanks
Rajdeep
|
|
|
|
|
Simply override CListCtrl's OnNcCalcSize and make your implementation return immediately.
Notice that this will also remove any border your list may have had using the default implementation of OnNcCalcSize...
|
|
|
|
|
I have two windows, let's call them Frame and List.
They are both laid on a dialog, both are direct children of this dialog.
List stands in the middle of the dialog, frame does too only that it's slightly larger than the list, just enough to add a frame around the list.
The thing is, when resized (together ofcourse) the list flickers.
Removing the list's OnEraseBkgnd eliminates the flicker, but also eliminates the list's background
Note that the frame only draws AROUND the list, i specifically don't draw anything where the list is positioned so that I won't cause flickering. I have an empty OnEraseBkgnd that returns TRUE.
Can someone assist ?
|
|
|
|
|
ohadp wrote:
I have an empty OnEraseBkgnd that returns TRUE.
why don't you draw the background urself and return TRUE
get the system color for window and draw away
i can't even remember what my point is now, but anyway, i'm correct
Barring unforeseen acts of God and Adminstrators, my server will be up tomorrow. I'm more worried about the Adminstrators.
|
|
|
|
|
The Frame doesn't need to erase it's background (because it omstly contains the list),
so that's why it has an empty implementation of OnEraseBkgnd .
The List on the other hand does need a background, and in the List's OnEraseBkgnd I do clean the background and return TRUE.
My problem is that this causes the list to flicker.
The List didn't flicker when there was no frame around/over it...
My question is how to eliminate the flicker, or more importantly why did it start flickering now ? when I didn't have my Frame the List cleaned it's background and nothing flickered....
|
|
|
|
|
|
I can't say I've made sure all the windows (including the dialog) have this style, but I have tried to give the Frame itself the WS_CLIPSIBLINGS style, and give the list the container of these two windows (the dialog), the WS_CLIPCHILDREN style, but not all of them together...
Can you explain why this will work ? Will it provide the list with a different clipping-region when it gets to 'OnEraseBkgnd' ?
thanks
|
|
|
|
|