Home > Software engineering >  Pray god help transfer of the code to the MFC.. [I changed many times still was full of mistakes]
Pray god help transfer of the code to the MFC.. [I changed many times still was full of mistakes]

Time:09-24

MFC with a simple dialog box can kneel and post code
PS: a little hand party embarrassed,,, you really don't come out, urgent urgent

# include & lt; stdio.h>
# include & lt; Winsock2. H>
# include & lt; Tchar. H>
# include & lt; Time. H>//client unique header file
# pragma comment (lib, "ws2_32. Lib")
//the server side -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Int _tmain (int arg c, _TCHAR * argv []) {
WSADATA wsa.
WSAStartup (MAKEWORD (2, 2), & amp; Wsa);
Struct sockaddr_in addr.
Addr. Sin_family=AF_INET;
Addr. Sin_addr. S_addr=inet_addr (" 192.168.0.106 ");
Addr. Sin_port=htons (7000);
Int len=sizeof (addr);
Struct sockaddr_in addr_server;
Addr_server. Sin_family=AF_INET;
Addr_server. Sin_port=htons (8000);
Addr_server) sin_addr) s_addr=inet_addr (" 192.168.0.106 ");
The SOCKET serversocket=SOCKET (AF_INET, SOCK_DGRAM, 0);
Bind (serversocket, (struct sockaddr *) & amp; Addr_server, sizeof (addr_server));
Struct sockaddr_in addr_from;
Int fromlen=sizeof (addr_from);
While (1) {
Char frombuff [1024]="\ 0";
Printf (" waiting for the client input information!" );
If (recvfrom (serversocket frombuff, sizeof (frombuff), 0, (struct sockaddr *) & amp; Addr_from, & amp; Fromlen)=={SOCKET_ERROR)
Printf (" there is an error in the server to accept!! ");
}
Printf (" the client input is: % s \ n ", frombuff);
//Sleep (5000);
If (sendto (serversocket frombuff, sizeof (frombuff), 0, (struct sockaddr *) & amp; Addr, fromlen)=={SOCKET_ERROR)
Printf (" server to send error!!!!! ");
}
}
Closesocket (serversocket);
WSACleanup();
return 0;
}

//the client -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Int main (void)
{
WSADATA wsa.
WSAStartup (MAKEWORD (2, 2), & amp; Wsa);
The SOCKET mySocket;
MySocket=socket (AF_INET, SOCK_DGRAM, 0);
Struct sockaddr_in addr.
Addr. Sin_family=AF_INET;
Addr. Sin_addr. S_addr=inet_addr (" 192.168.0.106 ");
Addr. Sin_port=htons (8000);
Int len=sizeof (addr);
Struct sockaddr_in addr_client;
Addr_client. Sin_family=AF_INET;
Addr_client. Sin_port=htons (7000);
Addr_client. Sin_addr. S_addr=inet_addr (" 192.168.0.106 ");
The SOCKET clientsocket=SOCKET (AF_INET, SOCK_DGRAM, 0);
Bind (clientsocket, (struct sockaddr *) & amp; Addr_client, sizeof (addr_client));
While (1) {
Printf (" please enter a string user testing latency: ");
Char buff [1024]="\ 0";
The scanf (" % s ", buff);
LARGE_INTEGER t1, t2, feq;
QueryPerformanceFrequency (& amp; Feq);//number of beats per second
If (sendto (clientsocket, buff, sizeof (buff), 0, (struct sockaddr *) & amp; Addr, len)=={SOCKET_ERROR)
Printf (" send error!!!!!! ");
}
The QueryPerformanceCounter (& amp; T1);//before beating number
If (recvfrom (clientsocket, buff, sizeof (buff), 0, (struct sockaddr *) & amp; Addr, & amp; Len)=={SOCKET_ERROR)
Printf (" receiving error!!!!!! ");
}
The QueryPerformanceCounter (& amp; t2);//after beating frequency measurement
Printf (" returned from the server: % s \ n ", buff);
Printf (" -- -- -- & gt;> Time delay: % s \ n f ", ((double) t2. QuadPart - (double) t1. QuadPart)/((double) feq. QuadPart));
}
Closesocket (mySocket);
WSACleanup();
return 0;
}

CodePudding user response:


Code without any problem, direct copy can use

CodePudding user response:

reference 1st floor WLDRZX response:

Code does not have any problem, direct copy can use the

IP lost to you, try using 127.0.0.1

CodePudding user response:

1. New MFC project, the wizard contains Windows sockets support
2. Add a button, the button open a thread,
3. The code is put inside the thread, printf to afxmessagebox show,

CodePudding user response:

This is the most basic knowledge of MFC, suggested the building or the operational process of MFC clear, otherwise this help you to harm you

CodePudding user response:

On the network have a ready-made examples, oneself read code

CodePudding user response:

I put the code in the execution of the BUTTON that made many mistakes in the part of the code, conflict each other, what all not line, get dizzy, I MFC small white,,,,

CodePudding user response:

Change is not to come out, write yourself

CodePudding user response:

Baidu search relevant keywords,

CodePudding user response:

Header files in XXXDlg. H
BUTTON function in the jump after double click the BUTTON
I was just learning MFC, to contribute to the building
  • Related