Home > Software engineering >  Excuse me a TCP client connection efficient multiple servers?
Excuse me a TCP client connection efficient multiple servers?

Time:11-02

Now need a client connect to 30-40 server at the same time, using a for loop, in turn, connect to the server, it also can realize, is that if there are a lot of routing inaccessible or the address of the server connection is not smooth, will cost a lot of time, because Windows connect blocking mode is a 25 seconds, 30 to 40 through calculation and test needs more than 10 minutes, oneself use non-blocking way and instability, would you please tell me what the the great spirit under the stable and fast point way?

CodePudding user response:

You don't have to thread?
The thread + event driven

CodePudding user response:

I am considering using multithreading to achieve, but originally in the for, in turn, connect the code, a single after the thread function also is not stable, and message-driven probably what is thinking? Can you point in detail

CodePudding user response:

refer to the original poster huangzwin response:
now need a client connect to 30-40 server at the same time, using a for loop, in turn, connect to the server, it also can realize, is that if there are a lot of routing inaccessible or the address of the server connection is not smooth, will cost a lot of time, because Windows connect blocking mode is a 25 seconds, 30 to 40 through calculation and test needs more than 10 minutes, oneself use non-blocking way and instability, would you please tell me what the the great spirit under the stable and fast point way?

Non-blocking mode instability? Didn't see,
Inefficient's main problem is to use the way of blocking, needs to wait for a timeout when abnormal return
To multithreading and asynchronously, that is, the original single thread loop join server work
Into multiple threads, parallel processing,

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<[1]=IP & amp; & IP [1] <=255
& & 0<[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:

Ping first in not connect?

CodePudding user response:

1, complete the
with thread2, asynchronous connects the client with the completion port

CodePudding user response:

I have said more understand the upstairs

CodePudding user response:

This kind of situation I think using a thread or or use an asynchronous manner, it can only choose

CodePudding user response:

You can use the asynchronous way, try to connect using the select model

CodePudding user response:

You just test web server is available, is not really need connection and communication with them?
In this way, can use CAsyncSocket asynchronous mode, establish the batch socket, then try to connect a for loop, connection operation itself will return immediately, but each connection success may need to wait for about 20 to 30 seconds (after connection timeout) to obtain the result, if the connection is successful, it can get the result within a few seconds, is waiting for the overhead of overlap, is dozens of connection is the same time begin to wait, may only need less than a total of 40 seconds to get the full results,
But this method is very stupid,

More simple way to send the UDP broadcast to enumerated in the available IP network, adopts multithreading method, can get instant results,
http://bbs.csdn.net/topics/391046264
  • Related