Home > Software engineering >  MFC PING and TRACE procedures, VC PING program SOCKET communication model, multithreading PING and T
MFC PING and TRACE procedures, VC PING program SOCKET communication model, multithreading PING and T

Time:10-11

Write a program to the ping and trace, the ping and trace respectively using a thread in the program implementation, meet a problem now, such as the ping and trace baidu at the same time, the ping and trace receive bag have a problem, and this is normal when two threads in isolation, application framework is as follows:

 ping: (m_hSocket is created in the thread local variable) 
M_hSocket=socket (AF_INET SOCK_RAW, IPPROTO_ICMP);
Setsockopt setting timeout buffer properties such as
Create the ICMP message
Sendto (m_hSocket, m_pIcmpData m_nDataSize, 0, (struct sockaddr *) & amp; M_addrDest, sizeof (m_addrDest));
Recvfrom (m_hSocket, m_pRecvBuf MAX_PACKET, 0, (struct sockaddr *) & amp; M_addrFrom, & amp; Fromlen);
Parsing a message

Trace and ping are similar, just before the sendto every time the message set the TTL value increased from 1 to start,
The setsockopt (m_hSocket, IPPROTO_IP IP_TTL, (char *) & amp; M_TTL, sizeof (m_TTL));


Advice genuflect is begged!

CodePudding user response:

Fyi:
 # include & lt; Windows. H> 
#include
#include
Char YN (int k) {
The FILE * f;
Char fn [40].
Char ln [80].
Char yn;
int n;

Yn='N'.
Sprintf (fn, "d: \ \ % ping which xt", k);
F=fopen (fn, "r");
If (NULL!=f) {
n=0;
While (1) {
If (NULL==the fgets (ln, 80, f)) break;//
If (STRSTR (ln, "ms")) {
Yn='Y'
break;//
}
n++;
If (n>=4) break;//
}
The fclose (f);
}
Return yn;
}
Void main (int arg c, char * * argv) {
Char CMDSTR [256].
int i;
Int IP [3];
char c;

If (argc<2) {
USAGE:
Printf (" the Usage example: \ n % s 192.168.60. \ nto test 192.168.60.1-254 \ n ", argv [0]).
return;
}
If (4==sscanf (argv [1], "% d, % d, % d % c", & amp; IP [0], & amp; IP [1], & amp; IP [2], & amp; C)) {
If (0 & lt; [0]=IP & amp; & IP [0] <=255
& & 0 & lt; [1]=IP & amp; & IP [1] <=255
& & 0 & lt; [2]=IP & amp; & IP [2] <=255
& & '. '=={
c)For (I=1; i<255; I++) {
Sprintf (CMDSTR, "ping CMD/c % s % d - 1 - w n 1000 & gt; D: \ \ % ping which xt, "argv [1], I, I);
WinExec (CMDSTR, SW_HIDE);
}
Sleep (3000);
For (I=1; i<255; I++) {
Printf (" % d % c % s \ n ", YN (I), argv [1], I);
}
Sleep (3000);
Del WinExec (" CMD/c/q d: \ \ ping *. TXT ", SW_HIDE);
} the else goto the USAGE;
} the else goto the USAGE;
}

CodePudding user response:

If two threads share data between etc, lead to receive packets lost

CodePudding user response:

No Shared variables between two threads, today also found that if at the same time open two threads ping baidu at the same time, the result is wrong, how can you ensure A thread when the SOCKET communication in the SOCKET send ping baidu package, can be A thread receives, rather than by thread B receive??
  • Related